mdmartiny Posted March 8, 2017 Share Posted March 8, 2017 I am writing a script for a personal mail system in a CMS system.It produces multiple dynamic divs one the page listing out all of the messages. I have done this using a while loop. At the bottom of the page, I have placed a reply box. I need some of the information from the query in the while loop but I can't get the info outside of the while loop. This is the code that I have so far. I have tried using the counter == 0 on it and that does not work. I have tried doing a variable $message = '' on the outside of the loop but that does not work. Line 42 and 44 is where I am trying to place the variables. <div class="row compose-form"> <?php $id = $_GET['id']; $que = "SELECT mail.*, CONCAT(s_t.first, ' ', s_t.last) AS 'to', CONCAT(s_b.first, ' ', s_b.last) AS 'for' FROM mail INNER JOIN users AS s_t ON mail.sent_to = s_t.id INNER JOIN users AS s_b ON mail.sent_by = s_b.id WHERE mail.id = '$id' OR mail.replied_to = '$id' ORDER BY mail.sent DESC"; $res = mysqli_query($dbc, $que); $counter = 0; while ($message = mysqli_fetch_assoc($res)) { if($counter == 0) { echo "<div class='email-header' style='border-bottom: none; margin-bottom: 25px;'>"; echo "<P>".$message['subject']."</P>"; echo "</div>"; } $counter++; ?> <div class="row"> <div class="col-md-12"> <div class="col-md-6"><span class="to"> <strong><?php echo $message['to']; ?></strong></span></div> <div class="col-md-6"><span class="to pull-right"> <a href="#" class="show_hide" rel="#slidingDiv_<?php echo $counter; ?>"><i class="fa fa-caret-square-o-down" aria-hidden="true"></i></a> <?php echo date("m-d-Y", strtotime($message['sent']));?> (<?php echo getTimeSince($message['sent']).' ago'; ?>)</span> </div> </div> </div> <div class = "slidingDiv" id="slidingDiv_<?php echo $counter; ?>"> <div class="row"> <div class="col-md-12"> <div class="email-content"> <?php echo $message['body']; ?> </div> </div> </div> </div> <hr class="mail-controls-divider"> <?php } ?> <div class="col-md-12"> <form role="form" class="form-horizontal" action="<?php echo MAIL_URL; ?>/index.php?page=email&id=<?php echo $_GET['id']; ?>" method="post" enctype="multipart/form-data"> <div class="form-group"> <input type="hidden" name="from" value="<?php echo $user['id']; ?>"> <input type="hidden" name="for" value="<?php echo $message['sent_to']; ?>"> <input type="hidden" name="date" value="<?php echo date('Y/m/d h:i:s a', time()); ?>"> <input type="hidden" name="subject" value="<?php echo $message['subject']; ?>"> <input type="hidden" name="originalMessage" value="<?php echo $_GET['id']; ?>"> </div> <div class="form-group"> <div class="col-md-12"> <textarea class="form-control" name="inputBody" id="inputBody" rows="5"></textarea> </div> </div> <div class="form-group"> <div class="col-sm-12"> <input type="reset" class="btn btn-warning" name="clear" id="clear" value="Clear"> <input type="submit" class="btn btn-primary" name="sent" id="sent" value="send"> </div> </div> </form> </div> </div> Quote Link to comment Share on other sites More sharing options...
requinix Posted March 8, 2017 Share Posted March 8, 2017 Think about why it isn't working. When does the while loop end? Yes, I know, when there are no more messages in the result from the query, but technically speaking when will the loop end? That's why you can't use $message. And the $counter thing won't help because it does output up in the divs when you need the data down in the reply box. You're ordering the messages in chronological order so the last message processed will be the most recent and the one the user is logically replying to. Try changing your loop so that the problem with $message doesn't happen. Quote Link to comment Share on other sites More sharing options...
mdmartiny Posted March 8, 2017 Author Share Posted March 8, 2017 How else would I set it up differently? This is how I have always set up while loops. Quote Link to comment Share on other sites More sharing options...
zamight Posted March 9, 2017 Share Posted March 9, 2017 Currently, way your code is the setup you can only access the $message array inside the scope of your while loop. Anything else trying to access the $message array outside the scope of the while loop will be null. Quote Link to comment Share on other sites More sharing options...
NigelRel3 Posted March 9, 2017 Share Posted March 9, 2017 (As stated before) If you consider what happens why the loop ends - it's when $message = mysqli_fetch_assoc($res) Returns NULL to show there are no more rows. So after this - $message will contain NULL and not any message at all. Also consider that even if it did contain a message - it may not be the one you want. If you want the first message - the one where $counter is 0, perhaps you could store this message somewhere and use those values further down. Quote Link to comment 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.