Jump to content

Recommended Posts

Hi, I am a newbie in PHP (about week and a half). 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. The is a table user_files that stores user id and file id.

And another table that stores files data, ID name who uploaded date time ect....

What I'm trying to do, is disabling buy button if the user has already the file.

This is what I've done :


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 
                      // Echo "disabled" if $row['id'] id is present in row2['file_id'] <<<<<<< Need to do this in a while.
                      ?>
                      ><a href="download.php?id=<?php echo $row['id']; ?>"></a><?php echo $row['price'] . "D$" ?></button></td> 
                    </tr>
                  <?php } ?>

What I can't do is check if the file id ($row['id']) is present in ($row2['file_id']), if yes echo "disabled";

 

1 minute ago, Barand said:

What is the query to get $row?

What is the query to get $row2?

(If youused "SELECT * ' the provide the table structures too)

All query :

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

Files table structure

user_files table structure

Have you tried recreating  test files in your database using images as input?

SHOW CREATE TABLE files;
SHOW CREATE TABLE user_files;

Use a single query on both tables and use a LEFT JOIN. That way you know which are present in both tables. Disable the button where the file is in both.

Here's a simplified example

$uid = 2;

$res = $bdd->prepare("SELECT f.id
                           , f.name
                           , u.file_id
                      FROM files f 
                             LEFT JOIN
                           user_files u 
                                ON f.id = u.file_id
                                AND u.user_id = ?
                      ORDER BY f.id
                    ");
$res->execute([$uid]);

$tdata = '';
foreach ($res as $row) {
    $disabled = $row['file_id'] ? 'disabled' : '';
    $tdata .= "<tr>
                 <td>{$row['id']}</td>
                 <td>{$row['name']}</td>
                 <td><button $disabled>Download</button></td>
               </tr> 
              ";
}
?>
<html>
<head>
<title>Example</title>
</head>
<body>
    <table>
        <?=$tdata?>
    </table>
</body>
</html>
      

 

Edited by Barand
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.