Forum Moderators: coopster
My question seems is more about HTML than PHP or MySQL (and I guess its pretty stupid ). I have a table with two vertical columns on the web-page where I print the search results. In the first column I put the ordinal number of the search strings, in the second – the very search strings. The task seems easy, but I get the columns mixed all the time. It really nonplused me. Could you please look at the code and tell me where I should put <tr> and <td> to put the table into the right order? Thank you very much in advance!
<?
echo "<form action=?=search method=post>
<input type=text name=search>
<input type=submit name=submit value=go>
</form>";
if ($search)
{
$srch="%".$search."%";
$query = "select id, string from tengri where msg LIKE '%$srch%'";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
echo '<p>Results found: '.$num_results.' </p>';
if ($result)
echo '<table cellspacing="2" cellpadding="2" border="0">';
{
for ($i=0; $i<$num_results; $i++)
{
echo "<tr>";
echo '<td colspan=4 bgcolor=green>'.($i+1).'.</td>';
while ($r = mysql_fetch_array($result))
{
$string = $r["string"];
echo '<td colspan=4 bgcolor =red>'.$string.'</td>';
}
echo "</tr>";
}
echo "</table>";
}
}
?>
I've cleaned up your PHP, MySQl, and HTML code.
I've tested out the code below, and it should do what you want:
<?php
echo '<form action='.$_SERVER['PHP_SELF'].' method="post">
<input type="text" name="search">
<input type="submit" name="submit" value="go">
</form>';if ('POST' == $_SERVER['REQUEST_METHOD']) {
$srch = quote_smart('%'.$_POST['search'].'%');
$query = "SELECT `id`, `string` FROM `tengri` WHERE `msg` LIKE $srch";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
echo '<p>Results found: '.$num_results.' </p>';
if (0!= $num_results)
{
echo '<table cellspacing="2" cellpadding="2" border="0">'."\n";
$i = 0;
while ($r = mysql_fetch_array($result))
{
$string = $r["string"];
echo "<tr>\n";
echo '<td bgcolor="green">'.($i+1)."</td>\n";
echo '<td bgcolor="red">'.$string."</td>\n";
echo "</tr>\n";
}
echo "</table>\n";
}
}// Quote variable to make safe
function quote_smart($value)
{
// Stripslashes
if (get_magic_quotes_gpc()) {
$value = stripslashes($value);
}
// Quote if not integer
if (!is_numeric($value)) {
$value = "'" . mysql_real_escape_string($value) . "'";
}
return $value;
}?>
Checking if the form was posted by using:
if ('POST' == $_SERVER['REQUEST_METHOD']) is a better method than checking if $search is equal to TRUE.
Calling the quote_smart() function on the search query protects you from SQL injection attacks.
I eliminated the "for" loop because all you needed was the "while" loop.
If you have any questions or problems just post back and I'll be glad to assist you further. :)
Hope this helps,
Elijah
Thank you so much for your assistance:)!
Elijah using the code I get this error message on -
Fatal error: Call to undefined function: quote_smart() in z:\home\localhost\www\s.php on line 10
Beside this I have the same problem with mysql_num_rows function. What am I to do? Is this a PHP version problem or something?
Thanks again!
Regards, Tengri.
Also, might be a good idea to add the $srch variable between apostrophes:
$query = "SELECT `id`, `string` FROM `tengri` WHERE `msg` LIKE '$srch'";
dc