wont loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

wont loop

Post by pinehead18 »

Code: Select all

require('forum_head.php');
        $con = mysql_connect("localhost","pinehead","") or die(mysql_error());
        $db = mysql_select_db("lkcforum",$con) or die(mysql_error());
        $sql = "SELECT tid,uname,body,topic FROM threads WHERE tid='{$_GET['tid']}'";
        $result = mysql_query($sql,$con) or die(mysql_error());
        while ($row = mysql_fetch_array($result)) {

                $topic_name = $row['topicname'];
                $description = $row['topicdes'];
                $author = $row['uname'];
                $body = $row['body'];

        $db = mysql_select_db("pinehead",$con);
        $sql = "SELECT create_date FROM users WHERE user='anthony'";
        $result = mysql_query($sql,$con) or die(mysql_error());
        $row = mysql_fetch_array($result);
                $cdate = $row['create_date'];

$output = "
<table class="table2">
                <tr><td width=20% valign=top><font face=arial size=-1>Author</font></td><td width=80%
align=center>Message</td></tr>
                <tr><td width=20% valign=top><font face=arial size=-1>$author</font><br>
                                <font face=arial size=-1>Joined: $cdate</font><br>
                                 <a href=http://www..com/myprofile/$author/>View $author's profile</a>
                                <font face=arial size=-1>Posts: $posts<td width=80%>". nl2br($body) ."</font><br>
                </td>
                </tr>
                <tr>
                 <td><a href=post.php?tid=$tid>Reply</a></td>
                </tr>

</table>";

}
echo $output;

I dont' recieve any errors but i cannot make this loop, any ideas?

Thank you
Anthony
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Try mysql_fetch_assoc() instead.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

<?php
require('forum_head.php'); 
        $con = mysql_connect("localhost","pinehead","") or die(mysql_error()); 
        $db = mysql_select_db("lkcforum",$con) or die(mysql_error()); 
        $sql = "SELECT tid,uname,body,topic FROM threads WHERE tid='{$_GET['tid']}'"; 
        $result = mysql_query($sql,$con) or die(mysql_error()); 
        while ($row = mysql_fetch_array($result)) { 

                $topic_name = $row['topicname']; 
                $description = $row['topicdes']; 
                $author = $row['uname']; 
                $body = $row['body']; 

        $db = mysql_select_db("pinehead",$con); 
        $sql = "SELECT create_date FROM users WHERE user='anthony'"; 
        $result = mysql_query($sql,$con) or die(mysql_error()); 
        $row = mysql_fetch_array($result); 
                $cdate = $row['create_date']; 

$output = " 
<table class="table2"> 
                <tr><td width=20% valign=top><font face=arial size=-1>Author</font></td><td width=80% 
align=center>Message</td></tr> 
                <tr><td width=20% valign=top><font face=arial size=-1>$author</font><br> 
                                <font face=arial size=-1>Joined: $cdate</font><br> 
                                 <a href=http://www..com/myprofile/$author/>View $author's profile</a> 
                                <font face=arial size=-1>Posts: $posts<td width=80%>". nl2br($body) ."</font><br> 
                </td> 
                </tr> 
                <tr> 
                 <td><a href=post.php?tid=$tid>Reply</a></td> 
                </tr> 

</table>"; 

echo $output; // moved echo withen the loop because when it is outside the loop only the last entry in the db will be echod

} 


?>
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post by pinehead18 »

phenom was their a change becuase it does the same thing still.
Thanks
Anthony
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Plz define "cannot make loop"... what is it doing? is it outputting just 1 field from your db?
Cateyes
Forum Commoner
Posts: 63
Joined: Mon Jun 14, 2004 5:06 pm

Post by Cateyes »

OK I am really new at this not even 2 weeks but here is a sample that I use for loop that works.

Code: Select all

<table width="70%" border="1" cellpadding="1" cellspacing="1">
  
  <?php do &#123; ?>
<tr>
    <td width="15%">ID:<?php echo $row_rs_press&#1111;'PRID']; ?></td>
    <td width="36%">Date:<?php echo $row_rs_press&#1111;'PRDate']; ?></td>
    <td width="49%">Title:<?php echo $row_rs_press&#1111;'PRTitle']; ?></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td colspan="2">Intro:<?php echo $row_rs_press&#1111;'PRIntro']; ?></td>
  </tr>
  <tr>
    <td align="right">Story:</td>
    <td colspan="2" rowspan="4" align="left" valign="top"><?php echo $row_rs_press&#1111;'PRStory']; ?></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
  <?php &#125; while ($row_rs_press = mysql_fetch_assoc($rs_press)); ?>
</table>
The first part is <?php do { ?> and the finishing part is <?php } while ($row_rs_press = mysql_fetch_assoc($rs_press)); ?>
I hope this helps
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

just wondering... but was you meaning to put multiple tables with the exact same class name?..

otherwise, you will need to put your echo '<table' statement before the while loop, and the echo '</table' statement after the loop.

not sure if this is what you are trying to do, but here is what i'm saying to do..

Code: Select all

<?php 
require('forum_head.php'); 
        $con = mysql_connect("localhost","pinehead","") or die(mysql_error()); 
        $db = mysql_select_db("lkcforum",$con) or die(mysql_error()); 
        $sql = "SELECT tid,uname,body,topic FROM threads WHERE tid='{$_GET['tid']}'"; 
        $result = mysql_query($sql,$con) or die(mysql_error()); 

//moved <table> tag to beginning of loop
       echo '<table class="table2">';

        while ($row = mysql_fetch_assoc($result)) { 

                $topic_name = $row['topicname']; 
                $description = $row['topicdes']; 
                $author = $row['uname']; 
                $body = $row['body']; 

        $db = mysql_select_db("pinehead",$con); 
        $sql = "SELECT create_date FROM users WHERE user='anthony'"; 
        $result = mysql_query($sql,$con) or die(mysql_error()); 
        $row = mysql_fetch_array($result); 
                $cdate = $row['create_date']; 

$output = "<tr><td width=20% valign=top><font face=arial size=-1>Author</font></td><td width=80% 
align=center>Message</td></tr> 
                <tr><td width=20% valign=top><font face=arial size=-1>$author</font><br> 
                                <font face=arial size=-1>Joined: $cdate</font><br> 
                                 <a href=http://www..com/myprofile/$author/>View $author's profile</a> 
                                <font face=arial size=-1>Posts: $posts<td width=80%>". nl2br($body) ."</font><br> 
                </td> 
                </tr> 
                <tr> 
                 <td><a href=post.php?tid=$tid>Reply</a></td> 
                </tr> "; 

echo $output; // moved echo withen the loop because when it is outside the loop only the last entry in the db will be echod 

} 
//moved </table> tag to end of loop
echo '</table>';

?>
Cateyes
Forum Commoner
Posts: 63
Joined: Mon Jun 14, 2004 5:06 pm

Post by Cateyes »

I think he is trying to make a dynamic page but I could be wrong.
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post by pinehead18 »

I'm trying to make a page that lists all the fields in the db. The funny thing is i do this all the time, and i think i just had a brain fart or something. Or i did it backwards.....

It only displays one fiedl and not all of them in the db.

Thank you
Anthony
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Instead of

Code: Select all

$output = ...
try

Code: Select all

$output .= ...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Grim... wrote:Instead of

Code: Select all

$output = ...
try

Code: Select all

$output .= ...
only do that if the echo is outside of the loop...

or else you'll get something like this

output1

output1
output2

output1
output2
output3
Post Reply