One foreach loop and two arrays

67
May 03, 2021, at 5:30 PM

I have a little issue with my foreach loop and can't find the mistake.

first array

$gemeinden = [$barsbek_G, $barsbek_U, $barsbek_F, $bendfeld_G, $bendfeld_U, $bendfeld_F, $brodersdorf_G, $brodersdorf_U, $brodersdorf_F, $fahren_G, $fahren_U, $fahren_F, $fiefbergen_G, $fiefbergen_U, $fiefbergen_F, $hoehndorf_G, $hoehndorf_U, $hoehndorf_F, $koehn_G, $koehn_U, $koehn_F, $krokau_G, $krokau_U, $krokau_F, $krummbek_G, $krummbek_U, $krummbek_F, $laboe_G, $laboe_U, $laboe_F, $lutterbek_G, $lutterbek_U, $lutterbek_F, $passade_G, $passade_U, $passade_F, $prasdorf_G, $prasdorf_U, $prasdorf_F, $probst_G, $probst_U, $probst_F, $schoenberg_G, $schoenberg_U, $schoenberg_F, $stakendorf_G, $stakendorf_U, $stakendorf_F, $stein_G, $stein_U, $stein_F, $stoltenberg_G, $stoltenberg_U, $stoltenberg_F, $wendtorf_G, $wendtorf_U, $wendtorf_F, $wisch_G, $wisch_U, $wisch_F];

second array

$results = $wpdb->get_results( $queryGemeinde );

That's my foreach loop

<?php foreach($results as $index => $print) { ?>
                <tr>    <!-- display data -->
                    <td width=<?php echo $width[0]; ?>><?php echo $print->postleitzahl; ?></td>
                    <td width=<?php echo $width[1]; ?>><?php echo $print->ort; ?></td>
                    <td width=<?php echo $width[1]; ?>><?php echo $gemeinden[$index]; ?></td>
                    <td width=<?php echo $width[1]; ?>><?php echo $gemeinden[$index]; ?></td>
                    <td width=<?php echo $width[1]; ?>><?php echo $gemeinden[$index]; ?></td>
                </tr>               
        <?php }?>
    </tbody>
</table>

That's the result

It seems as if it is stucked. And it makes sense but how Can I tell the foreach loop to go further after the third <td></td>

Answer 1

As per my understanding you need to fetch next item after 3rd td so you can use below approach:

<?php foreach($results as $index => $print) { ?>
                <tr>    <!-- display data -->
                    <td width=<?php echo $width[0]; ?>><?php echo $print->postleitzahl; ?></td>
                    <td width=<?php echo $width[1]; ?>><?php echo $print->ort; ?></td>
                    <td width=<?php echo $width[1]; ?>><?php echo $gemeinden[$index]; ?></td>
                    <?php $index++; ?>
                    <td width=<?php echo $width[1]; ?>><?php echo $gemeinden[$index]; ?></td>
                    <?php $index++; ?>
                    <td width=<?php echo $width[1]; ?>><?php echo $gemeinden[$index]; ?></td>
                </tr>               
        <?php }?>
    </tbody>
</table>

Or you can even iterate loop as below as well:

<?php foreach($results as $index => $print) { ?>
                <tr>    <!-- display data -->
                    <td width=<?php echo $width[0]; ?>><?php echo $print->postleitzahl; ?></td>
                    <td width=<?php echo $width[1]; ?>><?php echo $print->ort; ?></td>
                    <?php foreach($gemeinden as $gemValue) { ?> 
                         <td width=<?php echo $width[1]; ?>><?php echo $gemValue; ?></td>
                    <?php } ?>
                </tr>               
        <?php }?>
    </tbody>
</table>

Hope it helps you!!

READ ALSO
Is PowerMock using Java agents to instrument Java code?

Is PowerMock using Java agents to instrument Java code?

PowerMock is able to mock final classes because it modifies the bytecodeIs PowerMock using a Java Agent to do that or something else?

67
iterative through json data in flask application

iterative through json data in flask application

I've a sample flask application

67
how do i rectify the undefined output for this.Status?

how do i rectify the undefined output for this.Status?

My AJAX code for a reqres server doe not display the desired output or it does not update the default text when hit on submitenter image description here

25
How to write an equation with a changing variable and a click counter, but the result stays the same? [closed]

How to write an equation with a changing variable and a click counter, but the result stays the same? [closed]

Want to improve this question? Update the question so it focuses on one problem only by editing this post

23