ghop2003 Posted February 16, 2006 Share Posted February 16, 2006 I pasted my code below. It isn't my own code, I got it from somewhere that I can't remember and modified it. Right now, to see some entries i use a link like index.php?cat=*&val=*. I want to add another modifier. I'm trying to order the database by something other than 'title' initially. For example, to order by genre, I want something to use a link like index.php?cat=*&val=*&order=genre. I added this code right before the '//Performing SQL query' comment.[!--sizeo:1--][span style=\"font-size:8pt;line-height:100%\"][!--/sizeo--]$default_sort = 'title';$allowed_order = array ('title', 'director','year','genre','owner');/* 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'];}[!--sizec--][/span][!--/sizec--]Then, I place $order in the ORDER BY section of the query, but it doesn't seem to work. I've tried ORDER BY $order'; and ORDER BY '$order'\';Any help would be appreciated.[!--sizeo:1--][span style=\"font-size:8pt;line-height:100%\"][!--/sizeo--]<?php// Connecting, selecting database$link = mysql_connect('localhost', '******', '******') or die('Could not connect: ' . mysql_error());mysql_select_db('dvdswapn_movies') or die('Could not select database');// Performing SQL queryif ($_GET['cat']=='*' or $_GET['cat']==NULL) $query = 'SELECT title, director, year, genre, cover, owner, review_url, cover_url FROM movies ORDER BY title';else $query = 'SELECT title, director, year, genre, cover, owner, review_url, cover_url FROM movies WHERE '.$_GET['cat'].' LIKE \''.$_GET['val'].'\' ORDER BY title';$result = mysql_query($query) or die('Query failed: ' . mysql_error());// Printing results in HTMLecho "<table width=100% border=0 cellpadding=3 cellspacing=0>\n";echo "\t<tr bgcolor=#222>\n";echo "\t\t<td><font size=\"-6\"><u>Title</u></font></td>\n";echo "\t\t<td><font size=\"-6\"><u>Director</u></font></td>\n";echo "\t\t<td><font size=\"-6\"><u>Year</u></font></td>\n";echo "\t\t<td><font size=\"-6\"><u>Genre</u></font></td>\n";echo "\t\t<td><font size=\"-6\"><u>Cover Image</u></font></td>\n";echo "\t\t<td><font size=\"-6\"><u>Owner</u></font></td>\n";echo "\t</tr>\n";$count = 0;while ($line = mysql_fetch_array($result, MYSQL_BOTH)) { $count++; if($count%2) echo "\t<tr bgcolor=#1E1E1E>\n"; else echo "\t<tr bgcolor=#222>\n"; echo "\t\t<td><font size=\"-6\"><a href=\"$line[review_url]\">$line[title]</a></font></td>\n"; echo "\t\t<td><font size=\"-6\">$line[director]</font></td>\n"; echo "\t\t<td><font size=\"-6\">$line[year]</font></td>\n"; echo "\t\t<td><font size=\"-6\">$line[genre]</font></td>\n"; echo "\t\t<td><font size=\"-6\"><a href=\"$line[cover_url]\">$line[cover]</a></font></td>\n"; echo "\t\t<td><font size=\"-6\">$line[owner]</font></td>\n"; echo "\t</tr>\n";}echo "</table>\n";echo "<hr align=\"left\" width=60% size=1 color=\"#B0B0B0\">\n";echo "<font color=\"#B0B0B0\">$count results</font>\n";// Free resultsetmysql_free_result($result);// Closing connectionmysql_close($link);?> [!--sizec--][/span][!--/sizec--] Quote Link to comment https://forums.phpfreaks.com/topic/3444-need-help-with-my-code/ 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.