David.J
David.J

Reputation: 33

Able to display one row but not loop it through

I'm working on displaying data on a table. It works to display one row of data but I can't seem to loop it. It keeps saying "Undefined offset: 0", "* *", "*Undefined offset: 2 *" and keeps looping the error to the number of my results.

Here is my code: (this does not work)

 <?php
        for ($i = 0; $i < count($Results); $i++) {
            echo "
             <tr>
            <td>{$Results[$i]['Namn']}</td>  
            <td>{$Results[$i]['Efternamn']}</td
            <td></td>

             </td>
       </tr>
    ";
        }
        ?>

This works when I'm displaying one row only.

 <?php

            echo "
             <tr>
            <td>{$Results['Namn']}</td>
            <td>{$Results['Efternamn']}</td>
            <td></td>

             </td>
       </tr>
    ";

        ?>

Edit: How I get $Results:

This is from my controller layer (MVC)

$displayResults = new Sok($name, $aftername);

    $Results = $displayResults->getSearchResult();

    include "../view/test.php";

Upvotes: 0

Views: 29

Answers (2)

Jenis Patel
Jenis Patel

Reputation: 1615

Just try to implement it such a way :

<?php
        foreach ($Results as $result) {
            echo "
             <tr>
            <td>{$result['Namn']}</td>  
            <td>{$result['Efternamn']}</td
            <td></td>
             </td>
       </tr>";
        }
 ?>

Hope it helps....

Upvotes: 0

Affan
Affan

Reputation: 1140

 if your loop contain name [0] , name [1] then write $Results['Namn'][$i]

and if it not show error at $Results['Namn'] it means $Results['Namn'] is not an array further (no 0 , 1 , .... position of $Results['Namn'])

Upvotes: 1

Related Questions