RE: [PHP] reverse array for mysql data

From: Date: Wed, 02 May 2001 00:24:05 +0000
Subject: RE: [PHP] reverse array for mysql data
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-51057@lists.php.net to get a copy of this message
You're looking in the wrong place. What you want to do should be handled at the database level. I.e. your SQL query should be something like: select title,body from junkyard order by item_number desc, limit 7 Since you refer to what you want as the 'top 7' entries, there is presumably some sort of order that you are attempting to enforce in what you display. The way you currently have your query formed, the results you get back are not guaranteed to have any order. You must add the order by clause. The next problem you're likely to have, is that you don't have any way of enforcing order in your database other than the item number, which is probably not what you want. In this case, what is it that determines whether something is in the 'top 7'? Is it based on time? Is it alphabetical? Whatever it is, you need to sort by the corresponding field. My guess is that you're going to want to add a timestamp column to your database, and order by that, descending (i.e. highest to lowest). Once you do this, you don't need to change your php code at all which executes the query and displays the results. Good luck. Al > -----Original Message----- > From: > php-general-return-51044-adhir=govshops.com@lists.php.net > [mailto:php-general-return-51044-adhir=govshops.com@lists.php. > net] On Behalf Of ktb > Sent: Monday, April 30, 2001 2:52 PM > To: php-general@lists.php.net > Subject: [PHP] reverse array for mysql data > > > I'm new at programing and not getting very far with this. > I've looked at "array_revers" and "array_multisort" and can't > get anything to work. What I have is a mysql table laid out like: > > item number | title | body text | subject heading > > What I want to do is pull the top 7 entries off and display > them on a web page. I have written some code that will do > this but when it prints to the web page the first item is > printed at the top and I would like it on the bottom with > later entries successively on top. How can I do this? Hear > is the code I have so far: > > $result = mysql_query ("SELECT title, body FROM junkyard > LIMIT 0, 7"); $i = '0'; > > while ($i < 7) { > if ($row = mysql_fetch_array ($result)) { > echo "$row[0] <br />$row[1]"; > ++$i; > } > } > > Thanks, > kent > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: > php-general-help@lists.php.net To contact the list > administrators, e-mail: php-list-admin@lists.php.net > >

« previous php.general (#51057) next »