Reputation: 77
I am trying to hide a "add to table" button once ONE (basically, if the table is not empty) input has been added to the table.
I tried using a IF statement but with no luck.
Since i am a begginer in this and need to do this for my homework, i would be very grateful, if you could atleast point me into the right direction.
Regards.
This is my code. I have a script that toggles a contact form trough a button click. I would like that button to dissapear if the table IS NOT empty. So 1 input = button gone. I am only addint the relavant part of the code.
EDIT: FINAL CODE.
<?php
$koncnica=".png";
$conn = mysqli_connect("localhost", "username", "pass", "DB");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT debelina, odkat, rob, vtor, id, narocilo FROM polica where id='$id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" .
$row["debelina"] ." cm".'<a href="urediNarocilo.php?id=' . $row['id'] . '&uredi=debelina&narocilo='. $row['narocilo'] . '&value='.$row['debelina'].'"> ✎</a>'. "</td><td>" .
$row["odkat"].'<a href="urediNarocilo.php?id=' . $row['id'] . '&uredi=odkat&narocilo='. $row['narocilo'] . '&value='.$row['odkat'].'"> ✎</a>'. "</td><td>" .
$row["rob"].' <img src ="http://granital.owjej.org/wp-content/uploads/2019/03/' . $row['rob'] . $koncnica.'"height="20px"/><a href="uredizunanjo.php?id=' . $row['id'] . '&uredi=rob&narocilo='. $row['narocilo'] . '&value='.$row['rob'].'">✎</a>'.
"</td></tr>";}
echo "</table>";
}
$conn->close();
?>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#a').click(function() {
$('.b').toggle("slide");
});
});
</script>
<?php if ($result->num_rows == 0) { ?>
<div id="a">Dodaj</div>
<?php } ?>
<?php
$conn->close();
?>
<div class="b" style="display: none;">
<div class="container">
FORM
</div>
</div>
This does the trick :)
<?php if ($result->num_rows == 0) { ?>
<div id="a">Dodaj</div>
<?php } ?>
Upvotes: 1
Views: 571
Reputation: 61819
You can wrap the "button" HTML in some PHP which will cause it only to be rendered if the table has no items in it:
<?php if ($result->num_rows == 0) { ?>
<div id="a">Dodaj</div>
<?php } ?>
Upvotes: 2