Jump to content


Photo

Ordering help


  • Please log in to reply
3 replies to this topic

#1 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 22 May 2006 - 07:22 PM

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" ?

#2 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 22 May 2006 - 07:43 PM

I'd do the following code with a loop

SELECT * FROM `table` WHERE `company` LIKE 'A%' ORDER BY `company`

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.

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.


#3 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 22 May 2006 - 07:43 PM

use this query

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

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
[!--coloro:blue--][span style=\"color:blue\"][!--/coloro--]select * from table where company like '$letter%' order by company[!--colorc--][/span][!--/colorc--]

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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#4 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 23 May 2006 - 01:54 AM

[!--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) View Post[/div][div class=\'quotemain\'][!--quotec--]
use this query

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

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
[!--coloro:blue--][span style=\"color:blue\"][!--/coloro--]select * from table where company like '$letter%' order by company[!--colorc--][/span][!--/colorc--]

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users