loop help

Hi guys :slight_smile:

Can anyone tell me what I’m doing wrong? Trying to get an ‘no match/result found’ message to appear when no data is returned from the search…

The best I can get is the loop working, but displaying no information instead of it saying no results…

[php] $keywordinput = $_POST[“keywordinput”];
$result = mysqli_query($con,"SELECT * FROM pdfs

      WHERE Keywords LIKE '%$keywordinput%'");

      echo "<h1>Showing results for " . $keywordinput . "</h1>"; //You would post whatever the user is searching here
      $count = 0;
	
		if ($result=="") {
 			echo ("No matches");
						}
			 else {

        while($row = $result->fetch_assoc()){
 	      $count++;
     	  echo "$count." . $row['THUMBNAIL'] . "<a href='http://www.bictechnology.co.uk/" . $row['FILENAME'] . "'>" . $row['Title'] . "</a><br />" . $row['description'] . "<br /><br />";
		}
    }

[/php]

have also tried this…

[php] if (!$result)
{
echo (“No matches”);
}
else {

        	while($row = $result->fetch_assoc()){
  	      $count++;
      	  echo "$count." . $row['THUMBNAIL'] . "<a href='http://www.bictechnology.co.uk/" . $row['FILENAME'] . "'>" . $row['Title'] . "</a><br />" . $row['description'] . "<br /><br />";
		}

[/php]

What are you trying to increment using $count++? You havent given it a target value. But I dont think you want to use that in this situation.

try $result==empty()

But you need to get the number of rows that come from your query. Then use that in your if statement

(if($num_rows = 0) { echo ("No matches"); } else { ......... }

Hi mr Wilson,

The count variable is so I can display my results numbered.

Eg

  1. Result 1 bahdjdjdj
  2. Result 2 djdjdjfjcj

The script itself works but I’m now trying to ad a method of saying . ‘No results’.

I shall try your suggestion when I’m back at my computer…

Thanks for your time n effort

Try displaying your results in a ordered list. That should give you exactly what you need.

<ol>
<?php
//do a foreach here
echo "<li>$data</li>";
?>
</il>

Sponsor our Newsletter | Privacy Policy | Terms of Service