proud Posted January 11, 2010 Share Posted January 11, 2010 I have a table containing information about books in my library and this table has the following columns: id, title, category. What I want to do is to make a form that will allow me to sort these books according to one of these fields, its just like in you tube when you want to sort videos by date added, relevance, view count etc.. Both the form and the php code are in "view_books.php" Here is my form: <form method="POST" action="view_books.php"> Sort by: <select size="1" name="sort"> <option> id </option> <option> category </option> <option> Date Added </option> </select> <input type="submit" value="Go!"> </form> Here is my php code to sort the books in a table: $query = mysql_query ("SELECT * FROM books ORDER BY id asc ") or die (mysql_error()); ?> <table width="76%" border="0" align="center" > <? while($rows = mysql_fetch_array( $query )) { ?> <tr> <td> <font color="#696969" size=2> <?php echo $rows['id']; ?> </font></td> <td> <font color="#696969" size=2> <?php echo $rows['title']; ?> </font></td> <td> <font color="#696969" size=2> <?php echo $rows['category']; ?> </font></td> </tr> <? } ?> </table> Please apply the changes in my code so that it can perform the required function. Thanks, any help would be appreciated.. Quote Link to comment https://forums.phpfreaks.com/topic/187996-how-to-submit-a-form-to-the-same-page/ Share on other sites More sharing options...
tail Posted January 11, 2010 Share Posted January 11, 2010 Try this: <?php $optionlist=array('ID','Category','Date'); $order=$_GET['sort']; switch ($order) { case "date": $order = '`date` ASC'; break; case "category": $order = '`category` ASC`'; break; default: $order = '`id` ASC'; } $query = mysql_query("SELECT * FROM `books` ORDER BY ".$order."") or die (mysql_error()); echo '<form method="get" action="view_books.php">Sort by: <select name="sort">'; foreach($optionlist as $value) { if(strtolower($value)==$_GET['order']) { echo '<option value="'.strtolower($value).'">'.$value.'</option>'; } else { echo '<option value="'.strtolower($value).'" selected>'.$value.'</option>'; } } echo '</select><input type="submit" value="Go!"></form><table width="76%" border="0" align="center" >'; while($rows = mysql_fetch_array($query)) { echo '<tr><td><font color="#696969" size=2>'.$rows['id'].'</font></td><td><font color="#696969" size=2>'.$rows['title'].'</font></td><td><font color="#696969" size=2>'.$rows['category'].'</font></td></tr>'; } echo '</table>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/187996-how-to-submit-a-form-to-the-same-page/#findComment-992531 Share on other sites More sharing options...
soycharliente Posted January 11, 2010 Share Posted January 11, 2010 I know this is a PHP forum, but you could try a JavaScript solution as well: jQuery and Tablesorter I've had a lot of success with those tools. Simple code and very easy to manage. Quote Link to comment https://forums.phpfreaks.com/topic/187996-how-to-submit-a-form-to-the-same-page/#findComment-992625 Share on other sites More sharing options...
oni-kun Posted January 11, 2010 Share Posted January 11, 2010 I know this is a PHP forum, but you could try a JavaScript solution as well: jQuery and Tablesorter I've had a lot of success with those tools. Simple code and very easy to manage. Generally JS->SQL is a bad idea. Although it fits what he's trying to do, he's wanting to sort it serverside rather than client side. Quote Link to comment https://forums.phpfreaks.com/topic/187996-how-to-submit-a-form-to-the-same-page/#findComment-992626 Share on other sites More sharing options...
soycharliente Posted January 11, 2010 Share Posted January 11, 2010 Generally JS->SQL is a bad idea. Although it fits what he's trying to do, he's wanting to sort it serverside rather than client side. There's no JS->MySQL. It's all JS, HTML, CSS. Sorting server-side vs client-side makes no difference (I mean it does: bandwidth, server maint, etc.). He's not storing the conditions. Quote Link to comment https://forums.phpfreaks.com/topic/187996-how-to-submit-a-form-to-the-same-page/#findComment-992627 Share on other sites More sharing options...
oni-kun Posted January 11, 2010 Share Posted January 11, 2010 There's no JS->MySQL. It's all JS, HTML, CSS. What if he needed to export the table order in a specific way to anything? Teaching him JS to do something SQL can do is rather, backwards in the order of effectiveness in programming. Quote Link to comment https://forums.phpfreaks.com/topic/187996-how-to-submit-a-form-to-the-same-page/#findComment-992629 Share on other sites More sharing options...
soycharliente Posted January 11, 2010 Share Posted January 11, 2010 What if he needed to export the table order in a specific way to anything? Then he'd ask for that solution. For just sorting, those tools are VERY clean, simple, and easy to manage. Quote Link to comment https://forums.phpfreaks.com/topic/187996-how-to-submit-a-form-to-the-same-page/#findComment-992631 Share on other sites More sharing options...
oni-kun Posted January 11, 2010 Share Posted January 11, 2010 What if he needed to export the table order in a specific way to anything? Then he'd ask for that solution. For just sorting, those tools are VERY clean, simple, and easy to manage. I'm sure as all his relevant code, example references of what he wants to do.. Had nothing to do with PHP at all. You're right, he didn't ask specifics Quote Link to comment https://forums.phpfreaks.com/topic/187996-how-to-submit-a-form-to-the-same-page/#findComment-992632 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.