dachshund Posted August 28, 2009 Share Posted August 28, 2009 is it possible to combine these? <?php while($commentrows && =mysql_fetch_array($commentresult)){ while($userrow=mysql_fetch_array($userresult)){ ?> Quote Link to comment https://forums.phpfreaks.com/topic/172286-solved-combining-two-while-statements-into-one/ Share on other sites More sharing options...
dachshund Posted August 28, 2009 Author Share Posted August 28, 2009 sorry meant while($commentrows=mysql_fetch_array($commentresult)){ for the first line Quote Link to comment https://forums.phpfreaks.com/topic/172286-solved-combining-two-while-statements-into-one/#findComment-908382 Share on other sites More sharing options...
ReKoNiZe Posted August 28, 2009 Share Posted August 28, 2009 I've read your other post about the comments, why are you needing to do two seperate queries? Quote Link to comment https://forums.phpfreaks.com/topic/172286-solved-combining-two-while-statements-into-one/#findComment-908384 Share on other sites More sharing options...
dachshund Posted August 28, 2009 Author Share Posted August 28, 2009 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"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/172286-solved-combining-two-while-statements-into-one/#findComment-908392 Share on other sites More sharing options...
ReKoNiZe Posted August 28, 2009 Share Posted August 28, 2009 Okay so it looks like you're wanting to associate the username with the comment they make? Quote Link to comment https://forums.phpfreaks.com/topic/172286-solved-combining-two-while-statements-into-one/#findComment-908396 Share on other sites More sharing options...
dachshund Posted August 28, 2009 Author Share Posted August 28, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/172286-solved-combining-two-while-statements-into-one/#findComment-908401 Share on other sites More sharing options...
ReKoNiZe Posted August 28, 2009 Share Posted August 28, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/172286-solved-combining-two-while-statements-into-one/#findComment-908404 Share on other sites More sharing options...
dachshund Posted August 28, 2009 Author Share Posted August 28, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/172286-solved-combining-two-while-statements-into-one/#findComment-908409 Share on other sites More sharing options...
ReKoNiZe Posted August 28, 2009 Share Posted August 28, 2009 What are you using the userrow for? I see that it provides an image and name to the comments but that information seems like it should be in your other table with the comments. Quote Link to comment https://forums.phpfreaks.com/topic/172286-solved-combining-two-while-statements-into-one/#findComment-908493 Share on other sites More sharing options...
dachshund Posted August 28, 2009 Author Share Posted August 28, 2009 the $userrow gets information from the users table like their username and profilepicture. the comments table just stores which user id the comment was posted by. hope i'm making sense. Quote Link to comment https://forums.phpfreaks.com/topic/172286-solved-combining-two-while-statements-into-one/#findComment-908513 Share on other sites More sharing options...
dachshund Posted August 28, 2009 Author Share Posted August 28, 2009 while($commentrows=mysql_fetch_array($commentresult)){ while($userrow=mysql_fetch_array($userresult)){ that comes up blank. all i need is some way to write it cohesively. anyone? Quote Link to comment https://forums.phpfreaks.com/topic/172286-solved-combining-two-while-statements-into-one/#findComment-908552 Share on other sites More sharing options...
ReKoNiZe Posted August 28, 2009 Share Posted August 28, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/172286-solved-combining-two-while-statements-into-one/#findComment-908563 Share on other sites More sharing options...
DavidAM Posted August 28, 2009 Share Posted August 28, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/172286-solved-combining-two-while-statements-into-one/#findComment-908586 Share on other sites More sharing options...
dachshund Posted August 28, 2009 Author Share Posted August 28, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/172286-solved-combining-two-while-statements-into-one/#findComment-908590 Share on other sites More sharing options...
dachshund Posted August 28, 2009 Author Share Posted August 28, 2009 sorry it was my error. just because it was called id not userid. works now! finally! thank you very much. Quote Link to comment https://forums.phpfreaks.com/topic/172286-solved-combining-two-while-statements-into-one/#findComment-908599 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.