简体   繁体   English

通过php中的索引选择性地通过多维关联数组进行交互

[英]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. 我正在使用php开发一个网站,我从sql server获取数据。 What I need to do is there are 2 rows which will contain 3 columns of data each. 我需要做的是有2行,每行包含3列数据。 The associative array I store the data has 6 sub-arrays. 我存储数据的关联数组有6个子数组。 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. 我需要创建一个循环,在3个条目之后,创建一个新行,并在下一行打印下三个数据。

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. 我尝试使用while循环,但我不知道如何通过它们的索引分离数组,而while($ index = mysqli_fetch_assoc(r))将打印所有数据,但我希望3在一行中,3在下一行。 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. 我需要的是,在第一行,我需要前3个阵列数据,然后在下一行,我需要其他3个。

You can apply a condition in the loop and check for $iterator%3==0 , if condition matches increment the array index 您可以在循环中应用条件并检查$iterator%3==0 ,如果条件匹配则增加数组索引

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. 现在你取结果集中只有一行,你必须使用while循环来遍历结果集并适当地填写使用每一列for循环。

<?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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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