Quick PHP Question

Discussion in 'PHP' started by NetworkTown.Net, Nov 1, 2007.

  1. #1
    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.
     
    NetworkTown.Net, Nov 1, 2007 IP
  2. tamen

    tamen Peon

    Messages:
    182
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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:
     
    tamen, Nov 1, 2007 IP
    NetworkTown.Net likes this.
  3. NetworkTown.Net

    NetworkTown.Net Well-Known Member

    Messages:
    2,022
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    165
    #3
    Thanks worked.
     
    NetworkTown.Net, Nov 1, 2007 IP
  4. tamen

    tamen Peon

    Messages:
    182
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Great :)
    Hmm... Have to write some more.
     
    tamen, Nov 1, 2007 IP
  5. NetworkTown.Net

    NetworkTown.Net Well-Known Member

    Messages:
    2,022
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    165
    #5
    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
     
    NetworkTown.Net, Nov 1, 2007 IP
  6. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #6
    
    <?php
    if(empty($var)) {
    echo 'empty';
    } else {
    echo 'not empty';
    }
    ?>
    PHP:
     
    crazyryan, Nov 1, 2007 IP
    NetworkTown.Net likes this.
  7. NetworkTown.Net

    NetworkTown.Net Well-Known Member

    Messages:
    2,022
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    165
    #7
    lol thanks i was just reading the same thing on php.net

    thanks ;)
     
    NetworkTown.Net, Nov 1, 2007 IP
  8. tamen

    tamen Peon

    Messages:
    182
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    empty just checks if the var is empty.

    You might want to use isset($var) to check if the var is set.
     
    tamen, Nov 1, 2007 IP
  9. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #9
    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";
     
    phper, Nov 1, 2007 IP
  10. 9450184

    9450184 Peon

    Messages:
    30
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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'; }
     
    9450184, Nov 2, 2007 IP
  11. tamen

    tamen Peon

    Messages:
    182
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #11
    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:
     
    tamen, Nov 2, 2007 IP