Can I exclude HTML output when there's no result in mysql database when querying via PHP -
in short, have php output (the code part) needs hyperlinked when displayed on webpage, issue is, when there's no result, hyperlinks blank url. there anyway not display html if there's no result when search executed?
to clarify, i'd need apply more 1 section of output.
here's specific part of code.
if(isset($_post['search'])) { $searchq = $_post ['search']; $query = mysql_query ("select * hh_list2 company '%$searchq%'") or die ("could not search"); //ot here !!!!! $count = mysql_num_rows($query); if ($count == 0) { $output = 'sorry 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>'; } }
}
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 second problem
if(isset($_post['search'])) { $searchq = $_post ['search']; $query = mysql_query ("select * hh_list2 company '%$searchq%'") or die ("could not search"); //ot here !!!!! $count = mysql_num_rows($query); if ($count == 0) { $output = 'sorry 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. no longer maintained and officially deprecated. see red box? learn prepared statements instead, , use pdo or mysqli - this article decide which. if choose pdo, here tutorial.
Comments
Post a Comment