Jump to content

One instance of a value in a while loop


Go to solution Solved by taquitosensei,

Recommended Posts

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 
		} 
	?>
Link to comment
https://forums.phpfreaks.com/topic/303272-one-instance-of-a-value-in-a-while-loop/
Share on other sites

  • Solution

 

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>";
}

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. 

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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