Jump to content

Echoing data from two arrays in one table


gtrufitt

Recommended Posts

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

Link to comment
Share on other sites

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

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.