doutuo7156 2017-06-18 19:19 采纳率: 100%
浏览 46
已采纳

PHP三元语句不起作用

 $string = '';
  $q = $l = $count = $count1 = 0;
  // Query to get number of rows in table
  $query = "SELECT * FROM $section4";
  $result = mysqli_query($connect, $query);
  $numrows = mysqli_num_rows($result);
  mysqli_free_result();
  // Loop and switch statement for different "type"s of subsections in section
  while ($q <= 10) {
     // Reset values
     $test = $shown = false;
     $first = true;
     $count1 = ($q == 1 ?: $count1 + $count);
     $count1 = ($q == 2 ?: $count1 + $count);
     $count1 = ($q == 3 ?: $count1 + $count);
     $count1 = ($q == 4 ?: $count1 + $count);
     $count1 = ($q == 5 ?: $count1 + $count);
     $count1 = ($q == 6 ?: $count1 + $count);
     $count1 = ($q == 7 ?: $count1 + $count);
     $count1 = ($q == 8 ?: $count1 + $count);
     $count1 = ($q == 9 ?: $count1 + $count);
     $count1 = ($q == 10 ?: $count1 + $count);
     $group = $count = 0;
     $output = $numrows - $count1;


     $query = "SELECT * FROM $section4 LIMIT $output OFFSET $count1";
     $result = mysqli_query($connect, $query);
     while ($row = mysqli_fetch_assoc($result)) {
        if ($first) {
           $prev = $row["type"];
           $first = false;
        } //$first
        if ($prev == $row["type"]) {
           $prev = $row["type"];
           $count++;
        } //$prev == $row["type"]
     } //$row = mysqli_fetch_assoc($result)
     $query = "SELECT * FROM $section4 LIMIT $count OFFSET $count1";
     $result = mysqli_query($connect, $query);
     while ($row = mysqli_fetch_assoc($result)) {
        if (!in_array($row["classid"], $data)) {
           if (!$shown) {
              $string = $string . 'You need to take ' . $row["classname"] . ' (' . $row["classid"] . ')';
              $string1 = 'You need to take ' . $row["classname"] . ' (' . $row["classid"] . ')';
           } //!$shown
           else {
              $string.= ', and ' . $row["classname"] . ' (' . $row["classid"] . ')';
              $string1.= ', and ' . $row["classname"] . ' (' . $row["classid"] . ')';
           }
           $shown = true;
        } //!in_array($row["classid"], $data)
        else {
           array_push($taken, $row["classid"]);
           $totalhours = $totalhours + $row["classhours"];
           $group++;
        }
     } //$row = mysqli_fetch_assoc($result)
     $group0 = ($q == 0) ? : $group;
     $group1 = ($q == 1) ? : $group;
     $group2 = ($q == 2) ? : $group;
     $group3 = ($q == 3) ? : $group;
     $group4 = ($q == 4) ? : $group;
     $group5 = ($q == 5) ? : $group;
     $group6 = ($q == 6) ? : $group;
     $group7 = ($q == 7) ? : $group;
     $group8 = ($q == 8) ? : $group;
     $group9 = ($q == 9) ? : $group;
     $group10 = ($q == 10) ? : $group;
     mysqli_free_result();
     if ($shown) {
        $string = $string . '. ';
        $l++;
        $str0 = ($q == 0) ? : $string1;
        $str1 = ($q == 1) ? : $string1;
        $str2 = ($q == 2) ? : $string1;
        $str3 = ($q == 3) ? : $string1;
        $str4 = ($q == 4) ? : $string1;
        $str5 = ($q == 5) ? : $string1;
        $str6 = ($q == 6) ? : $string1;
        $str7 = ($q == 7) ? : $string1;
        $str8 = ($q == 8) ? : $string1;
        $str9 = ($q == 9) ? : $string1;
        $str10 = ($q == 10) ? : $string1;
     } //$shown
     $q++;
  } //$q <= 10

My end goal is to have "$count1" be the offset for mySQL database query. $count1 is supposed to add "$count" to get an updated "$count1" value. However, when i echo $count1 it outputs this information:

0 37 17 8 13 6 9 4 9 2 1

I understand my code is sloppy, and so is the database. This is simply a school project that is due quite soon.

I'm trying to turn:

if($q = $value){ $count1 = $count1 + $count; }

to:

$count1 = ($q == 1 ?: $count1 + $count);

  • 写回答

2条回答 默认 最新

  • dougou8552 2017-06-18 19:52
    关注

    The documentation of the ternary operator says:

    Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.

    Your expression:

    $q == 1 ?: $count1 + $count
    

    evaluates to the value of $q == 1 if $q == 1 evaluates to TRUE and to $count1 + $count otherwise.

    Because == is a logical operator, $q == 1 always evaluates to TRUE or FALSE (the "evaluates to TRUE" in the quoted documentation means "equals to TRUE using loose comparison").

    When $q is 1, the value of the expression above is TRUE and this is probably not what you wanted.

    It's not clear for me what exactly do you want to achieve. Anyway, the ternary operator is not a shorter form of the if statement as many people think.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?