boredd Posted December 9, 2019 Share Posted December 9, 2019 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"; Quote Link to comment https://forums.phpfreaks.com/topic/309661-disable-button-if-value-exists-or-equal-to-array/ Share on other sites More sharing options...
Barand Posted December 9, 2019 Share Posted December 9, 2019 What is the query to get $row? What is the query to get $row2? (If you used "SELECT * ' the provide the table structures too) Quote Link to comment https://forums.phpfreaks.com/topic/309661-disable-button-if-value-exists-or-equal-to-array/#findComment-1572393 Share on other sites More sharing options...
boredd Posted December 9, 2019 Author Share Posted December 9, 2019 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 Quote Link to comment https://forums.phpfreaks.com/topic/309661-disable-button-if-value-exists-or-equal-to-array/#findComment-1572395 Share on other sites More sharing options...
Barand Posted December 9, 2019 Share Posted December 9, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/309661-disable-button-if-value-exists-or-equal-to-array/#findComment-1572397 Share on other sites More sharing options...
Barand Posted December 9, 2019 Share Posted December 9, 2019 (edited) 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 December 9, 2019 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/309661-disable-button-if-value-exists-or-equal-to-array/#findComment-1572398 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.