0

I'm writing a website for myself right now. I've got a problem that I cannot solve. The website is about buying files with website custom balance. What I want to do is disable buy button if user bought this file b4. I'm trying to check the data inside a while but idk how?? This is what I.

               while ($row = $rocket->fetch())
              {
              ?>
                <tr>
                  <td><?php echo $row['id']; ?></td>
                  <td><?php echo $row['name']; ?></td>
                  <td><?php echo $row['uploader']; ?></td>
                  <td><?php echo $row['uploadtime']; ?></td>
                  <td><button <?php 
                  if ($row['id'] == $row2['file_id']) // <---- Working only for the first since it's not a loop
                  {
                    echo "disabled";
                  }
                  ?>
                  ><a href="download.php?id=<?php echo $row['id']; ?>"></a><?php echo $row['price'] . "D$" ?></button></td> 
                </tr>
              <?php } ?>

I tried to do

$row['id'] == $row2['file_id'];

but eventually it's checking only the first line. Maybe the problem is easily solvable, as a newbie cannot find the solution. There are the queries:

  $rocket = $bdd->prepare('SELECT money FROM users WHERE id = ?');
$rocket->execute(array($_SESSION['id']));
$row = $rocket->fetch();
$rocket->closeCursor();

$rocket = $bdd->query('SELECT * FROM files');
$rocket2 = $bdd->prepare('SELECT * FROM user_files WHERE user_id = ?');
$rocket2->execute(array($uid));
$row2 = $rocket2->fetch();

What I want to do is check in every $row, is the $row['file_id'] present.

loverr9
  • 3
  • 2
  • what is id_file? – NoobDEV-GBL Dec 10 '19 at 17:08
  • Where does `$row2` come from? Is that something that's set before the loop and doesn't change during the loop? If so, it seems like what you tried should work. Can you show a version of the code that actually includes what you tried with `$row['id'] == $row2['file_id']`? – Don't Panic Dec 10 '19 at 17:08
  • Updated the post – loverr9 Dec 10 '19 at 17:30
  • So `$row2['file_id']` only contains one id, right? It makes sense that it would only match one row from the other query. I think you might be able to do this with one query by joining the two tables. Can you show the table structures and a small example of some of the data? – Don't Panic Dec 10 '19 at 17:40
  • The file_id can contain more ids, at the moment when there are more than 1 id, it disables only the first button and let the others work. Here is the table that stocks the [$row2](https://imgur.com/a/7FywGBl) and the table where is stocked [$row](https://imgur.com/a/7AkVcje) – loverr9 Dec 10 '19 at 17:54

2 Answers2

0

You could do something like this :

while ($row = $rocket->fetch())
              {
              ?>
                <tr>
                  <td><?php echo $row['id']; ?></td>
                  <td><?php echo $row['name']; ?></td>
                  <td><?php echo $row['uploader']; ?></td>
                  <td><?php echo $row['uploadtime']; ?></td>
                  <td>
                    <?php
                    $classdis= "";   
                    if($row['id'] == $row2['file_id']) {
                    $classbtndis = "disabled";
                     }
                      ?>                
                <button <?php echo $classbtndis ? $classbtndis : "" ?>
                  ><a href="download.php?id=<?php echo $row['id']; ?>"></a><?php echo $row['price'] . "D$" ?></button></td> 
                </tr>
              <?php } ?>
Krishna Savani
  • 919
  • 4
  • 10
0

I think you can do this with one query if you left join user_files to files

SELECT f.*, uf.user_id
FROM files f LEFT JOIN user_files uf ON f.id = uf.file_id
WHERE uf.user_id IS NULL OR uf.user_id = ?

This will fetch all the rows from files, and each file the user already has will have an associated record from user_files attached.

So when you're outputting the data, just check for a user_id in the row. If it's not null, then the user has that file and you can disable the button.

if ($row['user_id']) {
    echo "disabled";
}

You can get the same query result using a subquery:

SELECT f.*, uf.user_id
FROM files f LEFT JOIN
(SELECT * FROM user_files uf WHERE user_id = ?) uf ON f.id = uf.file_id

That may be faster than the other way, but I'm not enough of an SQL pro to say for sure of the top of my head.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • Tried ur code, got this: Fatal error: Uncaught Error: Call to a member function execute() and Call to a member function execute() on boolean, when binding the userid variable to ?. – loverr9 Dec 10 '19 at 19:27
  • Hmm, sounds like the prepare failed. The query should work in theory (small example here: http://sqlfiddle.com/#!9/f72a97/7/0). See if you can find what error mysql is giving you. The answer here is good for getting at those errors: https://stackoverflow.com/questions/32648371/my-pdo-statement-doesnt-work – Don't Panic Dec 10 '19 at 19:31
  • Did the etaps from thread u give me. [Errors I got](https://i.imgur.com/tlOGzHm.png) Also I writed my id manually a the place of '?' symbol and it worked. – loverr9 Dec 10 '19 at 19:46
  • Based on those errors, are you using that SQL with `query()`, or `prepare()`? I was meaning for it to be a prepared statement. – Don't Panic Dec 10 '19 at 20:28
  • Yeah I'm always using prepared statements. – loverr9 Dec 10 '19 at 21:19