Hi I have a quick php question i need some help on. Basicly i need php to echo </ul><ul> after each 4 results shown from a mysql query. Example: <?php $sql = mysql_query("SELECT * FROM `category` ORDER BY `id`"); while($c = mysql_fetch_array($sql)) { echo $c['title']; } ?> PHP: now what i need is after every 4 results to echo </ul><ul> how would i do this? Thanks, Rep will be added.
Non-tested, but you get the idea. <?php $sql = mysql_query("SELECT * FROM `category` ORDER BY `id`"); $i = 1; while($c = mysql_fetch_array($sql)) { echo $c['title']; if ($i == 4) { echo '</ul><ul>'; $i = 1; } else { ++$i; } } ?> PHP:
lol i got another question if you got time to answer. How do i make php check if a varible has bee defined. eg. <?php if($title == '?') { echo ' bla bla'; } ?> PHP: where the ? is i need something that can check if it has been defined, so if it does have anything set for $title it will echo bla bla. Do you know what the code to that is? Thanks
yup... empty() and isset() serve different purposes. the name 'empty' (or the implementation in PHP) can sometimes be a bit misleading. all values passed to empty() below are considered as "empty": if (empty("")) echo "one"; if (empty(0)) echo "two"; if (empty("0")) echo "three"; if (empty(null)) echo "four"; if (empty(false)) echo "five"; if (empty(array())) echo "six";
Check if variable is set and not empty: if(isset($title) && $title != "") { echo ' bla bla'; } Check if variable is set: if(!isset($title)) { echo 'title not set'; } Check if variable is empty: if($title == "") { echo 'title is empty'; }
Did a little checking. Seems empty() is faster than $var == '': But just a little. When doing either 10000000 times, empty() is about a second faster than $var == '' on my system. <?php $time_start = microtime(true); $var = ''; while ($i <= 10000000) { if (empty($var)) { //if ($var == '') { } ++$i; } $time_end = microtime(true); $time = $time_end - $time_start; echo 'Done in '.round($time, 3).' seconds'."\n"; ?> PHP: