alphabetize my listings

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

studio29
Forum Newbie
Posts: 12
Joined: Mon Dec 29, 2008 10:37 am

alphabetize my listings

Post by studio29 »

I am having problems sorting the names that are being pulled from the database in alphabetical order. I have tried several things but nothing is working. Here is the code.
<?php

require ("config.php");
$row = mysql_query ("SELECT * FROM `locations` where Type <> 0");
$count =0 ;
echo("<tr>");
while ($print = mysql_fetch_object($row)) {
if($count == 0) echo("<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>");
echo ("<td class='text'><b>". $print->Name . "</b></td>");
$count++;
if($count == 5 )
{
echo("</tr><tr>");
$count = 0;
}
}
if($count != 0)
echo("</tr>");

?>
<tr><td>&nbsp;</td></tr>
<tr><td>&nbsp;</td></tr>
<tr><td>&nbsp;</td></tr>
</table>
</td>
</tr>
<tr>

can anyone help? What is the piece of code that is missing?
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: alphabetize my listings

Post by jaoudestudios »

What have you tried?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: alphabetize my listings

Post by califdon »

Since you did not use an ORDER BY clause in your query, what is it that you thought would cause the records to be in alphabetic sequence?
studio29
Forum Newbie
Posts: 12
Joined: Mon Dec 29, 2008 10:37 am

Re: alphabetize my listings

Post by studio29 »

I tried the order by statement and it caused the code not to appear at all, could someone show me the appropriate place to put this piece of code?

and what I tried WAS the order by, but it would not work, maybe i am putting it in the wrong place.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: alphabetize my listings

Post by jaoudestudios »

post the code you tried and we will show you want is wrong!
studio29
Forum Newbie
Posts: 12
Joined: Mon Dec 29, 2008 10:37 am

Re: alphabetize my listings

Post by studio29 »

<?php

require ("config.php");
$row = mysql_query ("SELECT * FROM `locations` where Type <> 0 ORDER BY ASC");
$count =0 ;
echo("<tr>");
while ($print = mysql_fetch_object($row)) {
if($count == 0) echo("<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>");
echo ("<td class='text'><b>". $print->Name . "</b></td>");
$count++;
if($count == 5 )
{
echo("</tr><tr>");
$count = 0;
}
}
if($count != 0)
echo("</tr>");

?>
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: alphabetize my listings

Post by jaoudestudios »

If you had done a google search for 'mysql order' the first result is the one you want!!!
I'll even post it here for you :)
http://dev.mysql.com/doc/refman/5.0/en/ ... ation.html

Code: Select all

 
SELECT * FROM t1
  WHERE key_part1=1 
  ORDER BY key_part1 DESC;
 
 
studio29
Forum Newbie
Posts: 12
Joined: Mon Dec 29, 2008 10:37 am

Re: alphabetize my listings

Post by studio29 »

is this alphabetically order from a to z or z to a? i get confused with asc and desc
studio29
Forum Newbie
Posts: 12
Joined: Mon Dec 29, 2008 10:37 am

Re: alphabetize my listings

Post by studio29 »

so in this case, would the correct code be

<?php

require ("config.php");
$row = mysql_query ("SELECT * FROM `locations` where Type <> 0 ORDER BY Name ASC");
$count =0 ;
echo("<tr>");
while ($print = mysql_fetch_object($row)) {
if($count == 0) echo("<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>");
echo ("<td class='text'><b>". $print->Name . "</b></td>");
$count++;
if($count == 5 )
{
echo("</tr><tr>");
$count = 0;
}
}
if($count != 0)
echo("</tr>");

?>
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: alphabetize my listings

Post by onion2k »

Run it and see.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: alphabetize my listings

Post by jaoudestudios »

studio29 wrote:is this alphabetically order from a to z or z to a? i get confused with asc and desc
Come on, it probably easier to try both than to type the question here!
studio29 wrote:so in this case, would the correct code be

SELECT * FROM `locations` where Type <> 0 ORDER BY Name ASC
That looks ok, did it work?
studio29
Forum Newbie
Posts: 12
Joined: Mon Dec 29, 2008 10:37 am

Re: alphabetize my listings

Post by studio29 »

gosh im trying it, give me a sec lol. You guys are a bit pushy :)
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: alphabetize my listings

Post by jaoudestudios »

studio29 wrote:gosh im trying it, give me a sec lol. You guys are a bit pushy :)
If you want to play that card...and we are all being honest here...you are a bit lazy! I dont want to alienate you or discourage you from coming back. But you ask simple questions that would be easier to try it than to post the question itself. We are here helping you, giving you are knowledge & time for FREE! The least you can do is try to help yourself!

I have a better idea, post your brief and code so far, and I will write the entire thing for you :P
studio29
Forum Newbie
Posts: 12
Joined: Mon Dec 29, 2008 10:37 am

Re: alphabetize my listings

Post by studio29 »

it worked to a certain degree, they are in alphabetical order, except for the one i entered in the other day -

http://www.gatewaymortgagesml.com/servicesareas.php

any thoughts?
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: alphabetize my listings

Post by jaoudestudios »

How I Help Here: I will put in an equal amount of effort to the amount of effort you put in. If it's obvious you're not bothered about your problem (eg you don't post any code, you've not got any error messages) then I won't bother helping very much. If you make it clear you care and you're trying your best (eg you post code in

Code: Select all

tags, you've tried a few different fixes already) I'll do my best to help you.[/quote]
This is from [color=#00BF00]onion2k [/color]'s signature...read it, learn it, we all think it!!!
Post Reply