I made a little voting system for school and I have a problem when a question doesn't have 4 answers. I want the html form to show only the buttons with a value. Screenshot of the voting system
Button 3 and 4 are not defined. Only button 1 and 2 should be visible and clickable.
That's my code:
<form role="form" class="form-inlinecy" action="VotingVoting.php" method="post">
<div class="form-group">
<input type="submit" name="a" id="a" value="<?php echo $voting->a ?>"/>
</div>
<div class="form-group">
<input type="submit" name="b" id="b" value="<?php echo $voting->b ?>"/>
</div>
<div class="form-group">
<input type="submit" name="c" id="c" value="<?php echo $voting->c ?>"/>
</div>
<div class="form-group">
<input type="submit" name="d" id="d" value="<?php echo $voting->d ?>"/>
</div>
</form>
Doesn't sound like a complicated thing to do, but I can't find answers here. Thanks so much!
Greetings Timo
Use PHP Script to check
<?php if(isset($voting->a) && !empty($voting->a))
{ ?>
<div class="form-group">
<input type="submit" name="a" id="a" value="<?php echo $voting->a ?>"/>
</div>
<?php } ?>
follow the same for all four input fields
You have multiples ways to do it, one could be to add a condition around the input, like this :
<?php if($voting->b != ""){ ?>
<div class="form-group">
<input type="submit" name="b" id="b" value="<?php echo $voting->b ?>"/>
</div>
<?php } ?>
Or the CSS way :
input[value=""] { display: none; }
You can use PHP conditions as follows to hide the last two buttons if they're not set.
<form role="form" class="form-inlinecy" action="VotingVoting.php" method="post">
<div class="form-group">
<input type="submit" name="a" id="a" value="<?php echo $voting->a ?>"/>
</div>
<div class="form-group">
<input type="submit" name="b" id="b" value="<?php echo $voting->b ?>"/>
</div>
<?php if (isset($voting->c)) { ?>
<div class="form-group">
<input type="submit" name="c" id="c" value="<?php echo $voting->c ?>"/>
</div>
<?php } ?>
<?php if (isset($voting->d)) { ?>
<div class="form-group">
<input type="submit" name="d" id="d" value="<?php echo $voting->d ?>"/>
</div>
<?php } ?>
</form>
You can do it using if condition as follow
<?php if(isset($voting->a) && !empty($voting->a)){ ?>
<div class="form-group">
<input type="submit" name="a" id="a" value="<?php echo $voting->a ?>"/>
</div>
<?php } ?>
This should work :
<form role="form" class="form-inlinecy" action="VotingVoting.php" method="post">
<div class="form-group">
<?php
if($voting->a <> '') { echo '<input type="submit" name="a" id="a" value="'.$voting->a.'"/>';}
?>
</div>
<div class="form-group">
<?php
if($voting->b <> '') { echo '<input type="submit" name="b" id="b" value="'.$voting->b.'"/>';}
?>
</div>
<div class="form-group">
<?php
if($voting->c <> '') { echo '<input type="submit" name="c" id="c" value="'.$voting->c.'"/>';}
?>
</div>
<div class="form-group">
<?php
if($voting->d <> '') { echo '<input type="submit" name="d" id="d" value="'.$voting->d.'"/>';}
?>
</div>
</form>
try this
<form role="form" class="form-inlinecy" action="VotingVoting.php" method="post">
<div class="form-group">
<?php
if($voting->a != '') { echo '<input type="submit" name="a" id="a" value="'.$voting->a.'"/>';}
?>
</div>
<div class="form-group">
<?php
if($voting->b != '') { echo '<input type="submit" name="b" id="b" value="'.$voting->b.'"/>';}
?>
</div>
<div class="form-group">
<?php
if($voting->c != '') { echo '<input type="submit" name="c" id="c" value="'.$voting->c.'"/>';}
?>
</div>
<div class="form-group">
<?php
if($voting->d != '') { echo '<input type="submit" name="d" id="d" value="'.$voting->d.'"/>';}
?>
</div>
</form>
</div>
your class structure in general is not very good but for your example
<form role="form" class="form-inlinecy" action="VotingVoting.php" method="post">
<?php
foreach (['a','b','c','d'] as $id) {
if (empty($voting->$id)) continue;?>
<div class="form-group">
<input type="submit" name="<?php echo $id;?>" id="<?php echo $id;?>" value="<?php echo $voting->$id; ?>"/>
</div>
<?php } ?>
</form>