Current location: Hot Scripts Forums » Programming Languages » PHP » Multiple Results


Multiple Results

Reply
  #1 (permalink)  
Old 04-12-04, 06:27 PM
D_tunisia D_tunisia is offline
Newbie Coder
 
Join Date: Apr 2004
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Multiple Results

I've got a little out of my depth with this one, so I wonder if anyone can help!

I've go the following code:

PHP Code:

<?php


include ("dbConfig.php");

$result mysql_query("SELECT * FROM posts");

echo 
"<table border=0>\n";

while (
$myrow mysql_fetch_row($result)) {

        
printf("<tr><td><font face=verdana size=2><b>%s</b><br><font face=verdana size=1>Posted by %s<br><br><font face=verdana size=2>%s<br><a href=\"comment.php?id=%s\">Comment on this post</a><br><br></td></tr>\n",
        
$myrow[2], $myrow[1], $myrow[3], $myrow[0]);


}

echo 
"</table>\n";

?>
The code looks at my message post table and pulls out all of the results (http://69.73.155.48/index2.php).

All of the above works fine, and I have now created a second table called "comments" and created the input forms for the users. Again, this works fine up till this point.

The problem I am having is that I want to post all of the comments below the relevant posts on the main page. I just cannot get it to work. I've tried nesting another while loop into the code but I keep getting errors. When the user posts a comment it is stored in the comment table with the userID number and the message which it is relevent to.

If anyone can help I would be really greatful!

Last edited by D_tunisia; 04-12-04 at 06:30 PM.
Reply With Quote
  #2 (permalink)  
Old 04-12-04, 08:57 PM
mdhall's Avatar
mdhall mdhall is offline
Aspiring Coder
 
Join Date: Oct 2003
Posts: 510
Thanks: 1
Thanked 1 Time in 1 Post
Quote:
Originally Posted by D_tunisia
I've got a little out of my depth with this one, so I wonder if anyone can help!

I've go the following code:

PHP Code:

<?php
 
include ("dbConfig.php");
 
$result mysql_query("SELECT * FROM posts");
 
echo 
"<table border=0>\n";
 
while (
$myrow mysql_fetch_row($result)) {
 
        
printf("<tr><td><font face=verdana size=2><b>%s</b><br><font face=verdana size=1>Posted by %s<br><br><font face=verdana size=2>%s<br><a href=\"comment.php?id=%s\">Comment on this post</a><br><br></td></tr>\n",
        
$myrow[2], $myrow[1], $myrow[3], $myrow[0]);
 
 
}
 
echo 
"</table>\n";
 
?>
The code looks at my message post table and pulls out all of the results (http://69.73.155.48/index2.php).

All of the above works fine, and I have now created a second table called "comments" and created the input forms for the users. Again, this works fine up till this point.

The problem I am having is that I want to post all of the comments below the relevant posts on the main page. I just cannot get it to work. I've tried nesting another while loop into the code but I keep getting errors. When the user posts a comment it is stored in the comment table with the userID number and the message which it is relevent to.

If anyone can help I would be really greatful!
I've done something similair. When you pull your "posts" data, get the id.
In your "comments" table, have a field called "post_id", which holds the id of the post that it relates to. Then do a query of the comments table, matching the comments "post_id" to the posts' id, something like SELECT * FROM comments WHERE post_id='$id'". (Something along these lines).

Hope this helps a little.
Reply With Quote
  #3 (permalink)  
Old 04-12-04, 09:29 PM
D_tunisia D_tunisia is offline
Newbie Coder
 
Join Date: Apr 2004
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for the help, but I really need more advice. I've tried out adding a new while loop within the current one and it isn't working. I would be really greatful if someone can give me some more detail on this one.

The code I have is:

PHP Code:

<?php


include ("dbConfig.php");

$result mysql_query("SELECT * FROM posts");

echo 
"<table border=0>\n";

while (
$myrow mysql_fetch_row($result)) {
        
        
printf("<tr><td><font face=verdana size=2><b>%s</b><br><font face=verdana size=1>Posted by 

%s<br><br><font face=verdana size=2>%s<br><a href=\"comment.php?id=%s\">Comment on this 

post</a><br><br></td></tr>\n"
,
        
$myrow[2], $myrow[1], $myrow[3], $myrow[0]);

}

echo 
"</table>\n";

?>
The comments table is called "comments" and the post ID field is "post_id". I'm also not sure how to "get" the post number from the posts table and use it to compare to the comments table.
Reply With Quote
  #4 (permalink)  
Old 04-12-04, 09:36 PM
Infinite_Hackers's Avatar
Infinite_Hackers Infinite_Hackers is offline
Coding Addict
 
Join Date: Dec 2003
Posts: 307
Thanks: 0
Thanked 0 Times in 0 Posts
try insted of mysql_fetch_row use mysql_fetch_array.. see if that works.

Also can you show me your table structure if this doesn't work.

PHP Code:

<?php


include ("dbConfig.php");

$result mysql_query("SELECT * FROM posts");

echo 
"<table border=0>\n";

while (
$myrow mysql_fetch_array($result)) {
        
        
printf("<tr><td><font face=verdana size=2><b>%s</b><br><font face=verdana size=1>Posted by

%s<br><br><font face=verdana size=2>%s<br><a href=\"comment.php?id=%s\">Comment on this

post</a><br><br></td></tr>\n"
,
        
$myrow[2], $myrow[1], $myrow[3], $myrow[0]);

}

echo 
"</table>\n";

?>
__________________
New beta project: GFX-PRO.ATH.CX
Description: GFX-PRO a great new project that anyone can and setup in less then a minute. [ More info ]
Website: http://gfx-pro.ath.cx
Status: Online
http://gfx-pro.ath.cx/sig.png
Reply With Quote
  #5 (permalink)  
Old 04-13-04, 07:11 AM
D_tunisia D_tunisia is offline
Newbie Coder
 
Join Date: Apr 2004
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
I'm still having a lot of problems with this, does anyone else have any good ideas?

Thanks.
Reply With Quote
  #6 (permalink)  
Old 04-13-04, 11:21 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
use Joins to make it easy!
something like this query might help:
Code:
SELECT p.*, comments.* FROM posts AS p LEFT OUTER JOIN comments ON p.id=comments.post_id ORDER BY p.time;
this code will bring each post with its comments ! and will sort them by post time ..
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #7 (permalink)  
Old 04-14-04, 12:45 AM
D_tunisia D_tunisia is offline
Newbie Coder
 
Join Date: Apr 2004
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for the help. I have entered that code but I keep getting this error:


Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/ittechni/public_html/index2.php on line 81


Am I doing something silly? Here is the new cde I have:

PHP Code:

<?php


include ("dbConfig.php");

$result mysql_query("SELECT p.*, comments.* FROM posts AS p LEFT OUTER JOIN comments ON p.id=comments.post_id ORDER 

BY p.time;"
);

echo 
"<table border=0>\n";

while (
$myrow mysql_fetch_row($result)) {

        
printf("<tr><td><font face=verdana size=2><b>%s</b><br><font face=verdana size=1>Posted by 

%s<br><br><font face=verdana size=2>%s<br><a href=\"comment.php?id=%s\">Comment on this 

post</a><br><br></td></tr>\n"
,
        
$myrow[2], $myrow[1], $myrow[3], $myrow[0]);


}

echo 
"</table>\n";

?>
Reply With Quote
  #8 (permalink)  
Old 04-14-04, 04:38 PM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
change this line:
PHP Code:

$result mysql_query("SELECT p.*, comments.* FROM posts AS p LEFT OUTER JOIN comments ON p.id=comments.post_id ORDER BY p.time;"); 

to:
PHP Code:

$result mysql_query("SELECT p.*, comments.* FROM posts AS p LEFT OUTER JOIN comments ON p.id=comments.post_id ORDER BY p.time")or

die(
mysql_error()); 
if there is any error, it should be printed to screen, tell us the error message if anything appear ..
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #9 (permalink)  
Old 04-14-04, 06:03 PM
D_tunisia D_tunisia is offline
Newbie Coder
 
Join Date: Apr 2004
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Getting there! I get this error now:

Unknown column 'p.time' in 'order clause'
Reply With Quote
  #10 (permalink)  
Old 04-15-04, 07:24 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
you should've changed it silly !!
change it to the field name that you want your results to be sorted by or just remove it!
Code:
ORDER BY p.THE_FIELD_YOU_WANT_TO_SORT_RESULT_BY;
oh, that was long

I assumed that you had a field that contain date/time so just change it as I said !
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]

Last edited by NeverMind; 04-15-04 at 07:44 AM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
posting of results script Sir-ihlatrebon Script Requests 0 02-17-04 11:46 AM
results page to display prev/next php-learner PHP 11 01-12-04 09:40 AM
Database storage of the results of a cardclub Xirtam Script Requests 0 12-15-03 11:16 AM
query results in multiple columns SnowCrash PHP 2 10-09-03 03:08 AM
Using CDONTS to email Checkbox results AkaMadDiSk ASP 1 07-23-03 05:32 PM


All times are GMT -5. The time now is 10:26 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.