Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Looping through records in multi row and column format

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
redbrad0
Smarty Regular


Joined: 03 Feb 2007
Posts: 40

PostPosted: Tue Feb 26, 2019 5:30 am    Post subject: Looping through records in multi row and column format Reply with quote

I am not sure why but I just can't think off the top of my head on how I can accomplish something. I have a array of records and trying to loop through them (which isnt the issue) and display them in a table like https://jsfiddle.net/tkzyhdnj/ where I have color coded them showing that the yellow two boxes go together and the green two boxes go together. So going through a standard loop is all fine but because the output needs to be on two rows it would output the top left yellow first, then the top right green and then go back to the first record and do the bottom left yellow and then the bottom green record. Then if there isnt enough to finish the entire table it would just enter in blank cells.

So how would you go through and do this if you needed to output data when it doesnt come in the exact format you need it?

Code:
{foreach from=$arrRecords item=arrRecord}
   <!-- blah blah -->
{/foreach}
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1572

PostPosted: Tue Feb 26, 2019 9:21 am    Post subject: Reply with quote

You prepare array as you need it, before passing it to Smarty.
Back to top
View user's profile Send private message
redbrad0
Smarty Regular


Joined: 03 Feb 2007
Posts: 40

PostPosted: Tue Feb 26, 2019 12:49 pm    Post subject: Reply with quote

Isnt that the wrong way to think of doing it? Of course I can do it from PHP but typically you have a PHP developer that gives the data and then a designer that makes it work into PHP. I am actually pulling from the database so the code is much cleaner than this but there has to be a way in smarty to list out the items from a single array.

Code:
<?php
$arrAllData = array();

$arrSingleData = array();
$arrSingleData['ID'] = 1;
$arrSingleData['Logo'] = "https://www.smarty.net/forums/templates/smarty/images/phpbb2_logo.gif";
$arrSingleData['Name'] = "Product Name";
$arrSingleData['Link'] = "https://www.smarty.net";
array_push($arrAllData, $arrSingleData);

$arrSingleData = array();
$arrSingleData['ID'] = 2;
$arrSingleData['Logo'] = "https://www.smarty.net/forums/templates/smarty/images/phpbb2_logo.gif";
$arrSingleData['Name'] = "Product Name";
$arrSingleData['Link'] = "https://www.smarty.net";
array_push($arrAllData, $arrSingleData);

$arrSingleData = array();
$arrSingleData['ID'] = 3;
$arrSingleData['Logo'] = "https://www.smarty.net/forums/templates/smarty/images/phpbb2_logo.gif";
$arrSingleData['Name'] = "Product Name";
$arrSingleData['Link'] = "https://www.smarty.net";
array_push($arrAllData, $arrSingleData);

$gb->smarty->assign('arrRecords', $arrAllData);
   
$gb->smarty->display('db:product_listing.tpl');
?>
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1572

PostPosted: Wed Feb 27, 2019 10:09 am    Post subject: Reply with quote

Smarty have only rudimentary tools for data manipulation.
Yes, what you want could be done in Smarty, but it's way easier and much clearer to prepare data in the PHP code, leaving to Smarty only its intended rope of their presentation.
Back to top
View user's profile Send private message
redbrad0
Smarty Regular


Joined: 03 Feb 2007
Posts: 40

PostPosted: Wed Feb 27, 2019 2:53 pm    Post subject: Reply with quote

So each "record" I am looping through is an object so now if I am passing through 64 objects that I want listed I am now passing through 128 objects as I have to pass each object twice?
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1572

PostPosted: Wed Feb 27, 2019 6:51 pm    Post subject: Reply with quote

Can you please provide a test case?
If you are familiar with git, you can fork https://github.com/AnrDaemon/test-001 and create a forum-27748 branch to demonstrate your issue.
Please don't forget to commit composer.lock
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP