Reputation: 1
I am new and practising. So, I am sure I am not doing this the best way possible.
I am trying to get the product data to show if there is data, but if there isn't any data, I want the button to be hidden. In the DB, only about 40% actually have product data.
foreach ($part->results as $index=>$row){
echo '<a href="#" class="btn btn-primary"> Add to Cart</a>
<div class="btn-group">
<button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> Info
</button>
<div class="dropdown-menu">
<a class="btn btn-info " href="./media/products/product_data/'.$row["product_data"].'" target="_blank"class="btn btn-info"> Details </a>
</div>'
Upvotes: 0
Views: 57
Reputation: 1525
You're probably looking for the empty() function. I cleaned your code a bit, but it would look something like this:
<?php foreach ($part->results as $index=>$row) : ?>
<a href="#" class="btn btn-primary">Add to Cart</a>
<div class="btn-group">
<button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">Info</button>
</div>
<?php if (!empty($row["product_data"])): ?>
<div class="dropdown-menu">
<a class="btn btn-info " href="./media/products/product_data/>
<?= $row["product_data"]; ?>" target="_blank"class="btn btn-info">Details</a>
</div>;
<?php endif; ?>
<?php endforeach; ?>
This would not output the <div class="dropdown-menu">
section for any that were missing product-data. You could move the if-condition somewhere else to omit other output.
If you want to skip the whole row, use continue in your existing code:
foreach ($part->results as $index=>$row){
// this will skip the loop whenever product-data is missing
if(empty($row["product_data"]))
continue;
echo '<a href="#" class="btn btn-primary"> Add to Cart</a>
<div class="btn-group">
<button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> Info
</button>
</div>
<div class="dropdown-menu">
<a class="btn btn-info " href="./media/products/product_data/'.$row["product_data"].'" target="_blank"class="btn btn-info"> Details </a>
</div>'
?>
If you still want output and then hide it with CSS, @JackofSpades has the right answer.
Upvotes: 0
Reputation: 104
Use CSS and classes to show / hide elements on the page. In the css:
.hidden {
display: none;
}
Then you can echo this via php:
foreach ($part->results as $index=>$row){
$hidden = empty($row["product_data"]) ? ' hidden' : ''; //determine if hidden needs to be added or not
echo '<a href="#" class="btn btn-primary"> Add to Cart</a>
<div class="btn-group">
<button type="button' . $hidden . '" class="btn btn-info dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> Info
</button>
<div class="dropdown-menu">
<a class="btn btn-info " href="./media/products/product_data/'.$row["product_data"].'" target="_blank"class="btn btn-info"> Details </a>
</div>'
...
Upvotes: 1