Page 1 of 1

PHP table order from MYSQL

Posted: Tue Jul 10, 2007 10:37 pm
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>
		";
}
}

Posted: Tue Jul 10, 2007 11:17 pm
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>"

Re: PHP table order from MYSQL

Posted: Wed Jul 11, 2007 12:24 am
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.

Posted: Wed Jul 11, 2007 8:17 pm
by Suraski
ok, that's still not working, I don't know if it's my code or not.