Jump to content

Need help with php / mysql wether to display or not


jnerotrix

Recommended Posts

  • Replies 52
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

A left join would work too.

 

table1

id

1

2

3

 

table2

id name

1  a

2  b

3  c

4  d

5  e

 

He has 2 tables basically like that.  He wants to return all rows from table2 where table1.id != table2.id so returned results would be

 

4 d

5 e

 

 

I'm not sure I understand how a left join would work? If I were to do

 

select id, name from table2 left join table1 on table2.id = table1.id

I'd get

1  a

2  b

3  c

 

So I assume if I did

 

select id, name from table2 left join table1 on table2.id != table1.id

 

I would get

 

4 d

5 e

 

But that's not the case... I guess I'm not understanding the concept...

Link to comment
Share on other sites

now surveys never dissapear after completing

 

also the sessions user id whatever is hashed

 

What is the current code?

 

Logically if you select the completed surveys for that user and show only the ones NOT IN the completed_survey table then it should return correctly.

 

If it doesn't check your logic.

 

Link to comment
Share on other sites

I'm not sure I understand how a left join would work?

...

But that's not the case... I guess I'm not understanding the concept...

You would run this query:

 

select table2.id, table2.name from table2 left join table1 on ( table1.id = table2.id ) WHERE table1.id IS NULL 

Link to comment
Share on other sites

I'm not sure I understand how a left join would work?

...

But that's not the case... I guess I'm not understanding the concept...

You would run this query:

 

select table2.id, table2.name from table2 left join table1 on ( table1.id = table2.id ) WHERE table1.id IS NULL 

 

Ah! Now why didn't I think of that.  I was staring at my test table results with the NULLs and it just didn't occur to me... you are the man.

Link to comment
Share on other sites

ok sorry for the long wait for post  :P

 

I tried

 

select table2.id, table2.name from table2 left join table1 on ( table1.id = table2.id ) WHERE table1.id IS NULL 

 

It Displays

 

Table 'sex1800_loginbux.table2' doesn't exist

 

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

here is the code i had before i added he the left join code

 

<?php
mysql_connect("localhost", "sex1800_admin", "PASSWORD") or die(mysql_error()); //add you password
mysql_select_db("sex1800_loginbux") or die(mysql_error());

$query = "select id, title, adlink from survey where id not in (select survey_id from completed_surveys WHERE member_id = '" . $member_id . "')";
$result = mysql_query($query) or die(mysql_error());
?>
Number Of Surveys Available = <?php echo mysql_num_rows($result); ?>

<?php
while($row = mysql_fetch_array($result)){
echo "<tr><td><a href=\"survey.php?id={$row['id']}\">{$row['title']}</a></td></tr>";
}
?>

 

Hope you can help this is the website

 

http://epicbux.com/

 

 

Link to comment
Share on other sites

Ok I ran an sql query in phpmyadmin

 

 

select table2.id, table2.name from table2 left join table1 on ( table1.id = table2.id ) WHERE table1.id IS NULL 

 

and it Showed This

 

SQL query: Documentation

SELECT table2.id, table2.name
FROM table2
LEFT JOIN table1 ON ( table1.id = table2.id )
WHERE table1.id IS NULL
LIMIT 0 , 30

MySQL said: Documentation
#1146 - Table 'sex1800_loginbux.table2' doesn't exist 

 

=====================================

 

Here is the tables and fields i have in the database:

 

- = table
* = field

- completed_surveys
*member_id  <-- When user Completes a Survey their member_id is added
*survey_id <-- When user Completes a the Survey id Goes Here

- survey
*id <-- This Is The Survey ID
*title <-- This is the Survey title used in displaying Survey
*adlink <-- This is the Survey Ad Link Used in displaying Survey

- users
*userid <-- This is Where the Users Id is Stored

 

==================================

 

How would i make it so when php displays the surveys in the table survey

for each user

 

to not show ones that are in the survey_completed table by the userid

 

Hope I have explained this well enough for everyone to understand

 

 

Here is the code i have so far

 

<?php
mysql_connect("localhost", "sex1800_admin", "PASSWORD") or die(mysql_error()); //add you password
mysql_select_db("sex1800_loginbux") or die(mysql_error());

$query = "select table2.id, table2.name from table2 left join table1 on ( table1.id = table2.id ) WHERE table1.id IS NULL ";
$result = mysql_query($query) or die(mysql_error());
?>
Number Of Surveys Available = <?php echo mysql_num_rows($result); ?>

<?php
while($row = mysql_fetch_array($result)){
echo "<tr><td><a href=\"survey.php?id={$row['id']}\">{$row['title']}</a></td></tr>";
}
?>

 

 

Link to comment
Share on other sites

Ok well i dont know mysql

====================

what do i change these tables to in this code

 

select table2.id, table2.name from table2 left join table1 on ( table1.id = table2.id ) WHERE table1.id IS NULL 

 

Here are the tables

===================

 

- = table

* = field

 

- completed_surveys

*member_id  <-- When user Completes a Survey their member_id is added

*survey_id <-- When user Completes a the Survey id Goes Here

 

- survey

*id <-- This Is The Survey ID

*title <-- This is the Survey title used in displaying Survey

*adlink <-- This is the Survey Ad Link Used in displaying Survey

 

