gtrufitt Posted March 16, 2008 Share Posted March 16, 2008 Hi, I am trying to echo a comment, with the comment senders name, ina table, however, only the comments come up, I cant get the name of the sender to follow. The table structures are: user id, f_name profilecomment id, profileid, comment The sender's ID is retrieved from a session variable and the profile ID is retrieved from GET. Here is the code that I have so far: <?php $profilecomments = "SELECT id, comment FROM profilecomment WHERE profileid = '$user_id'"; $pc = mysql_query($profilecomments) or die(); $senderrow = mysql_fetch_array($pc); $senderid = $senderrow['id']; $getfriends = "SELECT friendid FROM friends WHERE id = '$user_id'"; $gf = mysql_query($getfriends) or die(); $friendrow = mysql_fetch_array($gf); while ($rowgf = mysql_fetch_array($gf, MYSQL_ASSOC)) { $getfriendid = $rowgf['friendid']; } $getfname = "SELECT f_name, l_name FROM user WHERE id = '$getfriendid'"; $gfn = mysql_query($getfname) or die(); ?> <?php if (mysql_num_rows($pc) == 0) { echo '<p><h2>' . $f_name . ' has no comments!</h2></p>' ; } else { $sender = "SELECT f_name FROM user WHERE id = '$senderid'"; $sc = mysql_query($sender) or die(); while($rowa = mysql_fetch_array($pc)) { $rows = mysql_fetch_array($sc); echo '<table id="profiletable"><tr><td><a href ="userprofile.php?user='; echo $rowa['id']; echo '">'; echo $rowa['comment']; echo ' from '; echo $rows['f_name']."</a></tr></td></table>\n"; } } ?> Any help would be great, I've been trying to work this out for a couple of days now and I just cant get my head around it! Thanks, Gareth Quote Link to comment https://forums.phpfreaks.com/topic/96378-echoing-data-from-two-arrays-in-one-table/ Share on other sites More sharing options...
gtrufitt Posted March 16, 2008 Author Share Posted March 16, 2008 I have tried combining the arrays, but couldnt get that to work either Quote Link to comment https://forums.phpfreaks.com/topic/96378-echoing-data-from-two-arrays-in-one-table/#findComment-493299 Share on other sites More sharing options...
wildteen88 Posted March 16, 2008 Share Posted March 16, 2008 Look into using JOINS Using JOINS allows you to query multiple tables at one. Rather using multiple queries. Quote Link to comment https://forums.phpfreaks.com/topic/96378-echoing-data-from-two-arrays-in-one-table/#findComment-493321 Share on other sites More sharing options...
gtrufitt Posted March 16, 2008 Author Share Posted March 16, 2008 No worries, I just worked it out. The working code, if anyone's interested is: <?php $profilecomments = "SELECT id, comment FROM profilecomment WHERE profileid = '$user_id'"; $pc = mysql_query($profilecomments) or die(); ?> <?php if (mysql_num_rows($pc) == 0) { echo '<p><h2>' . $f_name . ' has no comments!</h2></p>' ; } else { while($rowa = mysql_fetch_array($pc, MYSQL_ASSOC)) { $senderid = $rowa['id']; $sender = "SELECT f_name FROM user WHERE id = '$senderid'"; $sc = mysql_query($sender) or die(); $rows = mysql_fetch_array($sc, MYSQL_ASSOC); { echo '<table id="profiletable"><tr><td><a href ="userprofile.php?user='; echo $senderid; echo '">'; echo $rowa['comment']; echo ' from '; echo $rows['f_name']."</a></tr></td></table>\n"; } } } ?> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/96378-echoing-data-from-two-arrays-in-one-table/#findComment-493347 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.