Page 1 of 1
wont loop
Posted: Mon Jun 14, 2004 9:03 pm
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
Posted: Mon Jun 14, 2004 9:15 pm
by kettle_drum
Try mysql_fetch_assoc() instead.
Posted: Mon Jun 14, 2004 9:28 pm
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
}
?>
Posted: Mon Jun 14, 2004 9:45 pm
by pinehead18
phenom was their a change becuase it does the same thing still.
Thanks
Anthony
Posted: Mon Jun 14, 2004 9:59 pm
by John Cartwright
Plz define "cannot make loop"... what is it doing? is it outputting just 1 field from your db?
Posted: Mon Jun 14, 2004 10:02 pm
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 { ?>
<tr>
<td width="15%">ID:<?php echo $row_rs_pressї'PRID']; ?></td>
<td width="36%">Date:<?php echo $row_rs_pressї'PRDate']; ?></td>
<td width="49%">Title:<?php echo $row_rs_pressї'PRTitle']; ?></td>
</tr>
<tr>
<td> </td>
<td colspan="2">Intro:<?php echo $row_rs_pressї'PRIntro']; ?></td>
</tr>
<tr>
<td align="right">Story:</td>
<td colspan="2" rowspan="4" align="left" valign="top"><?php echo $row_rs_pressї'PRStory']; ?></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
<?php } 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
Posted: Mon Jun 14, 2004 10:05 pm
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>';
?>
Posted: Mon Jun 14, 2004 10:13 pm
by Cateyes
I think he is trying to make a dynamic page but I could be wrong.
Posted: Tue Jun 15, 2004 12:01 am
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
Posted: Tue Jun 15, 2004 6:57 am
by Grim...
Posted: Tue Jun 15, 2004 9:34 am
by John Cartwright
Grim... wrote:Instead of
try
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