Take a picture.
Using this query, you will get a "applyFor" column with each job. If it is 1, the user has applied for the task. If it is 0, the user did not apply for the task.
SELECT jobs.*, (COALESCE(bids.bidId, 0) > 0) AS appliedFor FROM jobs LEFT JOIN bids ON bids.jobID = jobs.jobID AND bids.userID = 1;
Before explaining, see the test here: http://sqlfiddle.com/#!9/eb5a4f/1/0
It is pretty simple ...
- Just make a
LEFT JOIN in the betting table with jobID and userID . This means that if there is a corresponding row bids.bidID in the betting table, bids.bidID will be numeric, otherwise it will be NULL . - Then I did
COALESCE in this column if the NULL value changed to 0 . - Insert the brackets in the brackets and make sure that the result is greater than
0 . This gives you a boolean , which may be an alias for your use as a result of the request.
I tested using the following circuit:
CREATE TABLE jobs ( jobID int NOT NULL AUTO_INCREMENT, PRIMARY KEY (jobID) ); CREATE TABLE bids ( bidID int NOT NULL AUTO_INCREMENT, userID int, jobID int, PRIMARY KEY (bidID) ); INSERT INTO jobs () VALUES(); INSERT INTO jobs () VALUES(); INSERT INTO jobs () VALUES(); INSERT INTO bids (userID, jobID) VALUES(1, 1); INSERT INTO bids (userID, jobID) VALUES(2, 1); INSERT INTO bids (userID, jobID) VALUES(1, 3);
The PHP code corresponding to this will look something like this:
<?php foreach ($jobs as $job){ $description = $job['description']; $jobDescription = substrwords($description, 30); $buttonText = "Apply"; if ($job['appliedFor']) { <div><?php echo $job['headline']; ?></div> <div><?php echo $jobDescription; ?></div> <div><?php echo $job['datePosted']; ?></div> <div><?php echo $job['amount']; ?></div> <div><?php echo $job['location']; ?></div> <div class="jobPosting"> <input type="text" value="apply" name="action" style="display: none" /> <button name="placeBid" type="submit" class="btn btn-primary" value="<?php echo $job['jobID']; ?>"><?php echo $buttonText; ?></button> </div> <hr style="border: dotted thin #eeefff" /> <?php }
Ps I looked at your diagram, and it looked as if the bids table contained all the applications for specific users. If this is not true, my example above will be incorrect.
source share