简体   繁体   中英

Selectively interating through an multidimentional associative array by their indexes in php

I m developing a website using php and I am fetching the data from sql server. What I need to do is there are 2 rows which will contain 3 columns of data each. The associative array I store the data has 6 sub-arrays. I need to create a loop where after 3 entries, a new row is created and the data of next three in printed in next row.

I tried using while loop but I dont know how I can separate the array by their indexes as doing while($index = mysqli_fetch_assoc(r)) will print all the data but I want 3 to be in one row and 3 in the next. Thanks!

        <?php
        $q = "SELECT * FROM main_intro";
        $r = mysqli_query($link, $q) or die(mysqli_error($link));
        $num_rows = mysqli_num_rows($r);
        $count = 0;
        $intro = mysqli_fetch_assoc($r);
        $keys = array_keys($intro) ?>

        <div class="row custom_pad">
            <?php
            for ($i = 0; $i < 3; $i++) {
                ?>
                <div class="col-md-4">
                    <h2><?php echo $intro['image']; ?><?php echo $intro['heading']; ?></h2>
                    <p><?php echo $intro['data']; ?></p>
                </div>
            <?php} ?>
            <!-- -->
        </div>

What I need is, in first row, i need the first 3 array data and then in next row, i need the other 3.

You can apply a condition in the loop and check for $iterator%3==0 , if condition matches increment the array index

For example

$arr = [];
$j=0;
for($i=1;$i<=9;$i++){
  $arr[$j][] = $i;
  if($i%3==0) $j++;  
}
echo '<pre>';
print_r($arr);

Result

Array
(
[0] => Array
    (
        [0] => 1
        [1] => 2
        [2] => 3
    )

[1] => Array
    (
        [0] => 4
        [1] => 5
        [2] => 6
    )

[2] => Array
    (
        [0] => 7
        [1] => 8
        [2] => 9
    )

  )

Looks like you want to populate three columns of each row with appropriate data. Right now you are fetching just one row from the result set, you have to use while loop to loop through the result set and appropriately fill each column using that for loop.

<?php
$q = "SELECT * FROM main_intro";
$r = mysqli_query($link, $q) or die(mysqli_error($link));
while($intro = mysqli_fetch_assoc($r)){
    ?>
    <div class="row custom_pad">
        <?php
        for ($i = 0; $i < 3; $i++) {
            ?>
            <div class="col-md-4">
                <?php
                    switch($i){
                        case 0:
                            echo $intro['image'];
                            break;
                        case 1:
                            echo '<h2>' . $intro['heading'] . '</h2>';
                            break;
                        case 2:
                            echo '<p>' . $intro['data'] . '</p>';
                            break;
                    }
                ?>
            </div>
            <?php 
        } 
    ?>
    </div>
    <?php
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM