2

In short, I have a PHP output (the code is only part) which needs to be hyperlinked when displayed on the webpage, but the issue is, when there's no result, it hyperlinks a blank URL. Is there anyway to not display the HTML if there's no result when the search is executed?

To clarify, I'd need to apply this to more than 1 section of the output.

Here's the specific part of the code.

if(isset($_POST['search'])) {

$searchq = $_POST ['search'];
$query = mysql_query ("SELECT * FROM HH_list2 WHERE Company LIKE '%$searchq%'") or die ("could not search"); //OT TO HERE !!!!!
$count = mysql_num_rows($query);
if ($count == 0) {
    $output = 'Sorry but we did not find anything';
    }else{
        while($row = mysql_fetch_array($query)) {
            $Company = $row ['Company'];
            $Twitter_ID = $row ['Twitter_ID'];
            $Online_chat = $row ['Online_chat'];
            $Online_form = $row ['Online_form'];
            $Email_1 = $row ['Email_1'];
            $Email_2 = $row ['Email_2'];
            $Email_3 = $row ['Email_3'];

            $output .= '<div style="background-color:#ebebeb; padding: 2em;"> <h3>'.$Company.' </h3><br/> Twitter: <a href="http://www.twitter.com/'.$Twitter_ID.'" target="_blank">'.$Twitter_ID.'</a><br/> <a href="'.$Online_chat.'" target="_blank">Online Chat</a> <br/> <a href="'.$Online_form.'" target="_blank">Online Form</a><br/>'.$Email_1.'<br/>'.$Email_2.'<br/>'.$Email_3.'</div>';


        }

}

}

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
user1721451
  • 247
  • 6
  • 15
  • Could you post your code of you querying the database? All you need to do is check if that comes back as empty or not and work with it in an IF/ELSE. – Tricky12 Aug 12 '13 at 18:14

1 Answers1

5

Simply use empty() function

if(!empty($Twitter_ID)){
$output .= '<div style="background-color:#ebebeb; padding: 2em;"> <br/>
Twitter: <a href="http://www.twitter.com/'.$Twitter_ID.'" target="_blank">'.$Twitter_ID.'</a>
</div>';
}

Reference php empty()

Regarding your second problem

if(isset($_POST['search'])) {

$searchq = $_POST ['search'];
$query = mysql_query ("SELECT * FROM HH_list2 WHERE Company LIKE '%$searchq%'") or die ("could not search"); //OT TO HERE !!!!!
$count = mysql_num_rows($query);
if ($count == 0) {
    $output = 'Sorry but we did not find anything';
    }else{
        while($row = mysql_fetch_array($query)) {
            $Company = $row ['Company'];
            $Twitter_ID = $row ['Twitter_ID'];
            $Online_chat = $row ['Online_chat'];
            $Online_form = $row ['Online_form'];
            $Email_1 = $row ['Email_1'];
            $Email_2 = $row ['Email_2'];
            $Email_3 = $row ['Email_3'];

            $output .= '<div style="background-color:#ebebeb; padding: 2em;">';
if(!empty($Company)){
 $output .= '<h3>'.$Company.' </h3><br/> ';
 if(!empty($Twitter_ID)){
 $output .= 'Twitter: <a href="http://www.twitter.com/'.$Twitter_ID.'" target="_blank">'.$Twitter_ID.'</a><br/>';
}
 if(!empty($Online_chat)){
  $output .= '<a href="'.$Online_chat.'" target="_blank">Online Chat</a> <br/> ';
}
 if(!empty($Online_form)){
 $output .= '<a href="'.$Online_form.'" target="_blank">Online Form</a><br/>';
}
 if(!empty($Email_1)){
 $output .=$Email_1.'<br/>';
}
 if(!empty($Email_2)){
 $output .=$Email_2.'<br/>';
}
 if(!empty($Email_3)){
 $output .=$Email_3;
}
 $output .='</div>';


        }

}

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118