[PHP] Order problem!

Joined
Apr 19, 2006
Messages
396
Reaction score
27
Ok, im coding an announce sistem for a site... I made EVERYTHING work, but the order of how the announcements are shown...

this is the code:
PHP:
<?
$query = "Select id,message from announcement order by id DESC limit 5";
$command = mysql_query($query)
            or die($db_error);
            

print "<table width=\"270\" cellpadding=\"1px\" align=\"center\" border=\"1\" style=\"border-style:solid; border-width:thin; border-collapse:collapse;\" cellspacing=\"0px\">";

while($result = mysql_fetch_array($command))
{
print "<tr><td>".$result['message']."</td></tr>";
}
print "</table>";
print "<br /><br />";
?>

As you can see its limited to 5, and the order should be shown like this:
Code:
5
4
3
2
1

but, actually its being ordered like
Code:
1
2
3
4
5

And when i add the value 6 it must at least be like this
Code:
2
3
4
5
6

But somehow the order stays
Code:
1
2
3
4
5

I know someone made the same post... but i also checked the MySQL error he had...

Code:
`id` INT( 10  )  NOT  NULL ,
 `message` VARCHAR( 225  )  CHARACTER  SET armscii8 COLLATE armscii8_bin NOT  NULL

i placed id as INT, i tried to place as PRIMARY KEY and auto_increment, but each time i insert a new announcement it says duplicated primary key, even if the first announcement has id 100 and the second has id 876...

PLEASE! T_T HELP ME!
 
Try this:
PHP:
<?php

$sql = mysql_query("SELECT message FROM announcement ORDER BY id ASC LIMIT 5") or die(mysql_error());

echo '<table width="270" cellpadding="1px" align="center" border="1" style="border-style:solid; border-width:thin; border-collapse:collapse;" cellspacing="0px">\n';
while($result = mysql_fetch_array($sql)){
echo "	<tr>
		<td>".$result['message']."</td>
	</tr>";
}
echo "</table>";
echo "<br /><br />";

?>
 
I've got:

PHP:
<?php

$sql = mysql_query("SELECT `message` FROM `announcement` LIMIT 5") or die($db_error);
$r = 1; //STARTING NUMBER
//Change that according to what you want.. so if you want to nrs 2 - 6, start nr is 2. etc.

echo '<table width="270" cellpadding="1px" align="center" border="1" style="border-style:solid; border-width:thin; border-collapse:collapse;" cellspacing="0px">\n';
while($result = mysql_fetch_array($sql)){
echo "<tr>";
 echo "<td>". $r++ ."</td>"; //FROM STARTING NUMBER TO LIMIT 5
 echo "<td>".$result['message']."</td>";
echo "</tr>";
}
echo "</table>";
echo "<br /><br />";

?>
 
PHP:
<?php
$sql = mysql_query("SELECT `message` FROM `announcement` ORDER BY `id` ASC LIMIT 5") or die($db_error);
$r = 1; //STARTING NUMBER
//Change that according to what you want.. so if you want to nrs 2 - 6, start nr is 2. etc.

echo '<table width="270" cellpadding="1px" align="center" border="1" style="border-style:solid; border-width:thin; border-collapse:collapse;" cellspacing="0px">\n';
while($result = mysql_fetch_array($sql)){
echo "<tr>";
 echo "<td>". $r++ ."</td>"; //FROM STARTING NUMBER TO LIMIT 5
 echo "<td>".$result['message']."</td>";
echo "</tr>";
}
echo "</table>";
echo "<br /><br />";
?>
 
PHP:
<?php

$sql = mysql_query("SELECT `message` FROM `announcement` LIMIT 5") or die($db_error);
$r = 1; //STARTING NUMBER
//Change that according to what you want.. so if you want to nrs 2 - 6, start nr is 2. etc.

echo '<table width="270" cellpadding="1px" align="center" border="1" style="border-style:solid; border-width:thin; border-collapse:collapse;" cellspacing="0px">\n';
while($result = mysql_fetch_array($sql)){
echo "<tr>";
$r++; //FROM STARTING NUMBER TO LIMIT 5
 echo "<td>".$result['message']."</td>";
echo "</tr>";
}
echo "</table>";
echo "<br /><br />";
?>

You don't HAVE to echo $r
 
You should use better namings for your variables then, these confusions/mistakes take much more time then properly naming the variables to something usefull (so after 6 weeks you still know what happens!).

Also, it would be easier to use single-quotes for the first <table> definition, then you don't have to escape the double-quotes in HTML, and single-quote is generally considered to be a bit faster.
You also should use <?php instead of <?, and I would prefer indention and capital characters for keywords in SQL.

PHP:
<?php
$result = mysql_query("SELECT id, message FROM announcement ORDER BY id DESC LIMIT 5") or die($db_error);            

print('<table width="270" cellpadding="1px" align="center" border="1" style="border-style:solid; border-width:thin; border-collapse:collapse;" cellspacing="0px">');
while ($result = mysql_fetch_array($result))
{
    print('<tr><td>'.$result['message'].'</td></tr>';
}
print('</table><br /><br />');
?>

Oh and s-p-n, your signature makes no sense ^^, if one does not call mysql_close(), it will be called automatically at the end of the script. It's only used for limiting the RAM usage (for example, you can dismiss MySQL in the middle of the script, the script after mysql_close() will then have a little more RAM available).
 
..... Oh and s-p-n, your signature makes no sense ^^, if one does not call mysql_close(), it will be called automatically at the end of the script. It's only used for limiting the RAM usage (for example, you can dismiss MySQL in the middle of the script, the script after mysql_close() will then have a little more RAM available).


Well nobody wants a build-up of RAM, right? *coughs* :smilie_xp *coughs*

Lol.. No, but really, mysql_close() helps a lot when going through code trying to reduce a page's load-time. It probably helps more then using apostrophies instead of quotes. But I'm just going by knowledge of something I read on another PHP forum so.. could be wrong ;)
 
Well nobody wants a build-up of RAM, right? *coughs* :smilie_xp *coughs*

Lol.. No, but really, mysql_close() helps a lot when going through code trying to reduce a page's load-time. It probably helps more then using apostrophies instead of quotes. But I'm just going by knowledge of something I read on another PHP forum so.. could be wrong ;)

Oh yes, it does help in reducing the RAM usage ofcourse, but mostly I have queries till the end of page ;).

In that case, you should also consider to only connect to MySQL when needed, ie. not at the top of the page (often).
 
Back