|
-
Aug 2nd, 2007, 06:12 PM
#1
Thread Starter
Member
order by links
Hi, im doing a ORDER BY php tutorial. I've followed every step carefully but it still won't work. I've copied the code from the tutorial and replaced all necessary parts but still it wont work. Someone that could point out the error?
PHP Code:
<?php
/* set the allowed order by columns */
$default_sort = 'id';
$allowed_order = array ('id', 'titel','genre', 'bolag');
/* if order is not set, or it is not in the allowed
* list, then set it to a default value. Otherwise,
* set it to what was passed in. */
if (!isset ($_GET['order']) ||
!in_array ($_GET['order'], $allowed_order)) {
$order = $default_sort;
} else {
$order = $_GET['order'];
}
/* connect to db */
mysql_connect ('xxxxxxx','xxxxxxxxx','xxxxxxx');
mysql_select_db ('xxxxxxxxx');
/* construct and run our query */
$query = "SELECT * FROM filmer ORDER BY $order";
$result = mysql_query ($query);
/* make sure data was retrieved */
$numrows = mysql_num_rows($result);
if ($numrows == 0) {
echo "No data to display!";
exit;
}
/* now grab the first row and start the table */
$row = mysql_fetch_assoc ($result);
echo "<TABLE border=1>\n";
echo "<TR>\n";
foreach ($row as $heading=>$column) {
/* check if the heading is in our allowed_order
* array. If it is, hyperlink it so that we can
* order by this column */
echo "<TD><b>";
if (in_array ($heading, $allowed_order)) {
echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading\">$heading</a>";
} else {
echo $heading;
}
echo "</b></TD>\n";
}
echo "</TR>\n";
/* reset the $result set back to the first row and
* display the data */
mysql_data_seek ($result, 0);
while ($row = mysql_fetch_assoc ($result)) {
echo "<TR>\n";
foreach ($row as $column) {
echo "<TD>$column</TD>\n";
}
echo "</TR>\n";
}
echo "</TABLE>\n";
?>
Last edited by diablo_programmer; Aug 3rd, 2007 at 06:05 AM.
-
Aug 2nd, 2007, 07:23 PM
#2
Re: order by links
Did my solution in the first post you created not help you enough? If you had further question, you should have asked.
Can you please edit your post using php tags? thanks!
My usual boring signature: Something
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|