$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);
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
returnsexpr1
ifexpr1
evaluates toTRUE
, andexpr3
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.
Won't it be the easier?..
if ($q >= 1 && $q <= 10) {
$count1 += $count;
}