0

I need to disable or hide, button if existsusername in the table, and == logged in user

For example: username John exists in table paym we should disable button to John

table: paym

 ID        username        column1      column2  
+-------+-------------+-------------+-----------+
|   1   |  John       |  Value      |    Value  |
+-------+-------------+-------------+-----------+
|   2   |  Alex       |  Null       |    Null   |
+-------+-------------+-------------+-----------+

Only infrmation that I can provide you:((

This is button in Html:

 <input class="reload-but" type="button" value="↻"">

** logic behind similar to this php code:**

  <?php
     $user_check_query = "SELECT * FROM paym
 WHERE username = '" . $_SESSION['username'] . "' ";
      $result = mysqli_query($db, $user_check_query);
      $row = mysqli_fetch_array($result, MYSQLI_ASSOC);

  ?>

Who can do fully workable code, with explained code!

Related topic to this!!

anonymous
  • 119
  • 1
  • 10

3 Answers3

0

1 method is that you can populate the table in Php by echoing the Table in HTML.

Another method would be to use a for loop in ur html looping through your data and do an if else in the loop to check the existence of the username. If true, use this set of button codes, if false, use this set of button codes.

0
echo "<input class=\"reload-but\" type=\"button\" value=\"↻\"".(count($row) > 0 ? " disabled" : " ".">";

Or, as @Justinas said, you can just echo the button only when you have a positive result.

Autruche
  • 67
  • 1
  • 6
  • Thanks for your reply, Can you expain your code fully with php code?? I did't Understand where to put your code:( – anonymous Apr 12 '19 at 07:37
  • If 'John' is in the table, your $rows will have values. So, you can use this condition later. Instead of putting your button with pure html, you can use a php code (with echo) to add it to your page, and if John isnt in the table, then you add the property "disabled" to your button, so he cannot click on it. – Autruche Apr 12 '19 at 10:06
  • @Autruche OP's code does not have any variable called `$rows`. First you'd have to show how to create and populate that. – ADyson Apr 12 '19 at 10:41
  • ok. But $row is an array representing a single row of the table. So yes using count() will return 0 if $row is null (because the query did not return any rows), and the code will work as required, but...semantically it's an odd way to check the query output, when you could just use the mysqli_num_rows() function intended for the purpose, instead of mysqli_fetch_array(). It looks like you're trying to count the number of columns in the row, and the real intent is not immediately obvious to someone reading the code. – ADyson Apr 12 '19 at 12:58
0

The code snippet is a little bit out of context, so it's hard to know if this is possible, but assuming your PHP and HTML are part of the same script file (or least the one with the HTML includes/requires the one with the database code), then you can do something simple like this:

<?php
  $user_check_query = "SELECT * FROM paym WHERE username = '" . $_SESSION['username'] . "' ";
  $result = mysqli_query($db, $user_check_query);
  $showButton = (mysqli_num_rows ($result) > 0 ? true : false);
?>

...

<?php
  if ($showButton == true) {
?>
    <input class="reload-but" type="button" value="↻"">
<?php
  }
?>

This will cause PHP to only send the button HTML to the browser when the if condition is true.

ADyson
  • 57,178
  • 14
  • 51
  • 63