Jump to content

Archived

This topic is now archived and is closed to further replies.

hostfreak

Ordering help

Recommended Posts

I am wondering how I would order information from a mysql field by the first letter of the name in the field "company". Then depending on what the letter is, have it on it's own page "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z" ?

Share this post


Link to post
Share on other sites
I'd do the following code with a loop

[code]SELECT * FROM `table` WHERE `company` LIKE 'A%' ORDER BY `company`[/code]

Just for fun, here's how I'd get started. It should print the A B C D E... thing and add hyperlinks to those that have values. It should also detect what page you're on and create an array for that letter.

[code]
if(!$_GET['letter']) $_GET['letter']="a"; // set the default page to "a"
for($i=65;$i<92;$i++) {
   $result=mysql_query("SELECT * FROM `table` WHERE `company` LIKE '".chr ($i)."%' ORDER BY `company`");
   if($_GET['letter']==chr ($i)) $myrow=mysql_fetch_assoc($result);
   echo " ";
   if (mysql_num_rows($result)>0) echo "<a href=\"thispage.php?letter=".chr ($i)."\">";
   echo strtoupper(chr ($i));
   if (mysql_num_rows($result)>0) echo "</a>";
   echo " ";
// note -- due to a forum bug, remove the spaces between chr and () in the above code.
[/code]

Share this post


Link to post
Share on other sites
use this query

[b][!--coloro:blue--][span style=\"color:blue\"][!--/coloro--]select * from table order by company[!--colorc--][/span][!--/colorc--][/b]

to get it to list alphabetically. now as far as the 2nd part of your question, be more specific. Do you mean like, you wish to have A-Z printed and you click on a letter and it takes you to a new page that list the company names of that letter? if so, then your query would be more like this:

$letter = 'A'; // example letter
[b][!--coloro:blue--][span style=\"color:blue\"][!--/coloro--]select * from table where company like '$letter%' order by company[!--colorc--][/span][!--/colorc--][/b]

but it would be like in the target php script and you would make dynamic links for each letter of the alphabet that passes $letter to the target page.

if that is not what you mean, then be more specific.

Share this post


Link to post
Share on other sites
[!--quoteo(post=376117:date=May 22 2006, 03:43 PM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ May 22 2006, 03:43 PM) [snapback]376117[/snapback][/div][div class=\'quotemain\'][!--quotec--]
use this query

[b][!--coloro:blue--][span style=\"color:blue\"][!--/coloro--]select * from table order by company[!--colorc--][/span][!--/colorc--][/b]

to get it to list alphabetically. now as far as the 2nd part of your question, be more specific. Do you mean like, you wish to have A-Z printed and you click on a letter and it takes you to a new page that list the company names of that letter? if so, then your query would be more like this:

$letter = 'A'; // example letter
[b][!--coloro:blue--][span style=\"color:blue\"][!--/coloro--]select * from table where company like '$letter%' order by company[!--colorc--][/span][!--/colorc--][/b]

but it would be like in the target php script and you would make dynamic links for each letter of the alphabet that passes $letter to the target page.

if that is not what you mean, then be more specific.
[/quote]


Spot on with my quesrtion. I am sorry that I wasn't more specific. What I want to do is have the letters a-z as links and on the page of the letter clicked, it would show the names in "company" by that letter. I just wasn't sure how to call it: '$letter%' . Thanks again guys. You both helped me.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.