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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?