Jump to content

display search results Horizontally


bickyz

Recommended Posts

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

<php?
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>";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/192753-display-search-results-horizontally/
Share on other sites

thanks greatstar00 but the results are displayed like this:

 

A

B C D E

J

M N O P

S

U V W X

 

i.e.

for example if there are 15 results from the search they displays like this

 

first row displays 1 result

second displays 4 results

third displays 1 result

fourth displays 4 results

fifth displays 1 result

sixth displays 4 results

 

i dont why they are not displayed 4 results per row.

A correction to the code you were given earler

$i=1;
echo '<table><tr>';
while($r=mysql_fetch_array($q))
{
        echo '<td>'."$id <br> $title <br> $message <br> $imgurl <br>".'</td>';
        if($i % 4 == 0)
                echo '</tr><tr>';
       $i++;
}
echo '</tr></table>';

just a quick ques:

how do i display text "No Results Found" if search cant find anything.

 

this is my final code:

<?php
ini_set('display_errors',2);
error_reporting(E_ALL);
?>
<?php
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%'");

$i=1;
echo '<table><tr>';
while($r=mysql_fetch_array($result))
{
   		$title=$r["firstname"];
   		$message=$r["surname"];
   		$imgurl=$r["imgurl"];
   		$vdourl=$r["vdourl"];
   		$id=$r["id"];

        echo '<td>'."$id <br> $title <br> $message <br> $imgurl <br>".'</td>';
        if($i % 4 == 0)
                echo '</tr><tr>';
       $i++;
}
echo '</tr></table>';
?>

just after you get $result

 

if(mysql_num_rows($result))
{
      $i=1;
      echo '<table><tr>';
      while($r=mysql_fetch_array($result))
      {
          ........
          ........
          existing code
      }
}
else
{
     echo 'No results found';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.