Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I get php to 'remember' the conducted search after selecting to sort results?
    primarykey
    data
    text
    <p>I have the following code:</p> <pre><code>&lt;?php include "include.php"; session_start(); // Defining variables from home.php if set if(isset($_POST['title'])){ $title = mysql_real_escape_string($_POST['title']); } if(isset($_POST['author'])){ $author = mysql_real_escape_string($_POST['author']); } if(isset($_POST['isbn'])){ $isbn = mysql_real_escape_string($_POST['isbn']); } if(isset($_POST['keyword'])){ $keyword = mysql_real_escape_string($_POST['keyword']); } if(isset($_POST['sort'])){ $sort=@$_POST['sort'];} // Determine the WHEREs to use $where = array(); if ( ! empty($title)) $where[] = "booktitle LIKE '%" . $title . "%'"; if ( ! empty($author)) $where[] = "author LIKE '%" . $author . "%'"; if ( ! empty($isbn)) $where[] = "isbn LIKE '%" . $isbn . "%'"; if ( ! empty($keyword)) $where[] = "description LIKE '%" . $keyword . "%'"; //Determine the ORDERs to use $order = array(); if ((isset($sort)) &amp;&amp; ($sort=='lowhigh')) $order[] = "price ASC"; if ((isset($sort)) &amp;&amp; ($sort=='highlow')) $order[] = "price DESC"; // Build the query $query = 'SELECT * FROM book'; if ( ! empty($where)) $query .= ' WHERE ' . implode(' AND ', $where); if(! empty($sort)) $query .= ' ORDER BY ' . implode($order); //Display results $result = mysqli_query($con, $query) or die("Error in query $query: " . mysqli_error()); while ($row = mysqli_fetch_array($result)) { ?&gt;&lt;a id="1" href="book.php" border="0"&gt;&lt;img src="&lt;?php echo $row[12]; ?&gt;" width="112px" height="150px" /&gt;&lt;/a&gt; &lt;a href="book.php?booktitle='&lt;?php echo $_SESSION['booktitle']=$row[1];?&gt;'"&gt;&lt;b&gt;&lt;?php echo $row[1] . ", (" . $row[5] . ")";?&gt;&lt;/b&gt;&lt;/a&gt;&lt;br&gt;&lt;?php echo $row[2]; echo "&lt;br&gt;&lt;div style='text-align:right'&gt; &lt;b&gt;&amp;pound;" . $row[9] . "&lt;/b&gt;&lt;/div&gt;"; echo "&lt;b&gt;Book description:&lt;/b&gt; " . substr($row[3],0,300) . "...&lt;br /&gt;&lt;hr&gt;"; } mysqli_close($con); //closes the connection ?&gt; </code></pre> <p>It passes inputs from four text fields on a front end page and displays the results. This all works fine. It's when I come to sorting the displayed results. If I selected Price: low to high for example, it arranges all the results in my database and displays everything, not just the results from the conducted search. Does anyone now how I can get it to remember the original search?</p> <p>This is my form:</p> <pre><code>&lt;form id="rform" name="formsort" action="" method="post"&gt; &lt;select name="sort" id="sort" onChange="document.forms['formsort'].submit()"&gt; &lt;option value="default"&gt;Sort Results&lt;/option&gt; &lt;option value="lowhigh"&gt;Price: Low to High&lt;/option&gt; &lt;option value="highlow"&gt;Price: High to Low&lt;/option&gt; &lt;/select&gt; &lt;/form&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload