Jump to content

Wiki-style Listing


unknown gamer
 Share

Recommended Posts

Alright, what I want to do is take a list of mysql results and order them by letter into a list like:AappleBBananaboatbag...I'm not exactly sure how to do this but I did find an example that is either out of date or I am just using it incorrectly.This is what I found:http://www.webmasterworld.com/php/3607632.htm (3rd post from the bottom)I don't exactly understand the code and how the sql select code works with the count() and group by works.So can someone either explain what i'd have to change to make that code work with the row "name" (without quotes) or make a new code that would work.-Thanks in advance

Edited by unknowngamer
Link to comment
Share on other sites

What they're doing with SQL isn't exactly what you wanted. You can check the SQL Tutorial to get some more information on COUNT() and GROUP BY.I'd just use ORDER BY, and then loop through the results in PHP checking when the first letter changes so that I could start a new section.

Link to comment
Share on other sites

I'm not sure what I would do to check the first letter of every name. Aswell as create a new letter when the first letter changes.
You can check the first letter by using the substr() function.You initialize a variable $first_letter as an empty string, and on each iteration, check if $first_letter is equal to the first letter of the field you're testing, if it is not, then start a new group with the letter and assign that letter to $first_letter.To put it in code, something like this:This example makes use of strtoupper() and strlen()
$first_letter = '';while($row = mysql_fetch_assoc($q)) {  // Check if the first letter is different than in previous rows  if( $first_letter != strtoupper(substr($row['my_field'],0,1)) ) {	// Only close a list if one is already open	if( strlen($first_letter) ) echo '</ul>';	// Print a new title for the letter and open a new list	echo "<h4>{$first_letter}</h4>";	echo '<ul>';	$first_letter = strtoupper(substr($row['my_field'],0,1));  }  // Print the row value and whatever else you want  echo "<li>{$row['my_field']}</li>";}// Only close a list if one is already open.if( strlen($first_letter) ) echo '</ul>';

Link to comment
Share on other sites

That works but it seems as if it goes like this:AppleApricotABananaBoatBinstead of:AAppleApricotBBananaBoatI've tried moving things around but it just didn't work for me.Aswell I also wanted all the letters to be placed, like this:

<link rel="stylesheet" type="text/css" href="style.css"><?phpfor ($i=65; $i<=90; $i++)echo "<a href='#".chr($i)."'>".chr($i)." | </a>";echo "<a href='#0-9'>0-9 | </a>";for ($i=65; $i<=90; $i++)echo "<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><a NAME='".chr($i)."'>".chr($i)."</a>";echo "<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><a NAME='0-9'>0-9</a>";?>

Edited by unknowngamer
Link to comment
Share on other sites

It should be ordering them correctly. You probably modified the code I gave you. Show me what you have.This code isn't correct:

<link rel="stylesheet" type="text/css" href="style.css"><?phpfor ($i=65; $i<=90; $i++)echo "<a href='#".chr($i)."'>".chr($i)." | </a>";echo "<a href='#0-9'>0-9 | </a>";for ($i=65; $i<=90; $i++)echo "<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><a NAME='".chr($i)."'>".chr($i)."</a>";echo "<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><a NAME='0-9'>0-9</a>";?>

<a> and <br> tags can't go in the <head> of the page, and <link> isn't supposed to go in the <body>. The name attribute is deprecated for all elements except form fields.You should use braces for your blocks of code, and indent it. That code probably is not structured well and it's difficult to know because you haven't indented it and you've omitted braces.

Link to comment
Share on other sites

<?phpinclude 'connect.php';$q = mysql_query("SELECT name FROM I3 ORDER BY name");$first_letter = '';while($row = mysql_fetch_assoc($q)) {  // Check if the first letter is different than in previous rows  if( $first_letter != strtoupper(substr($row['name'],0,1)) ) {	// Only close a list if one is already open	if( strlen($first_letter) ) echo '</ul>';	// Print a new title for the letter and open a new list	echo "<h4>{$first_letter}</h4>";	echo '<ul>';	$first_letter = strtoupper(substr($row['name'],0,1));  }  // Print the row value and whatever else you want echo "<li>{$row['name']}</li>";}// Only close a list if one is already open.if( strlen($first_letter) ) echo '</ul>';?>

Is exactly what I have.

Link to comment
Share on other sites

I see now where my mistake was. Put the lines in this order:

	// Print a new title for the letter and open a new list	$first_letter = strtoupper(substr($row['name'],0,1));	echo "<h4>{$first_letter}</h4>";	echo '<ul>';

Link to comment
Share on other sites

Rather than printing all of the things, you'd have to go putting the values into a string, store it in a variable and go adding the letters to an array.After that you can use the array of letters first and then print the string with the rest of the code.

Link to comment
Share on other sites

this works for me$result = mysql_query("SELECT name FROM table ORDER BY name" ,$db); $current=""; while ($myrow = mysql_fetch_array($result)) { if (strtolower(substr($myrow["name"],0,1)) <> $current) { echo "<h3 style=\"text-decoration: underline;\">".strtoupper(substr($myrow["name"],0,1))."</h3>";}echo $myrow["name"]."<br />";$current = strtolower(substr($myrow["name"],0,1));}

Link to comment
Share on other sites

Rather than printing all of the things, you'd have to go putting the values into a string, store it in a variable and go adding the letters to an array.After that you can use the array of letters first and then print the string with the rest of the code.
I don't really know how to do that.
Link to comment
Share on other sites

I don't really know how to do that.
You are just waiting for people to give you the code, without understanding it.I don't have a dictionary with all the algorithms written down, the code I write is code that I created. You must learn how to solve problems, it is a required characteristic for programmers. It only takes a little bit of logic and basic programming knowledge.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...