PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
$tbSQL = "SHOW TABLES";
$tbQuery = mysql_query($tbSQL);
$tNum = 0;
$num = 0;
if ($num >= 2 || $num == 0)
{
echo "There are " . $num . " results for your search.<br/><br/>";
}
elseif ($num == 1)
{
echo "There is " . $num . " result for your search.<br/><br/>";
}
while ($tb = mysql_fetch_array($tbQuery))
{
$sql[$tNum] = "SELECT * FROM " . $tb[0] . " WHERE title LIKE '%" . $search . "%' OR sub_section LIKE '%" . $search . "%' OR summary LIKE '%" . $search . "%' OR article LIKE '%" . $search . "%' ORDER BY title ASC";
$query[$tNum] = mysql_query($sql[$tNum]);
$num += (int)mysql_num_rows($query[$tNum]);
echo "<table>";
echo "<tr><td style=\"font-weight:bold;font-size:16px\">Results from " . ucwords(str_replace("_"," ", $tb[0])) . "</td></tr>";
echo "<tr><td>";
while($row[$tNum] = mysql_fetch_array($query[$tNum]))
{
echo "<table style=\"padding:20px;border-bottom:solid 1px #FF0F21\">";
echo "<tr><td style=\"color:#FF0F21;font-weight:bold;text-decoration:none\">" . str_replace($search,"<span style=\"background-color:#999999\">". $search . "</span>", $row[$tNum]['sub_section']) . "</td></tr>";
echo "<tr><td style=\"color:#999999;text-decoration:none;padding-left:30px\"><a href=\"test.php?s=" . $tb[0] . "&sub=" . $row[$tNum]['sub_section'] . "&ID=" . $row[$tNum]['ID'] . "\">" . str_replace($search,"<span style=\"background-color:#90EE90\">". $search . "</span>", $row[$tNum]['title']) . "</a></td></tr>";
echo "<tr><td><strong>" . str_replace($search,"<span style=\"background-color:#90EE90\">". $search . "</span>", $row[$tNum]['summary']) . "</strong></td></tr>";
echo "<tr><td>" . str_replace($search,"<span style=\"background-color:#90EE90\">". $search . "</span>", $row[$tNum]['article']) . "</td></tr>";
echo "</table>";
}
echo "</td></tr>";
echo "</table>";
$tNum ++;
}
see I want to display the search results where it's trying to output them, but because $num doesn't get populated until after the while loops have finished, it returns zero everytime.
$sql[$tNum] = "SELECT COUNT(id) FROM " . $tb[0] . " WHERE title LIKE '%" . $search . "%' OR sub_section LIKE '%" . $search . "%' OR summary LIKE '%" . $search . "%' OR article LIKE '%" . $search . "%' ORDER BY title ASC";
Don't output the results in the loop save it to a string and then output it after you output the count
If I use mysql_num_rows before the while loop, won't I have to run all the sql before that aswell, which means making two more while loops for before the mysql_num_rows?
It works if you output it after all the loops have finished, but as far as a searching page goes, I've never seen one with the number of results at the after the results.
Since hawlyjr already gave a couple of valid solutions (which you don't seem to understand) i'll give another one:
instead of echo-ing all the html in your loop... Append all the data to a $string... Preprend the numbers... And output... (Hmmz, perhaps this should be considered as the manual implementation of the output-buffering solution hawlyjr referred too)