php - Create list per 4 records -


this question has answer here:

i trying create multi-list of records 4 records per list without knowing how many records there are. however, cannot figure out how handle math. manually typed in $n == 5 || $n == 9 etc knowing stipud , cannot solve problem. can me how handle that. also, lists underneath works if total number of records cannot evenly divided 4. if can, create blank list @ end.

$query = "select * `table` `field` = $whatever"; if ($result = $con->query($query)){ $n = 1 $row_cnt = $result->num_rows; $total_lists = round($row_cnt / 4, 0); $current_list = 1; echo "<ul>list $current_list of $total_lists";     while ($row = $result->fetch_assoc()) {     echo "<li>$row['something']</li>";         if ($n == 5 || $n == 9 || $n == 13 || $n == 17 || $n == 21 || $n ==25 || $n ==29 || $n == 33 || $n == 37 || $n == 41 || $n == 45 || $n == 49 || $n == 53 || $n == 57 || $n == 61 || $n == 65 || $n == 69 || $n == 73 || $n == 77){         echo "</ul>";         $current_list = $current_list + 1;         echo "<ul>list $current_list of $total_lists";         }     $n = $n + 1;     } echo "</ul>"; } 

thanks in advance help. :)

solved:

$query = "select * `table` `field` = $whatever"; if ($result = $con->query($query)){ $n = 0 $row_cnt = $result->num_rows; $total_lists = ceil($row_cnt / 4); $current_list = 1; echo "<ul>list $current_list of $total_lists";     while ($row = $result->fetch_assoc()) {         $n++;     echo "<li>$row['something']</li>";         if ($row_cnt > 4) {     if ($n % 4 === 0) {     echo "</ul>";     $current_list = $current_list + 1;     echo "<ul>list $current_list of $total_lists";         }         }     } echo "</ul>"; } 

you're not far off track might want doing, need use 1 additional tool accomplish it: % or modulus operator.

the modulus operator return remainder of division problem:

$x = 5 % 2; // 1 

looking @ logic, action needs taken when incrementer ($n) minus 1 divided 4 have remainder of 0:

if (($n - 1) % 4 === 0) {     //your <ul> insertion go here. } 

here's link php manual page discussing mathematical operators:

http://us1.php.net/manual/en/language.operators.arithmetic.php


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -