dthjnc306679 2018-06-13 03:17
浏览 60

PHP条件语句?

First, I would like to say that I'm not the greatest PHP developer. That being said, I would like some input on my code. I spent an hour trying to get this to work and I finally got it to work, however, I would like to know if this is the best way to do it. I'm asking the community because I'm trying to become a better developer.

I'm building a search form that uses POST method to send values to a search page. This page then takes the values, checks the conditional statements, and updates the query based on the results.

Here is my code.

Search form code

echo '<form method="post" class="cat-search" action="'.esc_url( home_url( '/search' ) ).'" role="search" >
            <div class="gender-fields">
                <span class="label-wrap">
                    <input type="radio" name="gender" value="male" id="male">
                    <label for="male">Male</label>
                </span>
                <span class="label-wrap">
                    <input type="radio" name="gender" value="female" id="female">
                    <label for="female">Female</label>
                </span>
                <span class="label-wrap">
                    <input type="radio" name="gender" value="" id="both">
                    <label for="both">Both</label>
                </span>
            </div>
            <select class="cat-letters" name="letter">
             <option value="">Search by letter</option>';
            foreach ( $categories as $category ) {
                echo'<option value="'.$category->slug.'">'.$category->name.'</option>';
            }
            echo '</select>
            <input class="search-submit" value="Search" type="submit" />
        </form>';

Search page conditional code

$let = esc_html($_POST["letter"]);
$gen = esc_html($_POST["gender"]);


if ( !empty($_POST["gender"]) && !empty($_POST["letter"])) {
    $cat_array = array($let,$gen);
    $cat_string = implode("+", $cat_array);
} else if ( empty($_POST["gender"] ) ) {
    $cat_string = $let;
} else {
    $cat_string = $gen;
}

First, I tried to use the isset() function instead of empty(), but I just couldn't get it to work.

Is this the right way to do it, or is there a better way to achieve the same results?

*I never had a problem with the form, the problem was with the conditional statements.

  • 写回答

3条回答 默认 最新

  • dsfs587465 2018-06-13 03:36
    关注

    I would just recommend using isset here

    $let = esc_html(isset($_POST["letter"]) ? $_POST['letter'] : '');
    $gen = esc_html(isset($_POST["gender"]) ? $_POST['gender'] : '');
    

    So you don't have any PHP errors when there are not letter and gender parameters received ;)

    Read more about ternay operators here

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)