HELP! I'm having sorting problems.

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
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

HELP! I'm having sorting problems.

Post by gotDNS »

Hey guys...haven't written in awhile...but now i come crawling back, lol.

I have a poetry site at http://oversoul.mine.nu/~brian/poetry/ and I'm having trouble.

see, when you go to view your peoms, i have them just listed, but i want the list sorted NEWEST FIRST. It's impossible to like asort s title though.
Here's the code, see if u can help me out:

Code: Select all

<?php
session_start();

mysql_connect("localhost:3306", "poet") && mysql_select_db("poetry")
or $failed = "Could not connect to database.";

include "tprint.php";    //includes the top of the page

if(session_is_registered("loggedin"))   //check wether they're logged in
&#123;
	echo "<b>Total Poems:</b> ";
$listquery = "select count(1) from poems where user="$loggedin"";   // 
$listresult = mysql_query($listquery);                                // All of this connects to a DB table, etc
while ($row = mysql_fetch_assoc($listresult)) &#123;                       //
	$count = $row&#1111;"count(1)"];   //Counts the number of entries to be displayed
	echo $count;  //echo's that count
	if($count != "0") &#123; echo "<br /><br />"; &#125; //gives an error if there are no entries

$listquery = "select * from poems where user="$loggedin"";         //
$listresult = mysql_query($listquery);                               // Connects to another table without a count
while ($row = mysql_fetch_assoc($listresult)) &#123;                      //
	$title = $row&#1111;"title"];    //get the title of each poem in a loop
	$num = $row&#1111;"num"];    // get s the corrosponding number so that a link can be madfe with the GET method
	echo "<a href="read.php?num=$num">$title</a><br />";   //makes the link
&#125;
&#125;
else
&#123;
	echo "<b>You must be logged in to view your poems.</b>";  //error if ur not logged in
&#125;

include "bprint.php";

$prevurl = "mypoems.php";
session_register("prevurl");
?>

How can i reverse sort theses? Thanks SO much!
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

In your SQL:

SELECT field FROM table WHERE whatever = 'whatever' ORDER BY fieldName [DESC|ASC]

Look up ORDER BY in the MySQL manual.
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

Post by gotDNS »

Thanks Jason!
Post Reply