Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

andrydox's avatar

Break looping while in php

hi guys hope you are well, i'am still learning and new in php, now i'm on progress develop simple website but i have a problem with array and looping, how to stop looping when all data has been output.

<form method="POST" action="storedata_g6b.php">
	<table class="table">
		<thead class="thead-dark">
			<tr>
				<th class="col-3">id</th>
				<th class="col-7">nama</th>
				<th class="col-3">rank</th>
			</tr>
		</thead>
		<tbody>
		<?php
			$rank_b = 'B';
			$sql = "select t_empeval.id, t_empeval.nama from t_empeval Where t_empeval.grade='$c' and sect=CONCAT('$a','$b') order by t_empeval.v1 desc limit ".$var1.",".$var2."";
			$query = mysqli_query($koneksi, $sql);
			if(!$query)
			{
				die('SQL Error :' . mysqli_error($koneksi));
			}
			$b=0;
			while ($array_b = mysqli_fetch_assoc($query))
			{
				if(mysqli_num_rows($query) > 0)
				{
		?>
					<tr>
						<td><input type="text" name="id_b[<?php echo $b; ?>]" value="<?php echo $array_b['id']; ?>"></td>
						<td><input type="text" name="nama_b[<?php echo $b; ?>]" value="<?php echo $array_b['nama']; ?>"></td>
						<td><input type="text" name="rank_b[<?php echo $b; ?>]" value="<?php echo $rank_b; ?>"></td>
					</tr>
		<?php
					$b++;
				}
			}
		?>
			<input type="hidden" name="cotton_b" value="<?php echo $b; ?>" />
		</tbody>
	</table>
	<input type="submit" id="submit" name="submit"/>
</form>

in this case amount of data will be display is 2 row.

example : 1 => a 2 => a

then if amount of data has been displayed then will break the loop and start other loop. please englithment me, thanks in advance.

0 likes
4 replies
jlrdw's avatar

Should be just a logic problem using some if statements for example notice I went from expense to income in this report.

It did take a little trial and error to work out the details.

It helps to write the logic out with pencil and paper sometimes first.

But you can stop the loop with break.

You are using MySQLi, most here use PDO and then a foreach loop.

andrydox's avatar

what variable should i use for parameter for if ? so i can use as variable for break the loop

jlrdw's avatar

In my example I was looking for expense to change to income, not knowing all of your data you looking for whatever change you need.

Like if the top is cats you look for dog for the change if the top is cats in the bottom dogs.

Just an example there. Again this is just a logic problem work it out with pencil and paper first.

Sometimes it takes more than a single if statement.

I would suggest you take some of Jefferies free video training lessons, he has one covering PHP.

Snapey's avatar

You should be aware that code such as this is frowned upon these days. You don't want to be mixing database queries and presentation in the same file.

If you are learning then that fine, but choose good sources to learn from.

Laracasts has excellent getting started courses.

Please or to participate in this conversation.