Page 1 of 2

alphabetize my listings

Posted: Mon Dec 29, 2008 10:40 am
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?

Re: alphabetize my listings

Posted: Mon Dec 29, 2008 11:25 am
by jaoudestudios
What have you tried?

Re: alphabetize my listings

Posted: Mon Dec 29, 2008 1:09 pm
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?

Re: alphabetize my listings

Posted: Tue Dec 30, 2008 8:38 am
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.

Re: alphabetize my listings

Posted: Tue Dec 30, 2008 8:44 am
by jaoudestudios
post the code you tried and we will show you want is wrong!

Re: alphabetize my listings

Posted: Tue Dec 30, 2008 8:52 am
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>");

?>

Re: alphabetize my listings

Posted: Tue Dec 30, 2008 8:55 am
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;
 
 

Re: alphabetize my listings

Posted: Tue Dec 30, 2008 9:07 am
by studio29
is this alphabetically order from a to z or z to a? i get confused with asc and desc

Re: alphabetize my listings

Posted: Tue Dec 30, 2008 9:08 am
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>");

?>

Re: alphabetize my listings

Posted: Tue Dec 30, 2008 9:12 am
by onion2k
Run it and see.

Re: alphabetize my listings

Posted: Tue Dec 30, 2008 9:13 am
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?

Re: alphabetize my listings

Posted: Tue Dec 30, 2008 9:15 am
by studio29
gosh im trying it, give me a sec lol. You guys are a bit pushy :)

Re: alphabetize my listings

Posted: Tue Dec 30, 2008 9:20 am
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

Re: alphabetize my listings

Posted: Tue Dec 30, 2008 9:21 am
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?

Re: alphabetize my listings

Posted: Tue Dec 30, 2008 9:21 am
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!!!