Reputation: 328
$search_code = $_POST['search_code'];
global $wpdb;
$helloworld_id = $wpdb->get_var("SELECT s_image FROM search_code WHERE s_code = $search_code");
if (empty($helloworld_id)) {
echo '<div class="no_results">No results found</div>';
}else
{ ?>
<img src="http://igtlaboratories.com/wp-content/uploads/images/<?php echo $helloworld_id; ?>"style="width:200px;height: auto;">
<?php
}
}
I using this code but when page load by default "no result found" visible . how i disable on page load. Any help.
Thanks in advance
Upvotes: 0
Views: 88
Reputation: 21671
This is undefined index
$search_code = $_POST['search_code'];
So I would change that to
$search_code = isset( $_POST['search_code'] ) ? $_POST['search_code'] : false;
Then wrap the other part in an if statement
if( $search_code ){
global $wpdb;
$helloworld_id = $wpdb->get_var("SELECT s_image FROM search_code WHERE s_code = $search_code");
if (empty($helloworld_id)) {
echo '<div class="no_results">No results found</div>';
}else{
echo '<img src="http://igtlaboratories.com/wp-content/uploads/images/'.$helloworld_id.'"style="width:200px;height: auto;">';
}
}//end if $search_code
Also mixing the echo
with the PHP start and end blocks in the if else statement looked weird to me so I fixed that too.
Upvotes: 0
Reputation: 4148
It simple, add Condition above the your search code and put your all code inside this condition. Please check below code
Used isset () in condition for Determine if a variable is set and is not NULL and !empty()
if(isset($_POST['search_code']) && !empty($_POST['search_code']))
{
// Your code goes here
$search_code = $_POST['search_code'];
global $wpdb;
$helloworld_id = $wpdb->get_var("SELECT s_image FROM search_code WHERE s_code = $search_code");
if (empty($helloworld_id)) {
echo '<div class="no_results">No results found</div>';
}else { ?>
<img src="http://igtlaboratories.com/wp-content/uploads/images/<?php echo $helloworld_id; ?>"style="width:200px;height: auto;">
<?php }
}
Upvotes: 1
Reputation: 543
just try this
if(isset($_POST['search_code']) && !empty($_POST['search_code']))
{
$search_code = $_POST['search_code'];
global $wpdb;
$helloworld_id = $wpdb->get_var("SELECT s_image FROM search_code WHERE s_code = $search_code");
if (empty($helloworld_id)) {
echo '<div class="no_results">No results found</div>';
}else{ ?>
<img src="http://igtlaboratories.com/wp-content/uploads/images/<?php echo $helloworld_id; ?>"style="width:200px;height: auto;">
<?php }
}
Upvotes: 0