Reputation: 163
I have a site that has different features but I do not want a feature to show if it does not have a variable assigned to it.
For example, my product has a featured list like this:
<h3>Specifications</h3>
<ul>
<li class="element element-text first">
<strong>Manufacturer: </strong><?php echo $manufactuer;?>
</li>
<li class="element element-text">
<strong>Model #: </strong><?php echo $model;?>
</li>
<li class="element element-text">
<strong>Previously Ran: </strong><?php echo $ran;?>
</li>
<li class="element element-text last">
</ul>
On the front end, I don't want the Model
line displayed if that doesn't have a variable assigned to it.
Upvotes: 0
Views: 244
Reputation: 93
Full code like this:
<h3>Specifications</h3>
<ul>
<?php if(isset($manufacturer) && !empty($manufacturer)): ?>
<li class="element element-text first">
<strong>Manufacturer: </strong><?php echo $manufactuer;?>
</li>
<?php endif; ?>
<?php if(isset($model) && !empty($model)): ?>
<li class="element element-text">
<strong>Model #: </strong><?php echo $model;?>
</li>
<?php endif; ?>
<?php if(isset($ran) && !empty($ran)): ?>
<li class="element element-text">
<strong>Previously Ran: </strong><?php echo $ran;?>
</li>
<?php endif; ?>
<li class="element element-text last"></li>
</ul>
Upvotes: 0
Reputation: 41
<?php if (isset($model) && !empty($model)) : ?>
<li class="element element-text">
<strong>Model #: </strong>
<?php echo $model;?>
</li>
<?php endif; ?>
Upvotes: 1
Reputation: 291
You need to use JavaScript to write the HTML when you start the page. You probably want something like this (make sure you have a script called JQuery, you can find it online):
$.ajax({ url: '/my/site.php',
datatype: "JSON", /* Personal preference */
type: 'get',
cache: false,
success: function(data) {
alert(output);
if(data[1] != null){
document.getElementById('ID').innerHTML = "<li>" + data[1] + "</li>";
} else {
console.log("nothing found");
}
}
});
You may need to adjust things a bit for your particular code writing, but this would be the approach I think you need. In general, dynamic elements (ones that are not static i.e. ones that respond to stuff outside the HTML) need to dealt with in JavaScript.
Upvotes: 1
Reputation: 7152
You can do,
<?php echo (!empty($model))?$model:'';?>
Read up on: empty, and ternary operator
Upvotes: 0
Reputation: 3156
you can do this
<?php if(isset($model)): ?>
<li class="element element-text">
<strong>Model #: </strong><?php echo $model;?>
</li>
<?php endif; ?>
Upvotes: 0