Jump to content

[SOLVED] combining two WHILE statements into one


dachshund

Recommended Posts

Ok here is everything.

 

Sorry for the mess. Been working on this quite quickly.

 

 


<?php include "../template/header.php"; ?>

<div id="content">

<?php

$id=mysql_real_escape_string($_GET['id']);
if ($id != (int)$id) {
	echo "Invalid ID";

} else {
    	$sql = "UPDATE content SET views=views+1 WHERE id='$id'";
    	mysql_query($sql) or die (mysql_error());
	$sql="SELECT * FROM content WHERE id='$id'";
    	$result = mysql_query($sql) or die (mysql_error());
    	$rows=mysql_fetch_array($result);
	$commentsql="SELECT * FROM comments WHERE articleid ='$id' ORDER BY id DESC LIMIT 10";
	$commentresult=mysql_query($commentsql) or die (mysql_error());
	$query="SELECT * FROM users WHERE id = '$sessionname'";
	$userresult=mysql_query($query);
	$sessionname=$_SESSION['uid'];
	$articleid=$rows['id'];
?>
    
    <div id="description">
    <? echo $rows['type']; ?> <? echo $rows['section']; ?>
    </div>
    
    <div id="viewtitle">
    <? echo $rows['title']; ?>
    </div>
    
    <div id="maincontent">
    <? echo $rows['maincontent']; ?>
    </div>
    
    <div id="viewlink">
    <a href="http://<? echo $rows['link']; ?>"><? echo $rows['link']; ?></a>
    </div>
    
    <div id="comments">
    
    <?php

}	
if($sessionname){

	echo "<ul>";
	echo "<li>Leave a comment</li>";
	echo "<li>";
	echo "<form name=\"comment\" method=\"post\" action=\"";
	include $_SERVER['DOCUMENT_ROOT'];
	echo "/test/commentposted.php?id=";
	echo $articleid;
	echo "\">";
	echo "<textarea name=\"comment\" rows=\"5\"></textarea>";
	echo "</li>";
	echo "<li>";
	echo "<input type=\"submit\" name=\"submitcomment\" value=\"Comment\">";
	echo "</form>";
	echo "</li>";
	?>
        
        <?php
	while($commentrows=mysql_fetch_array($commentresult)){
	?>
        
        
        <li>
	<img src="/images/profiles/<? echo $userrow['id'] ?>/profilepicture.jpg" width="40"/> <? echo $userrow['username']; ?>
	</li>
	<li>
	<? echo $commentrows['comment']; ?>
	</li>
	</ul>
        
        <?php
	}
	mysql_close();
	?>
    
    <?

mysql_connect("blah, blah, blah");

}else	{

	?>
	<ul><li>You must be logged in to post a comment.</li>
        
        <?
	while($commentrows=mysql_fetch_array($commentresult)){

	?>
        
	<li>
	<? echo $userrow['username']; ?>
	</li>
	<li>
	<? echo $commentrows['comment']; ?>
	</li>
	</ul>
        
        <?
}
}

mysql_close();
?>
    
    </div>
    
</div>

<?php include "../template/footer.php"; ?>

 

no that's all fine.

 

the issue i'm having is that i have to do

 

<while (blah blah){>

 

print comments

 

< mysql_close >

 

for the if statement

 

and then i have to re open the connection and do the same again for the else.

 

i was just wondering if there was a way to avoid this.

Just don't close the connection til the end. do a mysql_connect() at the beginning of all your queries etc and then do the if/else and then close at the end. Put mysql_close(); right before <?php include "../template/footer.php"; ?> Only need the one connection for the whole thing.

ah yeah that works. i did try that before but it didn't work then. hmmm.

 

anyway, now i have the problem that the $userrow['username'] is not echo'd because it is not in the while statement

 

i need to somehow intergrate

 

$userrow=mysql_fetch_array($userresult);

 

and

 

while($commentrows=mysql_fetch_array($commentresult)){

 

is that possible?

 

 

In the main commentrows while loop, you could try do a query for that users_id. So you could do:

$userQuery = mysql_query("SELECT * FROM users WHERE id = '$sessionname'");
$userresult = mysql_fetch_array($userQuery);

 

if there is a different user for each comment, you will need to change that $sessionname to $commentrows['id'] or whatever you use that has the ID of the commented user.

 

Then you could just do $userresult['username']; and so forth.

Your $userresult is just going to be the user that is logged in, not the user that left the comment.  Why not have the comment query join to the users table and get all that information in a single result:

 

      $commentsql="SELECT * FROM comments JOIN users ON comments.userid = users.userid WHERE articleid ='$id' ORDER BY id DESC LIMIT 10";

 

then you have all of the information in a single while loop

yeah sorry i just realised it would only be the one currently logged on. such a stupid mistake.

 

DavidAM: that looks perfect but it returns Unknown column 'users.userid' in 'on clause'.

 

at the moment i have

 


$commentsql="SELECT * FROM comments JOIN users ON comments.userid = users.userid WHERE articleid ='$id' ORDER BY id DESC LIMIT 10";
$commentresult=mysql_query($commentsql) or die (mysql_error());

while($commentrows=mysql_fetch_array($commentresult)){

        
<li>
<? echo $commentrows['username']; ?>
</li>
<li>
<? echo $commentrows['comment']; ?>
</li>
</ul>

 

does that seem ok?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.