Jump to content

Recommended Posts

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.. ::)

Link to comment
https://forums.phpfreaks.com/topic/187996-how-to-submit-a-form-to-the-same-page/
Share on other sites

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>';
?>

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.

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.

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.

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

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.