jarvis Posted June 24, 2011 Share Posted June 24, 2011 Hi All, I must be missing a trick here. I'm using the following block of code to show navigation. It tests to see if a particular field is blank, if it is, it won't show the link. Rather than repeating the block of code and numbering each one, what would be the best way to check the field in a loop? Here's the code: <?php $Banner_Image = get_post_meta($post->ID, 'Banner_Image', true); ?> <?php if ($Banner_Image!='') : ?> <a href="javascript:goto('.item1')">1</a> <?php endif; ?> <?php $Banner_Image_2 = get_post_meta($post->ID, 'Banner_Image_2', true); ?> <?php if ($Banner_Image_2!='') : ?> <a href="javascript:goto('.item2')">2</a> <?php endif; ?> <?php $Banner_Image_3 = get_post_meta($post->ID, 'Banner_Image_3', true); ?> <?php if ($Banner_Image_3!='') : ?> <a href="javascript:goto('.item3')">3</a> <?php endif; ?> <?php $Banner_Image_4 = get_post_meta($post->ID, 'Banner_Image_4', true); ?> <?php if ($Banner_Image_4!='') : ?> <a href="javascript:goto('.item4')">4</a> <?php endif; ?> <?php $Banner_Image_5 = get_post_meta($post->ID, 'Banner_Image_5', true); ?> <?php if ($Banner_Image_5!='') : ?> <a href="javascript:goto('.item5')">5</a> <?php endif; ?> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/240301-php-efficient-loop/ Share on other sites More sharing options...
gristoi Posted June 24, 2011 Share Posted June 24, 2011 <?php for ($i=1; $i<6; $i++){ $Banner_Image = get_post_meta($post->ID, "Banner_Image_$i", true); if ($Banner_Image_$i !=''){ ?> <a href="javascript:goto('.item<?php echo $i; ?>')">1</a> <?php } } Quote Link to comment https://forums.phpfreaks.com/topic/240301-php-efficient-loop/#findComment-1234280 Share on other sites More sharing options...
AMcHarg Posted June 24, 2011 Share Posted June 24, 2011 Correction to the above <?php for ($i=1; $i<6; $i++) { $Banner_Image = get_post_meta($post->ID, "Banner_Image_$i", true); if ($Banner_Image_$i !='') { ?> <a href="javascript:goto('.item<?php echo $i; ?>')"><?php echo $i; ?></a> <?php } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/240301-php-efficient-loop/#findComment-1234285 Share on other sites More sharing options...
AMcHarg Posted June 24, 2011 Share Posted June 24, 2011 Incidentally: using so many <?php and ?> is horrible to code with. :-\ Quote Link to comment https://forums.phpfreaks.com/topic/240301-php-efficient-loop/#findComment-1234287 Share on other sites More sharing options...
gristoi Posted June 24, 2011 Share Posted June 24, 2011 cheers AMcHarg. i spotted that after i posted. was in a rush lol Quote Link to comment https://forums.phpfreaks.com/topic/240301-php-efficient-loop/#findComment-1234288 Share on other sites More sharing options...
jarvis Posted June 24, 2011 Author Share Posted June 24, 2011 Thank you, that's so helpful! Quote Link to comment https://forums.phpfreaks.com/topic/240301-php-efficient-loop/#findComment-1234290 Share on other sites More sharing options...
jarvis Posted June 24, 2011 Author Share Posted June 24, 2011 Sorry to trouble you again, the above gives the following message: Parse error: syntax error, unexpected T_VARIABLE The line it refers to is this one: if ($Banner_Image_$i !='') { I can't see why this would cause an issue though? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/240301-php-efficient-loop/#findComment-1234296 Share on other sites More sharing options...
AMcHarg Posted June 24, 2011 Share Posted June 24, 2011 Ah yes, human spots some of the errors; computer spots them all. Try: <?php for ($i=1; $i<6; $i++) { $Banner_Image[$i] = get_post_meta($post->ID, "Banner_Image_$i", true); if ($Banner_Image[$i] !='') { ?> <a href="javascript:goto('.item<?php echo $i; ?>')"><?php echo $i; ?></a> <?php } } ?> :'( Quote Link to comment https://forums.phpfreaks.com/topic/240301-php-efficient-loop/#findComment-1234298 Share on other sites More sharing options...
jarvis Posted June 24, 2011 Author Share Posted June 24, 2011 Sorry AMcHarg I can't seem to see your suggestion, it appears blank? Quote Link to comment https://forums.phpfreaks.com/topic/240301-php-efficient-loop/#findComment-1234299 Share on other sites More sharing options...
jarvis Posted June 24, 2011 Author Share Posted June 24, 2011 Hi, Needed a slight amend but thanks to your code, it now works. Here's the final code: <?php for ($i=1; $i<6; $i++) { $Banner_Image_[$i] = get_post_meta($post->ID, "Banner_Image_$i", true); if ($Banner_Image_[$i]!='') :?> <a href="javascript:goto('.item<?php echo $i; ?>')"><?php echo $i; ?></a> <?php endif; } ?> Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/240301-php-efficient-loop/#findComment-1234301 Share on other sites More sharing options...
cyberRobot Posted June 24, 2011 Share Posted June 24, 2011 Note that the code could look cleaner by doing something like: <?php if($Banner_Image_[$i]!='') { echo "<a href=\"javascript:goto('.item$i')\">$i</a>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/240301-php-efficient-loop/#findComment-1234302 Share on other sites More sharing options...
AMcHarg Posted June 24, 2011 Share Posted June 24, 2011 lol cyberRobot, exactly how I would have done it. I hate all this <php and ?> stuff. Quote Link to comment https://forums.phpfreaks.com/topic/240301-php-efficient-loop/#findComment-1234305 Share on other sites More sharing options...
jarvis Posted June 24, 2011 Author Share Posted June 24, 2011 Thanks cyberRobot, I'll tidy it after. I'm trying to apply the above code to this loop as well: <?php $Banner_Image = get_post_meta($post->ID, 'Banner_Image', true); ?> <?php if ($Banner_Image!='') : ?> <li class="item1"> <?php $Banner_Image = get_post_meta($post->ID, 'Banner_Image', true); echo wp_get_attachment_image($Banner_Image, 'large'); ?> </li> <?php endif; ?> <?php $Banner_Image_2 = get_post_meta($post->ID, 'Banner_Image_2', true); ?> <?php if ($Banner_Image_2!='') : ?> <li class="item2"> <?php $Banner_Image_2 = get_post_meta($post->ID, 'Banner_Image_2', true); echo wp_get_attachment_image($Banner_Image_2, 'large'); ?> </li> <?php endif; ?> This is what I've amended it to: <?php for ($i=1; $i<6; $i++) { $Banner_Image_[$i] = get_post_meta($post->ID, "Banner_Image_$i", true); if ($Banner_Image_[$i]!='') :?> <li class="item<?php echo $i; ?>"> <?php $Banner_Image_[$i] = get_post_meta($post->ID, 'Banner_Image_$i', true); echo wp_get_attachment_image($Banner_Image_[$i], 'large'); ?> </li> <?php endif; } ?> I thought I was being smug, instead, I think my code has officially b**ch slapped me as it won't show the images :-( Have I missed something obvious? Thanks again everyone Quote Link to comment https://forums.phpfreaks.com/topic/240301-php-efficient-loop/#findComment-1234306 Share on other sites More sharing options...
cyberRobot Posted June 24, 2011 Share Posted June 24, 2011 Looks like it might be an issue with using single quotes instead of double. Change this: <?php $Banner_Image_[$i] = get_post_meta($post->ID, 'Banner_Image_$i', true); To this: <?php $Banner_Image_[$i] = get_post_meta($post->ID, "Banner_Image_$i", true); Quote Link to comment https://forums.phpfreaks.com/topic/240301-php-efficient-loop/#findComment-1234309 Share on other sites More sharing options...
jarvis Posted June 24, 2011 Author Share Posted June 24, 2011 Holy cow! Yeah that solved it! So I know & understand, why is that? Would rather learn for next time than rely on others to bail me out Thanks Quote Link to comment https://forums.phpfreaks.com/topic/240301-php-efficient-loop/#findComment-1234311 Share on other sites More sharing options...
cyberRobot Posted June 24, 2011 Share Posted June 24, 2011 Actually, after taking a closer look, you seem to be duplicating code. Is there a reason why you're doing the $Banner_Image_[$i] assignment twice? You should be able to remove the second one in the if statement. Quote Link to comment https://forums.phpfreaks.com/topic/240301-php-efficient-loop/#findComment-1234312 Share on other sites More sharing options...
cyberRobot Posted June 24, 2011 Share Posted June 24, 2011 Holy cow! Yeah that solved it! So I know & understand, why is that? Would rather learn for next time than rely on others to bail me out Thanks When using double quotes, things like variables will be interpreted by PHP. On the other hand, PHP displays the string as is if you use single quotes. Quote Link to comment https://forums.phpfreaks.com/topic/240301-php-efficient-loop/#findComment-1234316 Share on other sites More sharing options...
jarvis Posted June 24, 2011 Author Share Posted June 24, 2011 Thanks cyberRobot. Will also review my code for $Banner_Image_[$i] as it doesn't need to be assigned twice Thanks Quote Link to comment https://forums.phpfreaks.com/topic/240301-php-efficient-loop/#findComment-1234320 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.