Jump to content

IF item is in database don't echo. .


Bradley99

Recommended Posts

Evening,

 

I'm trying to add some code so that IF 2 items ARE inside a table, then part of an echo, doesn't echo.

 

My echo is listing forms for users to make picks, but I'm trying to code so that IF a user has already a pick for a certain fight, that section of the form doesn't show, just the ones he hasn't picked will.

 

Here's my echo code:

      <?

   $showevent = mysql_query("SELECT * FROM fights WHERE event='$viewevent' ORDER by id ASC");
   while($the=mysql_fetch_object($showevent)){ 
   
  echo "  <form action='' method='post'><tr><td class='profilerow'><input type=radio name=fighter_id value=$the->fighter1> $the->fighter1 <BR> <input type=radio name=fighter_id value=$the->fighter2> $the->fighter2 <BR> <input type=radio name=fighter_id value=1> Draw</td>
	  <td class='profilerow'><select name=method><option value=KO>Knockout</option><option value=TKO>Technical Knockout</option><option value=DEC>Decision</option></select></td>
	  <td class='profilerow'><select name=round><option value=1>1</option><option value=2>2</option><option value=3>3</option><option value=4>4</option><option value=5>5</option></select></td>
	  <td class='profilerow'><select name=bonus><option value=Yes>Yes</option><option value=No>No</option></td>
	  <input type='hidden' name='fight_id' value='$the->id'>	  
	  <td class='profilerow'><input type='Submit' name='Submit' value='Pick' class='Submit' /> </td></form>

   </tr>"; } ?>

 

I was going to try and use something like this:

	   $pickdone = mysql_query("SELECT * FROM betting WHERE user_id='$user->id' AND fight_id='$fight_id");	
   if ($pickdone. . . .  ){

But I can't figure what should go in the IF part.

 

Any help is appreciated.

Link to comment
Share on other sites

I'm not really following your explanation. You state you want to exclude the output where "2 items ARE inside a table". I read that as if there are two records with the same value. but, I think you mean you want to exclude records where to fields have two different values. It also looks like you are trying to run two queries to accomplish that. That''s unnecessary. If there are records you are not going to use - then don't query them. You have one table for events and another table for the bets by users for those events and you want to exclude the events where the user has already placed a bet. So change the first query to exclude the records you don't want. I'm not sure what fields from the two table are used to associate them, but I am going to guess that the fight_id in the bets table is used to associate with the id in the fights table. Not sure, but you can make the correction as needed.

 

I can think of two ways to accomplish this, but not sure which is more efficient:

SELECT *
FROM fights
WHERE event='$viewevent'
  AND id NOT IN (SELECT fight_id FROM betting WHERE user_id='$user->id')
ORDER by id ASC

 

SELECT fights.*
FROM fights
LEFT JOIN betting
  ON betting.fight_id = events.event_id AND user_id='$user->id'
WHERE fights.event='$viewevent'
  AND betting.fight_id IS NULL
ORDER by fights.id ASC

 

EDIT: Modified errors in 2nd query

Link to comment
Share on other sites

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.