Jump to content

php efficient loop


jarvis

Recommended Posts

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

Link to comment
Share on other sites

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 
}
}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 
}
}
?>

  :'(

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.