the problem is that its an undifined varible ($last) intill i type the second word, i

if($_POST)
{

$q=$_POST['searchword']; 

 $q = explode(' ', $q);  
 
 
              
 $first = $q[0];
 
 $last = $q[1];
    
$sql_res=mysqli_query($mysqli, "select * from dogs where firstname like '%$first%' and lastname like '%$last%' order by id LIMIT 5");           
while($row=mysqli_fetch_array($sql_res))
{
$firstname=$row['firstname'];
$lastname=$row['lastname'];
$MainImage=$row['MainImage'];
$country=$row['country'];
$id = $row['id'];

$re_fname ='<b>'.$first.'</b>';
$re_lname='<b>'.$last.'</b>';

$final_fname = str_ireplace($first, $re_fname, $firstname);

$final_lname = str_ireplace($last, $re_lname, $lastname);



echo '
<div style="position:relative;z-index:100000;">

<div class="display_box" style="display:block;" align="left">

<img src="image/'.$MainImage.'" style="width:50px; height:50px; float:left; margin-right:6px" />'.$final_fname.'&nbsp;'.$final_lname.'<br/>
<span style="font-size:9px; color:#999999">'. $country .'</span></div>

</div>

';      



}

}
else
{

}

would like do somet like this....

$textFields = array('lastname', 'firstname');

$conditions = array();
foreach ($textFields as $field)
    foreach ($q as $searchWord)
    
        $conditions[] = "\n\t$field LIKE '%$searchWord%'";
 
 
 
$sql_res=mysqli_query($mysqli, 'SELECT * FROM dogs WHERE '.implode(' or ', $conditions).' order by id LIMIT 5');

there problem is i dont want to have a query like this....

$sql_res=mysqli_query($mysqli, "select * from dogs where firstname like '%$first%' and lastname like '%$first%' and firstname like $last AND $lastname like $last order by id LIMIT 5");

i want it like this....

$sql_res=mysqli_query($mysqli, "select * from dogs where firstname like '%$first%' and lastname like '%$last%' order by id LIMIT 5");


any help ill be happy :)

Recommended Answers

All 2 Replies

Well, you'd need to be able to know if "$q[1]" exists, for example,

if($q[1]){
$last=$q[1]; }
else {
$last=""; }

There are many ways to do this, and the above is just a simple way - i.e if $q[1] is of a value other than empty, then make $last the value of $q[1], otherwise, make $last and empty string.

Quite tired at the moment, so apologies if this is of less help than I intend it to be.

Take care!

Well, you'd need to be able to know if "$q[1]" exists, for example,

if($q[1]){
$last=$q[1]; }
else {
$last=""; }

There are many ways to do this, and the above is just a simple way - i.e if $q[1] is of a value other than empty, then make $last the value of $q[1], otherwise, make $last and empty string.

Quite tired at the moment, so apologies if this is of less help than I intend it to be.

Take care!

thanks it helped out alot i used ... if(isset($q[1]; } :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.