#1
Posted 25 February 2013 - 04:07 PM
ID username followname
username is the name of the person that is doing the action/following and followname is the person who is being followed.
I want to set up a news feed for each user on the profile.php page. The news feed should look at who the user is following (followname) and then fill the news feed with all the posts from those people in order of what time they were posted. The posts are in a different table called "needs" and each post is timestamped.
Can anyone help me in started to code this?
#2
Posted 25 February 2013 - 04:08 PM
#3
Posted 25 February 2013 - 04:38 PM
$following = mysql_query("SELECT * FROM follow WHERE username='$username'");
while ($followrow = mysql_fetch_array($following)) {
$followname = $followrow['followname'];
$lookupposts = mysql_query ("SELECT * FROM needs WHERE needsusername='$followname'");
while ($postrow = mysql_fetch_array($lookupposts)) {
$description = $postrow['description'];
$needsusername = $postrow['needsusername'];
$datesubmitted = $postrow['datesubmitted'];
echo "<b>User:</b> $needsusername<br><br><b>Description:</b><br>$description<br><br><b>Date Submitted:</b> $datesubmitted<br><br>----------------------------------------<br><br>";
It shows it in order of username THEN timestamp, but I need it by just timestamp. Any suggestions?
#4
Posted 25 February 2013 - 04:40 PM
How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq
Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!
#5
Posted 25 February 2013 - 04:43 PM
#6
Posted 25 February 2013 - 04:44 PM
Google "mysql join"
How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq
Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!
#7
Posted 25 February 2013 - 04:56 PM
$following = mysql_query("SELECT * FROM follow WHERE username='$username'");
$followrow = mysql_fetch_array($following);
$followname = $followrow['followname'];
$lookupposts = mysql_query("SELECT * FROM follow a, needs b WHERE a.username='$username' AND b.needsusername='$followname' AND b.status='posted' ORDER BY b.datetime");
while ($postrow = mysql_fetch_array($lookupposts)) {
$description = $postrow['description'];
$needsusername = $postrow['needsusername'];
$datesubmitted = $postrow['datesubmitted'];
echo "<b>User:</b> $needsusername<br><br><b>Description:</b><br>$description<br><br><b>Date Submitted:</b> $datesubmitted<br><br>----------------------------------------<br><br>";
}
It's only showing one user's posts that I'm following and I'm not quite sure why. It also shows the same posts multiple times.
#8
Posted 25 February 2013 - 05:01 PM
If you're going to use table aliases make them make sense. a and b are meaningless.
You also should be using a primary key ID instead of the username. Username should only be stored in the user table, everything else should use their user id.
You are running one query to get a user, and then selecting the information for that ONE USER. You need to do just ONE query joining the two tables only limiting it to the logged in user, not BOTH users.
How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq
Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!
#9
Posted 25 February 2013 - 05:07 PM
#10
Posted 25 February 2013 - 05:07 PM
#11
Posted 25 February 2013 - 05:09 PM
Do only ONE query.
How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq
Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!
#12
Posted 25 February 2013 - 05:15 PM
$lookupposts = mysql_query("SELECT * FROM follow a, needs b WHERE a.username='$username' AND b.username='$username' b.status='posted'");
while ($postrow = mysql_fetch_array($lookupposts)) {
$description = $postrow['description'];
$needsusername = $postrow['needsusername'];
$datesubmitted = $postrow['datesubmitted'];
echo "<b>User:</b> $needsusername<br><br><b>Description:</b><br>$description<br><br><b>Date Submitted:</b> $datesubmitted<br><br>----------------------------------------<br><br>";
}
I don't feel like it's accomplishing what I want but it gives me this error:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given on line 46
#13
Posted 25 February 2013 - 05:18 PM
How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq
Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!
#14
Posted 25 February 2013 - 05:20 PM
#15
Posted 25 February 2013 - 05:21 PM
You have a syntax error in your query. Read the link in my signature on debugging SQL.
You're the fastest reader I've ever seen!
OR there's no way you read the post, implemented proper error checking and got the actual error.
How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq
Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!
#16
Posted 25 February 2013 - 05:22 PM
I know it's because I'm reusing the variables in my while loop, doesn't change the fact that I still don't know how to fix the issue.
#17
Posted 25 February 2013 - 05:24 PM
How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq
Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!
#18
Posted 25 February 2013 - 05:32 PM
#19
Posted 25 February 2013 - 05:50 PM
$sql = "SELECT * FROM follow, needs WHERE follow.username=needs.username ORDER BY needs.datetime";
$lookupposts = mysqli_query($sql) or trigger_error("Query Failed! SQL: $sql - Error: ".mysqli_error(), E_USER_ERROR);
while ($postrow = mysql_fetch_array($lookupposts)) {
$description = $postrow['needs.description'];
$needsusername = $postrow['needs.needsusername'];
$datesubmitted = $postrow['needs.datesubmitted'];
echo "<b>User:</b> $needsusername<br><br><b>Description:</b><br>$description<br><br><b>Date Submitted:</b> $datesubmitted<br><br>----------------------------------------<br><br>";
}
and here what your debug code showed me:
Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/content/17/9932517/html/ProjectX/profile.php on line 45
Warning: mysqli_error() expects exactly 1 parameter, 0 given in /home/content/17/9932517/html/ProjectX/profile.php on line 45
Fatal error: Query Failed! SQL: SELECT * FROM follow, needs WHERE follow.username=needs.username ORDER BY needs.datetime - Error: in /home/content/17/9932517/html/ProjectX/profile.php on line 45
Still no clue how to solve this issue......
#20
Posted 25 February 2013 - 05:57 PM
However, you did actually fix the syntax error, so good job. You still need to specify what column to do your join ON, but otherwise good.
How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq
Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users












