How can i skip the first 4 data loops in a foreach php loop?

<?php
		$db = mysqli_connect('localhost', 'root', '', 'db_test');
		$data = mysqli_query($db, 'SELECT * FROM imgs ORDER BY id');
		$mydata = array();
		while($row = mysqli_fetch_assoc($data)) {
			$img = array();
			$img['id'] = $row['id'];
			$mydata[] = $img;
		}
		mysqli_free_result($data);
	?>
     	<div id="shuffle">
		<?php
		$i = 0;
		foreach ($mydata as $key => $img) {
			if($i == 4) {
				$i = 0;
			}
						if($i == 0) {
				echo '<div class="col-lg-12 imgwrap">';
			}
			?>

			<?php
			if($i == 3) {
				echo '</div>';
			}
			$i++;
		}
		?>

Is there a way so that i can skip the first 4 loops of data ? :slight_smile: :slight_smile:

Try searching for “Php MySQLi_query limit”

The query result will be limited to where the limit should start and how many rows you would like to return.

Okay i will look at it right away :slight_smile:

Why does this not work?

This is what i have that works:

$data = mysqli_query($db, 'SELECT * FROM videos ORDER BY id DESC

Then i wanted to skip the first 0,1,2,3 videos by adding LIMIT 4, 10 but it does not work?

looks like this
$data = mysqli_query($db, ‘SELECT * FROM videos ORDER BY id DESC LIMIT 4, 10’);

Did you manage to solve your problem?

I tested your $SQL statement on a database and because you are using DESC it got the last 10 rows:

    $sql = 'SELECT * FROM videos ORDER BY id DESC LIMIT 0, 10'; 
    // id = 3300 .. 3291

    $sql = 'SELECT * FROM videos ORDER BY id DESC LIMIT 4, 10'; 
    // id = 3296 .. 3287

If you want records 5…14 you must use ASC

    $sql = 'SELECT * FROM videos ORDER BY id ASC LIMIT 4, 10'; // 3296 .. 3287
    // id = 5 .. 14 

Special Note:
ASC is the default and therefore no need to declare.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.