If there any value to the search query

#1

Hi, I haven’t looked at php for a very long time and have just been asked to look at this, and add a small change to it but I’m not sure how to do it.

<?php

$in=$_GET['txt'];
$sid=$_GET['sid'];

if(isset($_GET['radio'])) { 
$radioB=$_GET['radio'];
}

$msg="";
if(strlen($in)>0 and strlen($in) <40 ){
if ($radioB == "resort") {
	$sql="select Nom_Hot, IdType_Hot, Id_Hot, IdRsrt_Hot, Dir_Hot, IdCat_Hot, Act_Hot from tbl_hotels LEFT JOIN tbl_resorts ON (tbl_resorts.Id_Rsrt=tbl_hotels.IdRsrt_Hot) where tbl_resorts.Nom_Rsrt like '%$in%' AND Act_Hot=1";
} elseif ($radioB == "hotel") {
	$sql="select Nom_Hot, IdType_Hot, Id_Hot, IdRsrt_Hot, Dir_Hot, IdCat_Hot, Act_Hot from tbl_hotels where Nom_Hot like '%$in%' AND Act_Hot=1 AND (IdType_Hot=3)";
} elseif ($radioB == "restaurants") {
	$sql="select Nom_Hot, IdType_Hot, Id_Hot, IdRsrt_Hot, Dir_Hot, IdCat_Hot, Act_Hot from tbl_hotels where Nom_Hot like '%$in%' AND (Act_Hot=1) AND (IdType_Hot=4) ";
} elseif ($radioB == "cruises") {
	$sql="select Nom_Hot, IdType_Hot, Id_Hot, IdRsrt_Hot, Dir_Hot, IdCat_Hot, Act_Hot from tbl_hotels where Nom_Hot like '%$in%' AND (Act_Hot=1) AND (IdType_Hot=2) ";
}
foreach ($dbo->query($sql) as $nt) {

$msg2="";
$int=$nt['IdCat_Hot'];

if (in_array($nt['IdCat_Hot'], array(6, 7))) {
 } else {
if($int>0) { $k=0; while($k<$int) {$msg2.="<img src='site_images/orange_Star_Transparent.png' width='11' height='10' style='vertical-align:1px;' alt='gold star' />"; $k++; } 
}
}



$msg .="<div style='position:relative; width:100%; height:auto; clear:both; margin-bottom:3px; margin-left:5px;'><a href='hotel.php?hotel_ID=$nt[Id_Hot]&amp;Type=$nt[IdType_Hot]&amp;Resort=$nt[IdRsrt_Hot]' title='$nt[Nom_Hot]' class='result_Link' style='width:100%; clear:both;'><span style='text-decoration:underline; color:#E17411'>$nt[Nom_Hot]</span> <img src='images/address_Icon_2.png' height='17px' style='vertical-align:-3px;' /> $nt[Dir_Hot] &nbsp;$msg2</a></div>";
}}
echo $msg;
?>

The bit I need to control is at the bottom and it’s $msg
At the moment its equal to the contents of that div, but basically what I need is that in an if statement so if the selects are equal to nothing in the if statements at the top, then I display an empty message in $msg, if its not empty then leave the $msg as it is.

At the moment I haven’t got a way of saying the search has no results, that is the reason for this. So a simple if else around $msg but I don’t know what to check to see if something has a value or not, hope that makes sense.

#2

Your code already does this.

Let me strip some stuff out.

<?php
$msg="";
$sql = "SELECT Something."
//PS: There is a flaw here in which $sql might not be defined. But that's a problem for another time.
foreach ($dbo->query($sql) as $nt) { //Not the best iteration form. Generally you should pull the query, and then while($result->fetch()).
$msg .="A lot of HTML";
}
echo $msg;
?>

If there are no rows returned by the query, then your foreach will execute exactly 0 times, and $msg will be empty.

#3

Ah cool that’s good to know, but what I need is in effect two messages.

One that says here are your results, and another saying you haven’t got any results

So at the moment, there only one message that says here are your results, and when there aren’t there it just shows nothing, so that’s why I thought I needed the if statement to split the two messages up.

Also hi m_hutley :slight_smile:

#4

If you want to present a message when there’s no results, simply check $msg after the foreach; if it’s still the empty string, you know there were no results.

Hi :slight_smile:

#6

Do you mean like this.

$in=$_GET['txt'];
$sid=$_GET['sid'];

if(isset($_GET['radio'])) { 
$radioB=$_GET['radio'];
}

