Display search results Horizontally

hi i have this code which displays search results vertically, how do i display results horizontally like 4 records in per row.

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
?>

<?
mysql_connect("localhost","user1","password"); 
	
mysql_select_db("dbimages"); 

$search=$_POST["search"];
$field=$_POST["field"];

if ($search == "")
{
echo "<p>You forgot to enter a search term";
exit;
}

$result = mysql_query("SELECT * FROM tblimg WHERE upper($field) LIKE '%$search%'");

while($r=mysql_fetch_array($result))
{	
  
   $title=$r["firstname"];
   $message=$r["surname"];
   $imgurl=$r["imgurl"];

   echo "$id <br> $title <br> $message <br> $imgurl <br>";
}
?>

I would recommend against using PHP to do this, because you’d probably end up with some ghastly table-based method. The best-practise way is to do it as a list, using CSS to make them stack vertically.

<?php 
ini_set('display_errors', 2); //2 means that it shows warnings/notices too.
error_reporting(E_ALL); 
mysql_connect("localhost","user1","password"); 
mysql_select_db("dbimages"); 
$search = mysql_real_escape_string($_POST['search']); //you MUST protect your queries from SQL injection
$field =mysql_real_escape_string($_POST['field']);
if ($search == ""){ 
    echo "<p>You forgot to enter a search term"; 
    exit; 
} 
$result = mysql_query("SELECT * FROM tblimg WHERE {$field} LIKE '&#37;{$search}%'"); 
echo '<ul class="SearchResults">';
while($r = mysql_fetch_array($result)){
    $ID = $r['id'];
    $Title = $r['firstname'];
    $Message = $r['surname'];
    $ImgURL = $r['imgurl'];
    echo <<<LIST
        <li>
            <ul class="Result">
                <li>ID: {$ID}</li>
                <li>Title: {$Title}</li>
                <li>Message: {$Message}</li>
                <li>Image: <img src="{$ImgURL}" /></li>
            </ul>
        </li>
LIST;
}
echo '</ul>';

Rough guess at the CSS:


.SearchResults{
    display: block;
    padding: 0;
    width: 45em;
}
.SearchResults .Result{
    display: block;
    float: left;
    width: 10em;
    margin: 0.5em;
}

thanks, Jake.

just a quick ques:
how do i display text “No Results Found” if search cant find anything.

If [fphp]mysql_num_rows[/fphp] returns 0, show your message. :wink: