Jump to content

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

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.