Jump to content

Sorting a table with PHP


rogueit

Recommended Posts

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?

 

Link to comment
https://forums.phpfreaks.com/topic/243007-sorting-a-table-with-php/
Share on other sites

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

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.