472,110 Members | 2,182 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,110 software developers and data experts.

display multiple random rows from a table if nothing found in search

66
Hi all, ive got a search function running on a database, the action performed by the form is shown below, within the 'else' statement i would like to show roughly 5 random rows from the sql table (alternative products) if you like.

please find below the script im currently running for the search, the else statment just shows 'no results found' message at the moment.

many thanks in advance for any who can help:

Expand|Select|Wrap|Line Numbers
  1.   <table width="40%" border="0" align="center" cellpadding="0" cellspacing="0">
  2.     <tr>
  3.       <td><img src="graphics/sunflowerbanner.jpg" width="830" height="227" /></td>
  4.     </tr>
  5.     <tr>
  6.       <td><?php
  7.     include "flowermatchdbcnx.php";
  8.      $r = @$_POST['occcas']; //users choice... e.g. birthday, anniversary etc.
  9.      $s = @$_POST['style']; //display type... modern or classic.
  10.      $p = @$_POST['price']; //price, need to be within the price range. 
  11.      $t = @$_POST['type']; //flower suitability.... mother father collegue etc.
  12.      $pak = @$_POST['pacakge']; //flowers packaging
  13.  
  14.  
  15.  
  16.     //The query to the database
  17.  
  18.  
  19.  
  20.     $query = ("SELECT * FROM products WHERE style IN ('$s') AND occcas IN ('$r') AND type IN ('$t') AND pacakge IN ('$pak') AND price IN ('$p')");
  21.  
  22.  
  23.     $result = mysql_query($query);
  24.     $result_count = mysql_num_rows($result);
  25.     //while loop to go through database table search all matching records.
  26.     if ($row = mysql_fetch_array($result)) {
  27.     //start of table... sets table headers
  28.     echo "<table border='1'><tr><td>name</td><td>type</td></tr>";
  29.     //returns results and puts them in ajoining table
  30.     printf ("<tr><td>%s</td><td>%s</td><td>%s</td></tr></table>",$row[8], $row[6], $row[5]);
  31.  
  32. }else { 
  33.     echo "there were no matches found, please find some alternatives below"; 
  34. }
  35.  
  36. ?></td>
  37.     </tr>
  38.     <tr>
  39.       <td><div align="center"><?PHP echo "$result_count\n"; ?></div></td>
  40.     </tr>
  41.   </table>
  42.  
  43.  
  44.  
  45.  
Dec 4 '07 #1
1 1630
stepterr
157 100+
You could do another query using Rand() within your query. Such as:
[PHP]
$query = "SELECT * FROM table ORDER BY Rand() LIMIT 2";
[/PHP]
Dec 15 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Voetleuce en f?nsievry | last post: by
32 posts views Thread by tshad | last post: by
reply views Thread by simon | last post: by
2 posts views Thread by assgar | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.