$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
我的最终目标是让“$ count1”成为mySQL数据库查询的偏移量。 $ count1应该添加“$ count”以获得更新的“$ count1”值。但是,当我回显$ count1时,它会输出以下信息:
0
37
17
8
13
6
9
4
9
2
1
我理解我的代码很草率,数据库也是如此。这只是一个即将到期的学校项目。
我想转:
if($q = $value){ $count1 = $count1 + $count; }
为:
$count1 = ($q == 1 ?: $count1 + $count);
答案 0 :(得分:2)
MDN的文档说:
从PHP 5.3开始,可以省略三元运算符的中间部分。如果
expr1 ?: expr3
评估为expr1
,则表达式expr1
会返回TRUE
,否则会返回expr3
。
你的表达:
$q == 1 ?: $count1 + $count
如果$q == 1
评估为$q == 1
,则评估TRUE
的值,否则评估为$count1 + $count
。
由于==
是ternary operator,$q == 1
始终评估为TRUE
或FALSE
(&#34;评估为{{1}引用文档中的&#34; 表示&#34; logical operator&#34;)。
当TRUE
为$q
时,上面表达式的值为1
,这可能不是您想要的。
我不清楚你到底想要达到什么目标。无论如何,equals to TRUE
using loose comparison 不是是ternary operator的缩写形式,正如许多人想的那样。
答案 1 :(得分:1)
难道不是更容易吗?..
if ($q >= 1 && $q <= 10) {
$count1 += $count;
}