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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jasone
    New Member
    • Mar 2007
    • 66

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

    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:

    Code:
      <table width="40%" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <td><img src="graphics/sunflowerbanner.jpg" width="830" height="227" /></td>
        </tr>
        <tr>
          <td><?php
        include "flowermatchdbcnx.php";
    	 $r = @$_POST['occcas']; //users choice... e.g. birthday, anniversary etc.
    	 $s = @$_POST['style']; //display type... modern or classic.
    	 $p = @$_POST['price']; //price, need to be within the price range. 
    	 $t = @$_POST['type']; //flower suitability.... mother father collegue etc.
    	 $pak = @$_POST['pacakge']; //flowers packaging
    	 
    	
    	 
    	//The query to the database
    	
    	
    	
    	$query = ("SELECT * FROM products WHERE style IN ('$s') AND occcas IN ('$r') AND type IN ('$t') AND pacakge IN ('$pak') AND price IN ('$p')");
    
    	
    	$result = mysql_query($query);
    	$result_count = mysql_num_rows($result);
    	//while loop to go through database table search all matching records.
    	if ($row = mysql_fetch_array($result)) {
    	//start of table... sets table headers
    	echo "<table border='1'><tr><td>name</td><td>type</td></tr>";
    	//returns results and puts them in ajoining table
    	printf ("<tr><td>%s</td><td>%s</td><td>%s</td></tr></table>",$row[8], $row[6], $row[5]);
    
    }else { 
    	echo "there were no matches found, please find some alternatives below"; 
    }
    	
    ?></td>
        </tr>
        <tr>
          <td><div align="center"><?PHP echo "$result_count\n"; ?></div></td>
        </tr>
      </table>
  • stepterr
    New Member
    • Nov 2007
    • 157

    #2
    You could do another query using Rand() within your query. Such as:
    [PHP]
    $query = "SELECT * FROM table ORDER BY Rand() LIMIT 2";
    [/PHP]

    Comment

    Working...