wigglesby Posted December 4, 2009 Share Posted December 4, 2009 Hi I have a for loop, which does a count and outputs an unordered list only if the criteria in the loop is made: <?php echo '<div id="box_info_downloads">'; echo '<h2>downloads</h2>'; echo '<div id="files">'; echo '<ul class="attached_files">'; $limit = 4; for ($i=1; $i<= $limit ;$i++) { $file = call_user_func(array($facility, 'getFile'.$i)); $filedesc = call_user_func(array($facility, 'getFileDesc'.$i)); if(empty($file)){ echo ' '; } else{ $img = image_tag('pdf.jpg', array('height'=>'30', 'width'=>'30')); $filedesc = ( $filedesc !== '' ) ? $filedesc : 'Download File'; echo "<br /><li><a href=\"/preview/uploads/facilities/{$file}\">{$img}</a> <br /> {$filedesc}<br /></li>"; } }?> Now at the moment, that will display the <div id="box_info_downloads"> even if no records are found. I want to have the <div> only displayed if the for loop is successful at with its parameters. How would I do that? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/184007-ifelse-withiin-a-for-loop/ Share on other sites More sharing options...
premiso Posted December 4, 2009 Share Posted December 4, 2009 <?php $limit = 4; $output = ""; for ($i=1; $i<= $limit ;$i++) { $file = call_user_func(array($facility, 'getFile'.$i)); $filedesc = call_user_func(array($facility, 'getFileDesc'.$i)); if(empty($file)){ $output .= ' '; }else{ $img = image_tag('pdf.jpg', array('height'=>'30', 'width'=>'30')); $filedesc = ( $filedesc !== '' ) ? $filedesc : 'Download File'; $output .= "<br /><li><a href=\"/preview/uploads/facilities/{$file}\">{$img}</a> <br /> {$filedesc}<br /></li>"; } } $style = ""; if (strstr($output, "preview/uploads") === False) { $style = 'style="display:none;"'; } $output = '<div id="box_info_downloads" ' . $style . '> <h2>downloads</h2> <div id="files"> <ul class="attached_files">' . $output; echo $output; ?> That is one way to do it, generally you do not want to output stuff in the middle of processing scripts, it is generally better to store it in a variable and echo it after the script is processing as it can screw with header and make them not work. Hopefully that is what you were after. EDIT: Fixed a syntax error (added semicolon after $output = "") Quote Link to comment https://forums.phpfreaks.com/topic/184007-ifelse-withiin-a-for-loop/#findComment-971458 Share on other sites More sharing options...
wigglesby Posted December 4, 2009 Author Share Posted December 4, 2009 Thank you for the help. Unfortunately that didnt work. It still displays the <div> when they are no files associated. Quote Link to comment https://forums.phpfreaks.com/topic/184007-ifelse-withiin-a-for-loop/#findComment-971472 Share on other sites More sharing options...
premiso Posted December 4, 2009 Share Posted December 4, 2009 Can you post the output that is displayed when there are "no files associated" and the output when there are "files associated". I think that will help me figure out what you are after and how to get it to work for ya. Quote Link to comment https://forums.phpfreaks.com/topic/184007-ifelse-withiin-a-for-loop/#findComment-971477 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.