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
#2

(This post was last modified: 08-27-2021, 11:53 AM by ikesela.)

I try it and got answer :

foreach ($tRows as $tKey):
echo "<tr>";
$tnewKey = $tKey->jsonSerialize();
foreach ($tnewKey as $key => $value){
echo "<td>";
echo $value;
echo "</td>";
}

echo "</tr>";
endforeach;
Reply
#3

(This post was last modified: 08-28-2021, 12:32 AM by Corsari.)

WOW !

First of all, thank you for the help!

Cool, I'll try that this afternoon when at the PC.

How did you got the jsonSerialize() hint/idea? Thank you for this curiosity
Reply
#4

PHP.net - JsonSerializable::jsonSerialize
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(This post was last modified: 08-28-2021, 04:01 AM by Corsari.)

(08-27-2021, 11:51 AM)ikesela Wrote: I try it and got answer :

PHP Code:
foreach ($tRows as $tKey):
        echo "<tr>";
      $tnewKey $tKey->jsonSerialize(); 
        foreach ($tnewKey as $key => $value){
            echo "<td>";
            echo $value;
            echo "</td>";
        }

        echo "</tr>";                        
    
endforeach; 

hello, as stated above, I tried it and it works ... though one weird thing happen


model
PHP Code:
public function listTickets($quantity$offset)
 {

 
$ticketModel = new TicketModel();

 
$data $ticketModel->findAll($quantity$offset);
      
        
return $data;




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

 
$ticketModel = new TicketModel();

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

 
$data = [];

 
$data['tRows'] = $ticketData;

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

 } 

depite I browse myapp.local/home/list/25/0

they are printed ALL the 300 rows of records in the database

I mean, the resulting HTML table, doesn't have 25 rows, instead it has 300 rows

Wondering: how is this possible?.
Whilst if I use the original code at the begin of this thread, they only prints the expected 25 rows .. really confused about this behavior
Reply
#6

(This post was last modified: 08-28-2021, 08:10 AM by ikesela.)

(08-28-2021, 12:31 AM)Corsari Wrote: WOW !

First of all, thank you for the help!

Cool, I'll try that this afternoon when at the PC.

How did you got the jsonSerialize() hint/idea? Thank you for this curiosity

if you use dd() to see detail, the entities will show two tabs: [properties , available methods] (FOUND HERE IN AVAILABLE METHODS)

(08-28-2021, 03:59 AM)Corsari Wrote:
(08-27-2021, 11:51 AM)ikesela Wrote: I try it and got answer :

PHP Code:
foreach ($tRows as $tKey):
        echo "<tr>";
      $tnewKey $tKey->jsonSerialize(); 
        foreach ($tnewKey as $key => $value){
            echo "<td>";
            echo $value;
            echo "</td>";
        }

        echo "</tr>";                        
    
endforeach; 

hello, as stated above, I tried it and it works ... though one weird thing happen


model
PHP Code:
public function listTickets($quantity$offset)
 {

 
$ticketModel = new TicketModel();

 
$data $ticketModel->findAll($quantity$offset);
      
        
return $data;




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

 
$ticketModel = new TicketModel();

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

 
$data = [];

 
$data['tRows'] = $ticketData;

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

 } 

depite I browse myapp.local/home/list/25/0

they are printed ALL the 300 rows of records in the database

I mean, the resulting HTML table, doesn't have 25 rows, instead it has 300 rows

Wondering: how is this possible?.
Whilst if I use the original code at the begin of this thread, they only prints the expected 25 rows .. really confused about this behavior

How you define your routes? let see it ,
btw you can test directly data in controller dd(), to see if the result is correct before pass to view. do it step by step until found where the problem. also test query with external mysql client , suggest: HeidiSQL
Reply
#7

Oh, good hint, check available methods! Thank you

And yes, I'll go through a step after step testing

Thank you for your kind help
Reply




Theme © iAndrew 2016 - Forum software by © MyBB