9 Replies - 1259 Views - Last Post: 09 July 2009 - 07:12 AM Rate Topic: -----

#1 ninethousandfeet@msn.com  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 310
  • Joined: 09-February 09

do... while, with two tables from db

Post icon  Posted 01 June 2009 - 10:09 PM

hello,

i am stuck on a small problem, any help or suggestions more than appreciated, thank you. my users' main page is their profile page. on their profile page their comments are listed (all comments sent to this user show up here). these comments are in a comment table and the sql pulls info from there, etc. got it to this point.

my question is: how can i intertwine posts this user makes in with their comments? by posts, i mean items they post that they want to buy or sell, not posting comments. they post new items to buy or sell and that info is stored in the posting table in my db.

how can i make a do... while statement to list all comments to this user and all posts by this user in the same table and not have to keep them separate?

for example:

June 1st: 'hey, what's going on?' ---- comment table
May30th: New post to buy: Jeans ---- posting table
May29th: 'yea, i can do that, just let me know...' --- comment table

etc...

my current code for just the do... while for the comments table
<?php do { ?>
	  <tr>
		<td width="20" valign="top" align="left"><img src="Images/conversationBubble.png" width="23" height="22" align="absmiddle" /></td>
		<td valign="middle" width="470" align="left"><a href="OTHERuserlink.php?comment_id=<?php echo $row_getComment['comment_id']; ?>" class="link16"><?php echo $row_getComment['comment_username']; ?></a> <font size="-1">said</font> <?php echo $row_getComment['comment']; ?><br /><span class="font11Red"><?php echo $row_getComment['dateOFcomment']; ?></span></td>
	  </tr>
	  <?php } while ($row_getComment = mysql_fetch_assoc($getComment)); ?>


This post has been edited by ninethousandfeet@msn.com: 01 June 2009 - 10:14 PM


Is This A Good Question/Topic? 0
  • +

Replies To: do... while, with two tables from db

#2 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: do... while, with two tables from db

Posted 01 June 2009 - 10:33 PM

i changed the code and works for me.
<?php while ($row_getComment = mysql_fetch_assoc($getComment)){ ?>
	  <tr>
		<td width="20" valign="top" align="left"><img src="Images/conversationBubble.png" width="23" height="22" align="absmiddle" /></td>
		<td valign="middle" width="470" align="left"><a href="OTHERuserlink.php?comment_id=<?php echo $row_getComment['comment_id']; ?>" class="link16"><?php echo $row_getComment['comment_username']; ?></a> <font size="-1">said</font> <?php echo $row_getComment['comment']; ?><br /><span class="font11Red"><?php echo $row_getComment['dateOFcomment']; ?></span></td>
	  </tr>
	  <?php } ?>

Was This Post Helpful? 0
  • +
  • -

#3 sam_benne  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 16
  • View blog
  • Posts: 732
  • Joined: 16-January 08

Re: do... while, with two tables from db

Posted 02 June 2009 - 01:43 AM

You can do this as a While function.
while ($d = mysql_fetch_array($sql)) {

...

}



That will display the contents of the DB's. The only problem would be with that when you come to display the results from the query the headings of the two different tables will be different. So what I would do is in the mysql query is something like this.
SELECT * FROM comment AND SELECT * FROM post WHERE name = '$user' ORDER BY date DESC


That shoule get the data from both tables however it is untested. then in the while function you would show the code that would show the comment then under that add the code for the post it shoul only do one of them depending which one it is otherwise you can do a check with an if else statment.
Was This Post Helpful? 0
  • +
  • -

#4 ninethousandfeet@msn.com  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 310
  • Joined: 09-February 09

Re: do... while, with two tables from db

Posted 05 June 2009 - 11:54 AM

this last solution won't work because there are 3 tables involved in this; userTable, postingTable, and commentTable.

any other ideas on how i can show a user's comments and posts in the same do...while area and have them ordered by date desc. the key is that i want them to be intertwined rather than all of the comments listed and then below that all the posts (this wouldn't look very good).
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3079
  • View blog
  • Posts: 10,804
  • Joined: 08-August 08

Re: do... while, with two tables from db

Posted 05 June 2009 - 12:22 PM

This is where functions and OOP become really important. This would be easy if these things were functions/methods:

do {
	displayuserTableposts($userid);
	displaypostingTableposts($userid);
	displaycommentTableposts($userid);
} while(some condition is true);

Done.

This post has been edited by CTphpnwb: 05 June 2009 - 12:22 PM

Was This Post Helpful? 0
  • +
  • -

#6 ninethousandfeet@msn.com  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 310
  • Joined: 09-February 09

Re: do... while, with two tables from db

Posted 05 June 2009 - 01:06 PM

