dsbpac Posted December 29, 2012 Share Posted December 29, 2012 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/272472-foreach-loop-if-value/ Share on other sites More sharing options...
kicken Posted December 29, 2012 Share Posted December 29, 2012 The variable containing the product ID is $row['product_id'], so just use that as your if condition. <?php if ($row['product_id'] == 10): ?> something <?php else: ?> something else <?php endif; ?> Quote Link to comment https://forums.phpfreaks.com/topic/272472-foreach-loop-if-value/#findComment-1401972 Share on other sites More sharing options...
dsbpac Posted December 29, 2012 Author Share Posted December 29, 2012 Worked like a charm, TYVM!! Your awesome! Quote Link to comment https://forums.phpfreaks.com/topic/272472-foreach-loop-if-value/#findComment-1402030 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.