Welcome Guest, Not a member yet? Register   Sign In
Entity class - loop 20 rows and iterate through columns in view
#1

(This post was last modified: 08-27-2021, 08:50 AM by Corsari.)

Hello

I think I miss something and also maybe I don't do the right approach , so lot of thanks for any suggestion


I have an entity class named Ticket and in the TicketModel I take advantage of the Ticket entity class


Now the point


the Home controller I have this "list" method


PHP Code:
public function list($quantity$offset)
 {

 
$ticketModel = new TicketModel();

 
$ticketData $ticketModel->listTickets($quantity$offset);

 
$data = [];

 
$data['tRows'] = $ticketData;

 return 
view("Home/index"$data);

 } 
then if I browse to

myapp.local/home/list/0/25

as the entity class is conceived, in the view Home/index.php I then can use this

PHP Code:
<tbody>
    <?php foreach ($tRows as $tKey):
        echo "<tr>";

        echo "<td>";
        echo $tKey->id;
        echo "</td>";

        echo "<td>";
        echo $tKey->name;
        echo "</td>";

        echo "</tr>";                        
    
endforeach; ?>
</tbody> 
where, as you know, 'id' and 'name', thanks to the entity class, are exactly the DB table columns name


so far, so good, with this approach it works, but I have more columns to print... so it comes the point that I can't succeed.


What it doesn't work in my attempts,


is that $tKey is an object (isn't it?) and despite having tried some approaches , I can't get rid of doing the iteration of the $tKey attributes, they are 17 attributes mean 17 columns


I mean that instead of echoing so many repetitions of these three lines, one for each $tkey  attribute


PHP Code:
        echo "<td>";
        echo $tKey->property;
        echo "</td>"

rather I'd found of higher cleanness to have a nested foreach that iterates through all the attributes (formerly, given the entity class, through all the columns of the current database row)


something like


PHP Code:
<tbody>
    <?php foreach ($tRows as $tKey):
        echo "<tr>";

        foreach ($tKey as $key => $value){
            echo "<td>";
            echo $value;
            echo "</td>";
        }

        echo "</tr>";                        
    
endforeach; ?>
</tbody> 


well, I have tried almost everything and I can't get rid of have it working


also, with the first approach the limit of 25 rows is obey


while with the second nested foreach , in the resulting HTML, I can see that are iterated ALL the rows in the database (some 300 rows)


and the resulting HTML is just 300 times

...
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
... 300 times ...


nothing printed inside the tr tag, neither the td tag, so it does mean that it neither enters in the foreach


Thank you in advance for any hint regarding this topic


Also if you think my approach is wrong, thank you for hinting the better one

This is what I see if I dd($tKey)


[Image: attributes.png]
Reply


Messages In This Thread
Entity class - loop 20 rows and iterate through columns in view - by Corsari - 08-27-2021, 08:19 AM



Theme © iAndrew 2016 - Forum software by © MyBB