SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Empty value check when iterating through array...

    I have an array that is created in a WHILE loop (fetches values from the database, assigns, then generates a simple UL. On the rare occasions that the UL has an empty LI, I would like for it to simply generate "nothing" (something like white-space or whatever).

    In order to do this, I believe I'll have to check for empty values in the array during the iterations. How would I do this exactly?

    Below is the code I'm working with and I welcome input on how to improve what I have thus far:

    Code:
    	function fs_chart(){
    		$chart[] = '';
    		$result = mysql_query("SELECT * FROM fs_members");
    		$count = mysql_num_rows($result);
    		$i=0;
    		while($row = mysql_fetch_array($result)){
    			//if(in_array('',$row)){echo $row[];}//Create condition logic to handle empty array values...
    			if($i % 2){$class[] = 'even';}else{$class[] = 'odd';}
    			$chart[] = '<ul class="inline '.$class[$i].'">
    							<li><a href="#" title="'.$row['first_name'].'">'.$row['first_name'].'</a></li>
    							<li><a href="#" title="'.$row['middle_name'].'">'.$row['middle_name'].'</a></li>
    							<li><a href="#" title="'.$row['last_name'].'">'.$row['last_name'].'</a></li>
    							<li><a href="#" title="'.$row['department'].'">'.$row['department'].'</a></li>
    							<li><a href="#" title="'.$row['office_phone_number'].'">'.$row['office_phone_number'].'</a></li>
    							<li><a href="#" title="'.$row['office_room_number'].'">'.$row['office_room_number'].'</a></li>
    							<li><a href="#" title="'.$row['membership_length'].'">'.$row['membership_length'].'</a></li>
    							<li><a href="#" title="'.$row['university_tenure'].'">'.$row['university_tenure'].'</a></li>
    							<li><a href="#" title="Delete">Delete</a></li>
    						</ul>';
    						$i++;
    		}
    		foreach($chart as $value){echo $value;}
    	}
    I know there's a lot of room for improvement here, so again, I welcome any suggestions.

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $str '<ul class="inline '.$class[$i].'">';

    if (!empty(
    $row['first_name']))
        
    $str .= '<li><a href="#" title="'.$row['first_name'].'">'.$row['first_name'].'</a></li>';

    if (!empty(
    $row['middle_name']))
        
    $str .= '<li><a href="#" title="'.$row['middle_name'].'">'.$row['middle_name'].'</a></li>';

    //... etc for all the <li>

    $str .= '</ul>';
    $chart[] = $str
    Unrelated -- $class can just as easily be a string, there's no reason to use an ever-growing array to assign the class name for the <ul>.

    PHP Code:
    $class = ($class == 'even') ? 'odd' 'even';
    $str '<ul class="inline ' $class '">'
    Last edited by Dan Grossman; Feb 12, 2010 at 11:19. Reason: fixed typo

  3. #3
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I still have a hard time with some basic concepts but I'm finding that the more I actually code instead of reading about, the more I improve.

    Anyway, thanks for your excellent input Dan.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •