页面加载时不可见"未找到结果"

时间:2017-07-28 05:34:50

标签: php mysql wordpress

$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
  }
}

我使用此代码,但默认情况下页面加载&#34;未找到结果&#34;可见。我如何禁用页面加载。任何帮助。

提前致谢

3 个答案:

答案 0 :(得分:1)

很简单,在搜索代码上方添加Condition,并将所有代码置于此条件下。请检查以下代码

在条件中使用isset ()确定变量是否已设置且不是NULL且!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 }
}

答案 1 :(得分:0)

试试这个

class UsersController < ApplicationController

  def edit
    @user = User.find(params[:id])
  end

  def update
    # devise
    if params[:user][:password].blank?
      params[:user].delete(:password)
      params[:user].delete(:password_confirmation)
    end
    @user = User.find(params[:id])
    if @user.update_attributes(user_params)
      flash[:success] = 'User Successfully changed'
      sign_out_and_redirect(@user)
      # push the user to sign out
    else
      flash[:error] = @user.errors.full_messages[0]
      redirect_to users_path
    end
  end

  private

  # white list parameter 
  def user_params
    params.require(:user).permit(
      :username, 
      :password, 
      :password_confirmation, 
      :email
  end
end

答案 2 :(得分:0)

这是未定义的索引

   $search_code = $_POST['search_code'];

所以我会改为

   $search_code = isset( $_POST['search_code'] ) ? $_POST['search_code']  : false;

然后将其他部分包装在if语句

 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

同样将echo与if else语句中的PHP开始和结束块混合在一起看起来很怪异,所以我也解决了这个问题。