PHP table order from MYSQL

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
Suraski
Forum Newbie
Posts: 10
Joined: Tue Jul 10, 2007 10:30 pm

PHP table order from MYSQL

Post by Suraski »

I want to view the tables in descending order, how do I do that here?

http://www.bensphotosite.com/home/index.php

Here's my code

Code: Select all

mysql_select_db("news", $con);
$result = mysql_query("SELECT * FROM news");
while($row = mysql_fetch_array($result))
//if($row['cat'] = News)
{
if ( $row['cat'] == News){
echo "				<table bgcolor=\"#F0F1FF\" width=\"500\" height=\"100\" cellpadding=\"0\" cellspacing=\"0\" style=\"border: 1px solid #B3C0FF; margin: 10px; padding: 0;\" >
					<tr>
          				<td valign=\"top\" height=\"15\" style=\"padding: 3px 3px 3px 5px ; border-bottom: 1px solid #B3C0FF\">
							<d1head> News: &nbsp;" . $row['title'] . "</d1head>
		  				</td>
					</tr>
					<tr>
						<td valign=\"top\" height=\"20\" style=\"padding: 10px\"
							<t2>" . $row['news'] . "</t2><t2><br /> 
							<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $row['name'] . "</t2>										
						</td>
					</tr>
					<tr>
						<td valign=\"top\" height=\"20\" style=\"border-top: 1px solid #B3C0FF\">
							<t2>" . $row['date'] . "</t2>						
						</td>
					</tr>
				</table>
		";
}
else{
echo "				<table bgcolor=\"#F0F1FF\" width=\"500\" height=\"100\" cellpadding=\"0\" cellspacing=\"0\" style=\"border: 1px solid #B3C0FF; margin: 10px; padding: 0;\" >
					<tr>
          				<td valign=\"top\" height=\"15\" style=\"padding: 3px 3px 3px 5px ; border-bottom: 1px solid #B3C0FF\">
							<d1head> Blog: &nbsp;" . $row['title'] . "</d1head>
		  				</td>
					</tr>
					<tr>
						<td valign=\"top\" height=\"20\" style=\"padding: 10px\"
							<t2>" . $row['news'] . "</t2><t2><br /> 
							<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $row['name'] . "</t2>										
						</td>
					</tr>
					<tr>
						<td valign=\"top\" height=\"20\" style=\"border-top: 1px solid #B3C0FF\">
							<t2>" . $row['date'] . "</t2>						
						</td>
					</tr>
				</table>
		";
}
}
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

Post by mcccy005 »

Code: Select all

//This snippet taken from http://www.webcheatsheet.com/php/get_cu ... ge_url.php
function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}

$current_url=curPageURL( );

if(isset($_GET['order']))
{
     if($_GET['order']=='ascending') $order='descending';
     else $order='ASC';
}
else $order='ASC';

$sql_query="SELECT * FROM NEWS ORDER BY <<COLUMN NAME YOU WANT TO SORT BY>>".$order;

//RETRIEVE DATA HERE ETC ETC ETC.

echo "<table>
          <tr><td><a href=".$current_url."?order=".$order.">TITLE OF COLUMN TO SORT</a></td></tr>
         <tr><td>CONTENTS OF COLUMN OF TABLES RETRIEVED FROM DATABASE...</td></tr>
        </table>"
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: PHP table order from MYSQL

Post by Zoxive »

Code: Select all

mysql_select_db("news", $con);
$result = mysql_query("SELECT * FROM news ORDER BY `id` DESC"); // ORDER BY (COLUMN) (ASC|DESC)
//////////////
mcccy005 I'm pretty sure that the poster doesn't want to change the order dynamically.. anyways
and your code wouldn't even change the order to "DESC", it would always stay "ASC", unless you manually change the url to "ascending" and then it would change it to "descending" which isn't even correct.
Suraski
Forum Newbie
Posts: 10
Joined: Tue Jul 10, 2007 10:30 pm

Post by Suraski »

ok, that's still not working, I don't know if it's my code or not.
Post Reply