Jump to content

[SOLVED] Too many rows outputed!


techiefreak05

Recommended Posts

I have a widget system on my site Im trying to make, where users can choose which widgets they want to display on their home page.

I have the following code that selects which "widgets" the user has, and then the second query in the code is supposed to pick out the widgets that the user doesnt already have..

 

<?php
	$queryFlips = mysql_query("SELECT flipID  FROM userFlips WHERE `userID` = '$_SESSION[id]'") or die(mysql_error()); //Get FLIPS user has.
	$db = mysql_fetch_array($queryFlips);
	$fid=$db[flipID];
			$queryFlips2 = mysql_query("SELECT * FROM flips WHERE `id` != '$fid'") or die(mysql_error()); // Get info of flips that user doesnt have.
			while ($db2 = mysql_fetch_array($queryFlips2)){
				$flipName=$db2['name'];
				$flipId=$db2['id'];
				echo '<tr>
		<td><form action="" method="post"><input type="submit" name="add" value="Add"><input type="hidden" name="addID" value="' . $flipId . '"></form></td><td>' . $flipName . '</td>
				</tr>';
			}

?>

 

But.. it is displaying widgets that I already have... I'm not great at joining queries or what ever its called, so id love some help!

Link to comment
Share on other sites

[pre]

user          userflips      flips

--------      ---------      --------

userID  ---  userID    +--- flipID

username      flipID  ---+    name

[/pre]

 

To get user's flips

 

SELECT f.name FROM flips f

INNER JOIN userflips uf ON uf.flipID = f.flipID

WHERE uf.userID = '$user';

 

flips user doesn't have

 

SELECT f.name FROM flips f

LEFT JOIN userflips uf ON uf.flipID = f.flipID AND uf.userID = '$user'

WHERE uf.flipID IS NULL

Link to comment
Share on other sites

It works great! Thanks a lot! I needed to change the "f.flipID" to "f.id" because in the "flips" table, the id column is called "id". and I added a while loop:

 

<?php
$queryFlips = mysql_query("SELECT f.name FROM flips f
LEFT JOIN userFlips uf ON uf.flipID = f.id AND uf.userID = '$_SESSION[id]'
WHERE uf.flipID IS NULL");
while($db = mysql_fetch_array($queryFlips)){
$flipId=$db['id'];
$flipName=$db['name'];
echo '<tr>
	<td><form action="" method="post"><input type="submit" name="add" value="Add"><input type="hidden" name="addID" value="' . $flipId . '"></form></td><td>' . $flipName . '</td>
</tr>';
}
?>

Link to comment
Share on other sites

Uh oh, wait. it's not quite working yet.

 

I had this, which selected the flip name just fine, but not the ID..

 

<?php
while($db = mysql_fetch_array($queryFlips)){
$flipId=$db['id']; // this didnt work.
$flipName=$db['name']; // this DID work.
?>

 

Then I changed it to this, and now name doesnt work as well as ID.

 

<?php
while($db = mysql_fetch_array($queryFlips)){
$flipId=$db['f.id']; //this doesnt work..
$flipName=$db['f.name']; ///this doesnt work either.
?>

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.