rogueit Posted July 27, 2011 Share Posted July 27, 2011 I found this code but when I run it I get several \n followed by Count item Name max(ts) then several a href="/testing.php?sort=max(ts)>"2011-07-08 00:00:00 <?PHP //connect info $hostname = "localhost"; $database = "db"; $username = "user"; $password = "password"; $conn = mysql_pconnect($hostname, $username, $password); $page = $_SERVER['PHP_SELF']; // Makes $page the current page e.g:query.php //the query if (!isset($sort)){ $sort = COUNT(item); // the one to sort by default } mysql_select_db($database, $conn); $query = "SELECT COUNT(item) Count_of_item, item, name, max(ts) FROM highs WHERE ts > DATE_SUB( now( ) , INTERVAL 30 DAY ) GROUP BY item"; $sql = mysql_query($query, $conn) or die(mysql_error()); //print the table echo '<table><tr><td>Count_of_item</td><td>item</td><td>Name</td><td>Date</td></tr>'; while($row = mysql_fetch_assoc($sql)){ //echo the results echo '<tr><td><a href="'.$page.'?sort=COUNT(item)>"'.$row['COUNT(item)'].'</a></td><td><a href="'.$page.'?sort=item>"'.$row['item'].'</a></td><td>a href="'.$page.'?sort=max(ts)>"'.$row['max(ts)'].'</a></td></tr>\n'; } //close the table echo '</table>'; ?> can you tell me what I am doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/243007-sorting-a-table-with-php/ Share on other sites More sharing options...
xyph Posted July 27, 2011 Share Posted July 27, 2011 Shortcuts such as \n won't be parsed in single quotes. Use double quotes to turn \n into a line break. I'd love to help further, and will if you show an understanding of the code you've provided. I'm not here to fix copy and pasted code. Quote Link to comment https://forums.phpfreaks.com/topic/243007-sorting-a-table-with-php/#findComment-1248128 Share on other sites More sharing options...
rogueit Posted July 27, 2011 Author Share Posted July 27, 2011 I am not saying that I know anything about PHP, that is why I came here for help. I have commented everything that I know about the snippet to show you that I have an "understanding of the code you've provided" I am not asking you to write the code for me, and I appreciate the "Shortcuts such as \n won't be parsed in single quotes. Use double quotes to turn \n into a line break." hint. I would be greatful if you see anything else obviously wrong with the code, specifically the page sort information. <?PHP // begins php code //connection info //db server name $hostname = "localhost"; //database located on server $database = "db"; //username used to login to server $username = "user"; //password used for authentication $password = "password"; //connection string passed to host $conn = mysql_pconnect($hostname, $username, $password); // Makes $page the current page e.g:query.php $page = $_SERVER['PHP_SELF']; //the query if (!isset($sort)){ // the one to sort by default $sort = COUNT(item); } mysql_select_db($database, $conn); //initiaizing connection //SQL statement to fill table with $query = "SELECT COUNT(item) Count_of_item, item, name, max(ts) FROM highs WHERE ts > DATE_SUB( now( ) , INTERVAL 30 DAY ) GROUP BY item"; //error control $sql = mysql_query($query, $conn) or die(mysql_error()); //print the table headers echo '<table><tr><td>Count_of_item</td><td>item</td><td>Name</td><td>Date</td></tr>'; //array loop while($row = mysql_fetch_assoc($sql)){ //echo the results inside table echo '<tr><td><a href="'.$page.'?sort=COUNT(item)>"'.$row['COUNT(item)'].'</a></td><td><a href="'.$page.'?sort=item>"'.$row['item'].'</a></td><td>a href="'.$page.'?sort=max(ts)>"'.$row['max(ts)'].'</a></td></tr>"\n"'; } //close the table echo '</table>'; //end of code ?> Quote Link to comment https://forums.phpfreaks.com/topic/243007-sorting-a-table-with-php/#findComment-1248151 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.