mdmartiny Posted February 23, 2017 Share Posted February 23, 2017 I am in the process of writing a personal mail system for something that I am working on. I am setting the system up that when you open the message. It shows the original message and all messages associated with the original message. I am doing this with a while loop. I do not want to, however, show the message subject with every iteration of the while loop. How do I make it only show the subject once and run through the rest of the while loop? This is the code that I have so far. <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)) { $counter++; ?> <div class="email-header"> <P><?php echo $subject['subject']; ?></P> </div> <div id="toggler-<?php echo $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"> <?php echo date("m-d-Y", strtotime($message['sent']));?> (<?php echo getTimeSince($message['sent']).' ago'; ?>)</span> </div> </div> </div> <div class="toggler-hide-<?php echo $counter; ?>"> <div class="row"> <div class="col-md-12"> <div class="email-content"> <?php echo $message['body']; ?> </div> </div> </div> </div> </div> <hr class="mail-controls-divider"> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/303272-one-instance-of-a-value-in-a-while-loop/ Share on other sites More sharing options...
Solution taquitosensei Posted February 23, 2017 Solution Share Posted February 23, 2017 I am in the process of writing a personal mail system for something that I am working on. I am setting the system up that when you open the message. It shows the original message and all messages associated with the original message. I am doing this with a while loop. I do not want to, however, show the message subject with every iteration of the while loop. How do I make it only show the subject once and run through the rest of the while loop? This is the code that I have so far. <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)) { $counter++; ?> <div class="email-header"> <P><?php echo $subject['subject']; ?></P> </div> <div id="toggler-<?php echo $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"> <?php echo date("m-d-Y", strtotime($message['sent']));?> (<?php echo getTimeSince($message['sent']).' ago'; ?>)</span> </div> </div> </div> <div class="toggler-hide-<?php echo $counter; ?>"> <div class="row"> <div class="col-md-12"> <div class="email-content"> <?php echo $message['body']; ?> </div> </div> </div> </div> </div> <hr class="mail-controls-divider"> <?php } ?> You've got a counter if($counter==0) { echo "<div class='email-header'>"; echo "<P>".$subject['subject']."</P>"; echo "</div>"; } Quote Link to comment https://forums.phpfreaks.com/topic/303272-one-instance-of-a-value-in-a-while-loop/#findComment-1543260 Share on other sites More sharing options...
mdmartiny Posted February 23, 2017 Author Share Posted February 23, 2017 Thanks I did not think of that. I was actually using the counter for something else but I will give that a try. Quote Link to comment https://forums.phpfreaks.com/topic/303272-one-instance-of-a-value-in-a-while-loop/#findComment-1543273 Share on other sites More sharing options...
mdmartiny Posted February 23, 2017 Author Share Posted February 23, 2017 I tried that and it did not work. It still iterates through every loop. Quote Link to comment https://forums.phpfreaks.com/topic/303272-one-instance-of-a-value-in-a-while-loop/#findComment-1543275 Share on other sites More sharing options...
taquitosensei Posted February 23, 2017 Share Posted February 23, 2017 first...does the counter need to be 1 or 0 on the first time through. You set it to zero. Then immediately ++ I would move the $counter++; to the end of the loop. Then echo $counter; somewhere in the loop to make sure that it's actually incrementing. 1 Quote Link to comment https://forums.phpfreaks.com/topic/303272-one-instance-of-a-value-in-a-while-loop/#findComment-1543276 Share on other sites More sharing options...
mdmartiny Posted February 23, 2017 Author Share Posted February 23, 2017 The counter is only there to count the number of divs that I have in in the while loop. I moved the counter++; to the end and it worked perfectly. Thank You. Quote Link to comment https://forums.phpfreaks.com/topic/303272-one-instance-of-a-value-in-a-while-loop/#findComment-1543278 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.