•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 423,880 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,199 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 2792 | Replies: 6
![]() |
•
•
Join Date: Feb 2004
Posts: 11
Reputation:
Rep Power: 5
Solved Threads: 0
Hi all, i need help for a little php script, here is what i need:
I have a table named "clans" with 4 field in it (name, url, logo, description)
I have a php page with "A-B-C-D-E-F-G-H-I-........."
I need to create the links on the letters that will only show all clans that begins with the clicked letter
I just dont have a damn idea on how to do this, i am sure that the php gurus out there can help me with something simple, i only code for fun and im still newb. Thanks in advance
fred
I have a table named "clans" with 4 field in it (name, url, logo, description)
I have a php page with "A-B-C-D-E-F-G-H-I-........."
I need to create the links on the letters that will only show all clans that begins with the clicked letter
I just dont have a damn idea on how to do this, i am sure that the php gurus out there can help me with something simple, i only code for fun and im still newb. Thanks in advance
fred
•
•
Join Date: Feb 2003
Location: London, England
Posts: 281
Reputation:
Rep Power: 7
Solved Threads: 6
Do u mean a mysql table?
Try something like this
Letters:
<a href="<?php echo $PHP_SELF; ?>?letter=a">A</a>
<a href="<?php echo $PHP_SELF; ?>?letter=b">B</a>
<a href="<?php echo $PHP_SELF; ?>?letter=c">C</a>
...
<a href="<?php echo $PHP_SELF; ?>?letter=z">Z</a> <br /><br />
[php]
<?php
// Connect to DB... blah blah blah
$db_host = 'localhost'; // Probably OK
$db_user = 'Bob'; // Change to your MySQL Username
$db_pass = 'bar'; // Change to your MySQL Password
$db_name = 'database'; // Change to your MySQL Database name
$db_connect = mysql_connect($db_host, $db_user, $db_pass);
$db = mysql_select_db($db_name);
// END DB Connection
// Find the letter=whatever stuff in the URL and assign it to
// the $starting_letter variable
$starting_letter = $_GET['letter'];
// Create + execute SQL
$sql = "SELECT * FROM clans
WHERE LEFT(name, 1) = '$starting_letter'";
$result = mysql_query($sql);
// While there are results, keep looping...
while( $row = mysql_fetch_assoc($result) )
{
// Echo the results...
echo 'Clan: <a href="' . $row['url'] . '">' . $row['name'] . '</a><br />';
echo '<img src="' . $row['logo'] . '" /><br />';
echo $row['description'] . '<br /><br />';
}
// End
?>
[/php]
Try something like this
Letters:
<a href="<?php echo $PHP_SELF; ?>?letter=a">A</a>
<a href="<?php echo $PHP_SELF; ?>?letter=b">B</a>
<a href="<?php echo $PHP_SELF; ?>?letter=c">C</a>
...
<a href="<?php echo $PHP_SELF; ?>?letter=z">Z</a> <br /><br />
[php]
<?php
// Connect to DB... blah blah blah
$db_host = 'localhost'; // Probably OK
$db_user = 'Bob'; // Change to your MySQL Username
$db_pass = 'bar'; // Change to your MySQL Password
$db_name = 'database'; // Change to your MySQL Database name
$db_connect = mysql_connect($db_host, $db_user, $db_pass);
$db = mysql_select_db($db_name);
// END DB Connection
// Find the letter=whatever stuff in the URL and assign it to
// the $starting_letter variable
$starting_letter = $_GET['letter'];
// Create + execute SQL
$sql = "SELECT * FROM clans
WHERE LEFT(name, 1) = '$starting_letter'";
$result = mysql_query($sql);
// While there are results, keep looping...
while( $row = mysql_fetch_assoc($result) )
{
// Echo the results...
echo 'Clan: <a href="' . $row['url'] . '">' . $row['name'] . '</a><br />';
echo '<img src="' . $row['logo'] . '" /><br />';
echo $row['description'] . '<br /><br />';
}
// End
?>
[/php]
•
•
Join Date: Feb 2003
Location: London, England
Posts: 281
Reputation:
Rep Power: 7
Solved Threads: 6
No probs.
Actually that isn't too secure - you may wish to change
$starting_letter = $_GET['letter'];
to
$starting_letter = addslashes($_GET['letter']);
Actually that isn't too secure - you may wish to change
$starting_letter = $_GET['letter'];
to
$starting_letter = addslashes($_GET['letter']);
•
•
Join Date: Feb 2002
Location: Long Island, NY
Posts: 1,134
Reputation:
Rep Power: 12
Solved Threads: 4
I have a function I use when I'm inserting snippets of code into a MySQL database:
If you don't replace \\ with \\\\ you're going to have problems with slashes when you insert them into MySQL. Just try inserting this code that highlights text into MySQL through an HTML form and see what I mean.
function mysql_safe_data($string) {
$string = stripslashes($string);
$string = str_replace("'", "''", $string);
$string = str_replace("\\", "\\\\", $string);
return $string;
} If you don't replace \\ with \\\\ you're going to have problems with slashes when you insert them into MySQL. Just try inserting this code that highlights text into MySQL through an HTML form and see what I mean.
function highlight($string, $words_to_highlight, $delimiter=" ", $case=0,
$left_string="<b span style=\"background-color: yellow;\">", $right_string="</b>") {
// This is so it highlights the first word. Each word in a textblock must be surrounded by
// [^A-Za-z] in order to highlight whole words, and not subwords.
/* Filtering Process:
Replace statements take out all malicious chars that would break the
search part of a regular expression. Takes unwanted chars and only lets the chars:
[^-a-zA-Z0-9&] in. */
$list_of_words = eregi_replace("[^-a-zA-Z0-9&']", " ", $words_to_highlight);
// This portion of code is to take out single word characters.
$list_array = explode(" ", $list_of_words);
for($i=0; $i<sizeof($list_array); $i++)
if(strlen($list_array[$i]) == 1)
$list_array[$i] = "";
$list_of_words = implode(" ", $list_array); // Use space as delimiter
$list_of_words = eregi_replace(" +", "|", $list_of_words);
// Make sure there aren't any pipes | around $list_of_words
if($list_of_words{0}=="|")
$list_of_words{0} = "";
if($list_of_words{strlen($list_of_words)-1}=="|")
$list_of_words{strlen($list_of_words)-1}="";
$list_of_words = "(".trim($list_of_words).")";
if($case==0)
return eregi_replace("$list_of_words", "$left_string"."\\1"."$right_string", $string);
else
return ereg_replace("$list_of_words", "$left_string"."\\1"."$right_string", $string);
} // end function highlight _.:: my websites ::._
blog @ www.samaru.net * engi No Jutsu @ www.narutorp.net * portfolio @ shinylight.com
deviantART: inscissor
blog @ www.samaru.net * engi No Jutsu @ www.narutorp.net * portfolio @ shinylight.com
deviantART: inscissor
•
•
Join Date: Feb 2003
Location: London, England
Posts: 281
Reputation:
Rep Power: 7
Solved Threads: 6
...what's wrong with addslashes()? It add slashes to already existing slashes
•
•
Join Date: Feb 2004
Posts: 11
Reputation:
Rep Power: 5
Solved Threads: 0
thanks alot guys! i really appreciate!
if you want to see the result go there: http://www.battlefield-vietnam.ca/clans.php

fred
if you want to see the result go there: http://www.battlefield-vietnam.ca/clans.php

fred
![]() |
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Clean Previous Next Script for MySQL results (PHP)
- The name for the Resize Button to ad to Script (HTML and CSS)
- Random Programming Script (Computer Science and Software Design)
- Importing SQL Script File - Urgent !! (Database Design)
- Script problems with IE (Web Browsers)
- Help with shell script to auto gzip a .sql dump after backup (Shell Scripting)
- Adding mutiple PC's to an OU via script? (Windows NT / 2000 / XP / 2003)
- Table of Contents Script (Java)
- ASP slow-down server script (ASP)
Other Threads in the PHP Forum
- Previous Thread: Not allowed to reply a post
- Next Thread: Need help with HTMl Table echoing?



Linear Mode