Good morning everyone (Manila Time).
I have a script here that searches the database based on a keyword. It works fine, searching and output, however the snippet wont work ($mess) I dont know what to put so I put the username it only prints the username. I want to print is like that of google. The string that appears below the link. Here is the code. Hope anyone helps me.
[code=php]<?php
include ‘db_connect.php’;
if(isset($_POST[‘submit’])){
$search = cleanString($_POST[‘search’]);
if(trim($search) == ”){
$errors = ‘<font color=”red”>Please enter a keyword</font><br /><br />’;
}
if($errors == ”){
$query = “SELECT * from users WHERE username LIKE ‘%$search%’ OR emailadd LIKE ‘%$search%’ OR firstname LIKE ‘%$search%’ OR midname LIKE ‘%$search%’ OR lastname LIKE ‘%$search%’ OR city_add LIKE ‘%$search%’ OR city_mobile LIKE ‘%search%’ OR prov_add LIKE ‘%$search%’ OR prov_mobile LIKE ‘%$search%’ OR bplace LIKE ‘%$search%’ OR age LIKE ‘%$search%’ OR weight LIKE ‘%$search%’ OR height LIKE ‘%$search%’ OR gender LIKE ‘%$search%’ OR citizenship LIKE ‘%$search%’ OR religion LIKE ‘%$search%’ OR cstatus LIKE ‘%$search%’ OR skin LIKE ‘%$search%’ OR eyes LIKE ‘%$search%'”;
$result = mysql_query($query) or die(mysql_error());
if($result){
display_form();
echo “<h1>found “. mysql_num_rows($result).” results</h1><br />”;
echo “<ul>”;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
$mess=$row[‘username’];
$view = ‘page.php?id=’.$row[‘id’];
echo “<li><a href=”.$view.”>”.$row[‘firstname’].” “.$row[‘midname’].” “.$row[‘lastname’].'</a></li>’;
echo ‘<p style=”margin-right:100px;margin-top: 0px;”>’.snippet($mess, 200, true).'</p>’;
}
}
}
}
if(!isset($_POST[‘submit’]) || $errors != ”){
echo $errors;
display_form();
}
function display_form(){
?>
<form id=”search” action=”<?php $_SERVER[‘PHP_SELF’]; ?>” method=”post”>
Keyword: <input type=”text” name=”search” size=”30″ class=”text” /><br />
<input type=”submit” name=”submit” value=”Search” />
</form>
<?php
}
function snippet($text, $chars, $dots = false)
{
$end_char = substr($text, $chars, 1);
if(preg_match(‘#S#’, $end_char))
{
$chars += strpos($text, ‘ ‘, $chars) – $chars;
}
$ret = ($dots) ? substr($text, 0, $chars) . ‘…’ : substr($text, 0, $chars);
return $ret ;
}
?>