Ordering rows

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

Post Reply
webdivany
Forum Newbie
Posts: 2
Joined: Fri Jun 17, 2005 8:55 am

Ordering rows

Post by webdivany »

Hi, I'm having a problem ordering rows. The URL to the site is: http://www.glenridgeseniorcenter.com/main.php
On the side there's a box that says "Upcoming Events >"
It is pulling the table and displaying only the rows that have the month as the current month. However I want it to do that as well as ordering it, because right now the days are out of order.

The table name is:
event_2

The fields are:
month
day
event

This is the code I have right now:

Code: Select all

<?
$username="";
$password="";
$database="";

mysql_connect(localhost,$username,$password); 
@mysql_select_db($database) or die( "Unable to select database"); 
$result=mysql_query("select * from event_2 where month=".  

date("n")); 
for($i=1;$i<=mysql_num_rows($result);$i++){ 
                        $row=mysql_fetch_array($result); 
?> 
<b><? echo $row['month'] ?>/<? echo $row['day'] ?></b> - <? echo $row['event'] ?><br> 
     <? 
        } 
        ?>

I need help in ordering the rows using 'day'

I can't seem to get it. I tried ORDER by day
and still nothing, I get errors.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

what does your database table look like?
webdivany
Forum Newbie
Posts: 2
Joined: Fri Jun 17, 2005 8:55 am

Post by webdivany »

--
-- Table structure for table `event_2`
--

CREATE TABLE `event_2` (
`month` char(2) NOT NULL default '',
`day` char(2) NOT NULL default '',
`event` varchar(100) NOT NULL default ''
) TYPE=MyISAM;
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

$result=mysql_query("SELECT * FROM `event_2` WHERE `month` = '".date("n")."' ORDER BY `day` DESC");
Last edited by John Cartwright on Sat Jun 18, 2005 2:43 pm, edited 1 time in total.
dreamline
Forum Contributor
Posts: 158
Joined: Fri May 28, 2004 2:37 am

Post by dreamline »

I'd use something like:
order by day desc,'dd-mm-yyyy' (for sorting highest date first)
order by day asc,'dd-mm-yyyy' (for sorting lowest date first)

There are more swithes used for the date format but those are all in the php manual, don't know em all by heart.. lol

:)
Post Reply