- users

*userid <-- This is Where the Users Id is Stored

Link to comment
Share on other sites

I tried this but it doesnt work it doesnt error but theres nothing

 

<?php
mysql_connect("localhost", "sex1800_admin", "12921993") or die(mysql_error()); //add you password
mysql_select_db("sex1800_loginbux") or die(mysql_error());

$query = "select survey.id, survey.title, survey.adlink from survey left join completed_surveys on ( survey.id = completed_surveys.survey_id ) WHERE survey.id IS NULL ";
$result = mysql_query($query) or die(mysql_error());
?>
Number Of Surveys Available = <?php echo mysql_num_rows($result); ?>

<?php
while($row = mysql_fetch_array($result)){
echo "<tr><td><a href=\"survey.php?id={$row['id']}\">{$row['title']}</a></td></tr>";
}
?>

Link to comment
Share on other sites

The non-joined table will never have null values as the result of a left join -- try

 

$query = "select surveys.id, survey.title, survey.adlink from survey left join completed_surveys on ( survey.id = completed_surveys.survey_id ) WHERE completed_surveys.survey_id IS NULL ";

Link to comment
Share on other sites

ok its almost perfect but now

 

if

 

user1 completes a survey

it disappears from user1's survey list

but it also disappears from user2's list it should only disappear from the user that took the survey

so their should be something with member id

 

member id is stored in

users.userid

and when a user completes a survey it saves their id in

completed_surveys.member_id

 

or u can use

 

$_GET['id']

 

to get the id

Link to comment
Share on other sites

Heres the code i have so far

 

 

<?php
mysql_connect("localhost", "sex1800_admin", "12921993") or die(mysql_error()); //add you password
mysql_select_db("sex1800_loginbux") or die(mysql_error());

$query = "select surveys.id, survey.title, survey.adlink from survey left join completed_surveys on ( survey.id = completed_surveys.survey_id ) WHERE completed_surveys.survey_id IS NULL ";
$result = mysql_query($query) or die(mysql_error());
?>
Number Of Surveys Available = <?php echo mysql_num_rows($result); ?>

<?php
while($row = mysql_fetch_array($result)){
echo "<tr><td><a href=\"survey.php?id={$row['id']}\">{$row['title']}</a></td></tr>";
}
?>

 

it needs

 

to check the userid for completed surveys

 

this code makes it so if any1 completes the survey it dissapears off of all users surveys list

Link to comment
Share on other sites

<?php
mysql_connect("localhost", "sex1800_admin", "12921993") or die(mysql_error()); //add you password
mysql_select_db("sex1800_loginbux") or die(mysql_error());

$memberid = (is_numeric($_GET['id'])) ? $_GET['id'] : 0;
$query = "select surveys.id, survey.title, survey.adlink from survey left join completed_surveys on ( survey.id = completed_surveys.survey_id ) WHERE completed_surveys.survey_id IS NULL AND completed_surveys.member_id=$memberid";
$result = mysql_query($query) or die(mysql_error());
?>
Number Of Surveys Available = <?php echo mysql_num_rows($result); ?>

<?php
while($row = mysql_fetch_array($result)){
echo "<tr><td><a href=\"survey.php?id={$row['id']}\">{$row['title']}</a></td></tr>";
}
?>

 

try that

Link to comment
Share on other sites

Doesnt display surveys still i tried changing the $memberid code bt still didnt work

 

heres the code so far

 

<?php
mysql_connect("localhost", "sex1800_admin", "12921993") or die(mysql_error()); //add you password
mysql_select_db("sex1800_loginbux") or die(mysql_error());

$memberid = mysql_real_escape_string($_SESSION['userid']);
$query = "select survey.id, survey.title, survey.adlink from survey left join completed_surveys on ( survey.id = completed_surveys.survey_id ) WHERE completed_surveys.survey_id IS NULL AND completed_surveys.member_id = $memberid";
$result = mysql_query($query) or die(mysql_error());
?>
Number Of Surveys Available = <?php echo mysql_num_rows($result); ?>

<?php
while($row = mysql_fetch_array($result)){
echo "<tr><td><a href=\"survey.php?id={$row['id']}\">{$row['title']}</a></td></tr>";
}
?>

Link to comment
Share on other sites

<?php
mysql_connect("localhost", "sex1800_admin", "12921993") or die(mysql_error()); //add you password
mysql_select_db("sex1800_loginbux") or die(mysql_error());

echo $memberid = mysql_real_escape_string($_SESSION['userid']);
echo ' Member Id<br /><br />';
$query = "SELECT member_id FROM completed_surveys";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['member_id'];
}
die('end of test');
$query = "select survey.id, survey.title, survey.adlink from survey left join completed_surveys on ( survey.id = completed_surveys.survey_id ) WHERE completed_surveys.survey_id IS NULL AND completed_surveys.member_id = $memberid";
$result = mysql_query($query) or die(mysql_error());
?>
Number Of Surveys Available = <?php echo mysql_num_rows($result); ?>

<?php
while($row = mysql_fetch_array($result)){
echo "<tr><td><a href=\"survey.php?id={$row['id']}\">{$row['title']}</a></td></tr>";
}
?>

 

Run this so I can see what's going on

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.