Sorting mySQL entries (posts) after output

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
drewrockshard
Forum Commoner
Posts: 37
Joined: Sat May 29, 2004 6:07 pm
Location: Dallas, Texas
Contact:

Sorting mySQL entries (posts) after output

Post by drewrockshard »

I am currently writing a program. Now this program spits out posts in a crude CSS styled 1px border box for each post that is outputed.

The problem is, I can't really think of how I would sort each post by date.

Because of this part of the final code:

Code: Select all

$row = mysql_numrows($result);
			$mysqldate = $row[0];
			$cat = $row[1];
			$daily = $row[2];
		$i=0;
It is now an integer data type, therfore using the sort() function will not work (where I understand that this function can only be used with arrays).

Here is the final code for output:

Code: Select all

<?php
// dailies.php
	include("db.php"); 
	dbConnect("dailies");
	
	echo "<h1>Dailies</h1>";
	function showDailies() {
		$query  = "SELECT * FROM dailies";
		$result = mysql_query($query);
		$row = mysql_numrows($result);
			$mysqldate = $row[0];
			$cat = $row[1];
			$daily = $row[2];
		$i=0;
		while ($i < $row) {
			$mysqldate = mysql_result($result,$i,"date");
			$date = date("F dS, Y", strtotime($mysqldate));
			$cat = mysql_result($result,$i,"category");
			$daily = mysql_result($result,$i,"daily");
			echo "<div id='daily' style='border:1px #000 solid; margin-bottom:5px; padding:10px;'>";
			echo "<h3>Posted on " . $date . " in " . $cat . "</h3>";
			echo "<p>" . $daily . "</p>";
			echo "</div>";
			$i++;
		}
	}
?>
<html>
<body>
	<?php showDailies(); ?>
</body>
</html>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

use an ORDER BY clause.
drewrockshard
Forum Commoner
Posts: 37
Joined: Sat May 29, 2004 6:07 pm
Location: Dallas, Texas
Contact:

Post by drewrockshard »

Thanks alot :) As usual, simple fix to a big problem. Worked great.
Post Reply