$msg="";
if(strlen($in)>0 and strlen($in) <40 ){
if ($radioB == "resort") {
	$sql="select Nom_Hot, IdType_Hot, Id_Hot, IdRsrt_Hot, Dir_Hot, IdCat_Hot, Act_Hot from tbl_hotels LEFT JOIN tbl_resorts ON (tbl_resorts.Id_Rsrt=tbl_hotels.IdRsrt_Hot) where tbl_resorts.Nom_Rsrt like '%$in%' AND Act_Hot=1";
} elseif ($radioB == "hotel") {
	$sql="select Nom_Hot, IdType_Hot, Id_Hot, IdRsrt_Hot, Dir_Hot, IdCat_Hot, Act_Hot from tbl_hotels where Nom_Hot like '%$in%' AND Act_Hot=1 AND (IdType_Hot=3)";
} elseif ($radioB == "restaurants") {
	$sql="select Nom_Hot, IdType_Hot, Id_Hot, IdRsrt_Hot, Dir_Hot, IdCat_Hot, Act_Hot from tbl_hotels where Nom_Hot like '%$in%' AND (Act_Hot=1) AND (IdType_Hot=4) ";
} elseif ($radioB == "cruises") {
	$sql="select Nom_Hot, IdType_Hot, Id_Hot, IdRsrt_Hot, Dir_Hot, IdCat_Hot, Act_Hot from tbl_hotels where Nom_Hot like '%$in%' AND (Act_Hot=1) AND (IdType_Hot=2) ";
}

foreach ($dbo->query($sql) as $nt) {

$msg2="";
$int=$nt['IdCat_Hot'];

if (in_array($nt['IdCat_Hot'], array(6, 7))) {
 } else {
if($int>0) { $k=0; while($k<$int) {$msg2.="<img src='site_images/orange_Star_Transparent.png' width='11' height='10' style='vertical-align:1px;' alt='gold star' />"; $k++; } 
}
}


if ($msg = ""){
$msg = "No Results Found";
} else {
$msg .="<div style='position:relative; width:100%; height:auto; clear:both; margin-bottom:3px; margin-left:5px;'><a href='hotel.php?hotel_ID=$nt[Id_Hot]&amp;Type=$nt[IdType_Hot]&amp;Resort=$nt[IdRsrt_Hot]' title='$nt[Nom_Hot]' class='result_Link' style='width:100%; clear:both;'><span style='text-decoration:underline; color:#E17411'>$nt[Nom_Hot]</span> <img src='images/address_Icon_2.png' height='17px' style='vertical-align:-3px;' /> $nt[Dir_Hot] &nbsp;$msg2</a></div>";}
}}
echo $msg;

As it outputs the results but not the ‘No Results’ message

#7

Not quite. I literally meant “after the foreach”. as in… after the closing bracket of the foreach.

if ($msg = ""){
$msg = "No Results Found";
} else {
$msg .="<div style='position:relative; width:100%; height:auto; clear:both; margin-bottom:3px; margin-left:5px;'><a href='hotel.php?hotel_ID=$nt[Id_Hot]&amp;Type=$nt[IdType_Hot]&amp;Resort=$nt[IdRsrt_Hot]' title='$nt[Nom_Hot]' class='result_Link' style='width:100%; clear:both;'><span style='text-decoration:underline; color:#E17411'>$nt[Nom_Hot]</span> <img src='images/address_Icon_2.png' height='17px' style='vertical-align:-3px;' /> $nt[Dir_Hot] &nbsp;$msg2</a></div>";}
}}
echo $msg;

=>

$msg .="<div style='position:relative; width:100%; height:auto; clear:both; margin-bottom:3px; margin-left:5px;'><a href='hotel.php?hotel_ID=$nt[Id_Hot]&amp;Type=$nt[IdType_Hot]&amp;Resort=$nt[IdRsrt_Hot]' title='$nt[Nom_Hot]' class='result_Link' style='width:100%; clear:both;'><span style='text-decoration:underline; color:#E17411'>$nt[Nom_Hot]</span> <img src='images/address_Icon_2.png' height='17px' style='vertical-align:-3px;' /> $nt[Dir_Hot] &nbsp;$msg2</a></div>";}
}}
if ($msg = ""){ //At this point, we're done processing records. If the message is still blank, it means we never found a record to insert.
$msg = "No Results Found";
}
echo $msg;
1 Like
#8

No, no, no. Just shows, anyone can typo. :slight_smile:

if ($msg = ""){ //At this point, we're done processing records. If the message is still blank, it means we never found a record to insert.
$msg = "No Results Found";
}
#9

tries to spot typo, fails

You’ll have to point it out for me a bit more?

Anyway, multi the point was… leave the $msg .= line inside your foreach, and put this check after the foreach.

#10

This bit:

if ($msg = ""){ //At this point, we're done processing records. If the message is still blank, it

should be

if ($msg == "") {

surely?

1 Like
#11

Ye I have done that, and cant see the typo either lol, but its not working either way now.

foreach ($dbo->query($sql) as $nt) {

$msg2="";
$int=$nt['IdCat_Hot'];

if (in_array($nt['IdCat_Hot'], array(6, 7))) {
 } else {
if($int>0) { $k=0; while($k<$int) {$msg2.="<img src='site_images/orange_Star_Transparent.png' width='11' height='10' style='vertical-align:1px;' alt='gold star' />"; $k++; } 
}
}

$msg .="<div style='position:relative; width:100%; height:auto; clear:both; margin-bottom:3px; margin-left:5px;'><a href='hotel.php?hotel_ID=$nt[Id_Hot]&amp;Type=$nt[IdType_Hot]&amp;Resort=$nt[IdRsrt_Hot]' title='$nt[Nom_Hot]' class='result_Link' style='width:100%; clear:both;'><span style='text-decoration:underline; color:#E17411'>$nt[Nom_Hot]</span> <img src='images/address_Icon_2.png' height='17px' style='vertical-align:-3px;' /> $nt[Dir_Hot] &nbsp;$msg2</a></div>";
}}

if ($msg = ""){
$msg = "No Results Found";
}
echo $msg;
#12

Ah yes he is right :slight_smile:

#13

Oh yes. Okay, thats my fault for just quoting code from another post without logic-checking it :wink: good catch droop.

1 Like
#14

Ah yes, I hadn’t noticed it was that way in the OPs code as well.

1 Like
#15

Working now guys, thank you

2 Likes