Jump to content

Recommended Posts

How do I do a value check for an foreach loop? What I'm trying to do is declair if product_id = 10 then do this action else do this inside of the foreach loop where the link is located.

Example

<?php

$product_id = $_GET['product_id'];

 

if ( $product_id == 10 ) {

echo "<a href="product_custom_1";

} else {

echo "<a href="product_details.php?product_id=<?php echo $row['product_id']; ?>">";<----- how do I pull the product id inside of foreach?

}

 

<form class="add_product" method="post" action="">
 <?php foreach ($products as $row): ?>
					    <li class="first">
						 <a href="product_details.php?product_id=<?php echo $row['product_id']; ?>">
   <?php if (is_null($row['product_thumbnail'])): ?>
    <img src="?image=no_image.png" width="160" height="200" alt="" />
   <?php else: ?>
    <img src="?image=<?php echo $row['product_thumbnail']; ?>" width="160" height="200" alt="" />
   <?php endif; ?>
  </a>
						    <div class="clear"></div>
						    <h6><center><?php echo $row['product_name']; ?></center></h6>
						    <div class="clear"></div>
						    <p><center>Unit Price: <?php echo price($row['product_price']); ?></center></p>
						    <div class="clear"></div>
<center><input type=button onclick="parent.location='product_details.php?product_id=<?php echo $row['product_id']; ?>'" class="buttonaddtocart2" value='Read More'>
</center>

					    </li>


 <?php endforeach; ?>

Link to comment
https://forums.phpfreaks.com/topic/272472-foreach-loop-if-value/
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.