i am not too familiar with functions. i've read up a bit on php.net, but still fuzzy. would i make separate sql, one to get the comments for that user and one to get the posts by that user, and then put then into separate functions?
function comment($sql for comments by this user) {
displaycommentsTableposts($sql for comments by this user)
}
function post($sql for posts by this user) {
displaypostingTableposts($sql for posts by this user)
}



and then ...
do {
	displaycommentsTableposts($sql for comments by this user);
	displaypostingTableposts($sql for posts by this user);
} while ($sql to verify who the logged in user is);


Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3079
  • View blog
  • Posts: 10,804
  • Joined: 08-August 08

Re: do... while, with two tables from db

Posted 05 June 2009 - 01:32 PM

Here's an example that's close to what you're trying to do. It uses one function to output from two different arrays. When you access two different tables, you get two different arrays, so it's very similar.
<?php
function starttable() {
	echo '<table border=1><th>Name</th><th>Address</th><th>Phone</th><tr>';
}

function addtotable($thearray) {
	foreach($thearray as $value) {
		echo '<td>'.$value."</td>";
	}
	echo "<tr>";
}

$some_array[0] = array("Name"=>"Joe Schmoe","Addr"=>"111 Main Street","Phone"=>"888-555-1212");
$some_array[1] = array("Name"=>"Bob Jones","Addr"=>"1 Way Street","Phone"=>"888-333-2111");
$another_array[0] = array("Name"=>"Jane Doe","Addr"=>"222 Some Avenue","Phone"=>"888-444-4444");
$another_array[1] = array("Name"=>"Mary Smith","Addr"=>"55 Another Rd","Phone"=>"888-233-2222");
$another_array[2] = array("Name"=>"Ella Fitsimons","Addr"=>"23 Gulf Drive","Phone"=>"888-777-9998");

starttable();
foreach($some_array as $sub_array) {
	addtotable($sub_array);
}
foreach($another_array  as $sub_array) {
	addtotable($sub_array);
}
?>

Was This Post Helpful? 0
  • +
  • -

#8 ninethousandfeet@msn.com  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 310
  • Joined: 09-February 09

Re: do... while, with two tables from db

Posted 08 July 2009 - 12:08 PM

hi,

i am coming back to this problem, it got put on the back burner while i changed my layout and design.

looking at the most recent solution provided by CTphpnwb, my main point of confusion is the second function provided:

<?php
...

function addtotable($thearray) {
	foreach($thearray as $value) {
		echo '<td>'.$value."</td>";
	}
	echo "<tr>";
}
...
?>



i'm not the greatest with functions. can someone help me to better understand what $thearray and $value would be given my original problem? i think i can figure the rest out, but i just don't know how to tie this part into my solution.

thank you!
Was This Post Helpful? 0
  • +
  • -

#9 daxriders  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 25
  • Joined: 08-July 09

Re: do... while, with two tables from db

Posted 08 July 2009 - 04:24 PM

i'm not a pro in php but your problem seems to be rather an SQL issue. Why would you insert data from 2 arrays when you could have only one, if you do the proper sql query:

SELECT TheDate, TheUser, TheText FROM postingTable WHERE postingTable.TheUser = "AnyUser"
 UNION SELECT TheDate, TheUser, TheText  FROM commentTable WHERE commentTable.TheUser = "AnyUser";


AnyUser has to be the same user. You can then order the result by TheDate.



View Postninethousandfeet@msn.com, on 8 Jul, 2009 - 11:08 AM, said:

hi,

i am coming back to this problem, it got put on the back burner while i changed my layout and design.

looking at the most recent solution provided by CTphpnwb, my main point of confusion is the second function provided:

<?php
...

function addtotable($thearray) {
	foreach($thearray as $value) {
		echo '<td>'.$value."</td>";
	}
	echo "<tr>";
}
...
?>



i'm not the greatest with functions. can someone help me to better understand what $thearray and $value would be given my original problem? i think i can figure the rest out, but i just don't know how to tie this part into my solution.

thank you!

This post has been edited by daxriders: 08 July 2009 - 04:24 PM

Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3079
  • View blog
  • Posts: 10,804
  • Joined: 08-August 08

Re: do... while, with two tables from db

Posted 09 July 2009 - 07:12 AM

View Postninethousandfeet@msn.com, on 8 Jul, 2009 - 03:08 PM, said:

<?php
...

function addtotable($thearray) {
	foreach($thearray as $value) {
		echo '<td>'.$value."</td>";
	}
	echo "<tr>";
}
...
?>


i'm not the greatest with functions. can someone help me to better understand what $thearray and $value would be given my original problem? i think i can figure the rest out, but i just don't know how to tie this part into my solution.


When you call the function "addtotable" you pass it an array, which the function will call $thearray. The foreach loop iterates through $thearray assigning each value as it comes across them to $value. Then echo outputs the table data tags around the value so that they show up in your table.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1