Need help with php-templates array loops

Can anybody help me out with this?

I’m trying to loop through arrays to put into a template and show multiple posts on a page using the template. But I cannot get this to work and I don’t know why. I’m very new to OOP and don’t fully understand it. This seems generic and it should be easy to do but I keep having problems.

I can get it to work if I type the arrays in manually, but that doesn’t help me if I pull data from a database and loop through it.

here’s what i’m talking about: from this class:

I can’t get this part to work in the template

$template->assign(‘items’, array(
array(‘name’ => ‘First’),
array(‘name’ => ‘Second’)

I’m trying to use this to display arrays pulled from the database with now luck.

$names = array(‘test1’, ‘test2’, ‘test3’);
$names2 = array(‘test2 1’, ‘test2 2’, ‘test2 3’);

$template->assign(‘people’, array(
array(‘name’ => $names),
array(‘name2’ => $names2)

any help…please.

Do you have any control over which templating engine to use? If so I’d switch to Twig as that’s a lot more powerful.

With regards to your current issue, what is the exact problem you’re having? What isn’t working? What did you expect to happen and what happened instead?

Also it would be useful if you could post the full code, not just snippets.

All im trying to do is get the looping feature to work.

I’m trying to get “news” I guess you’d call it, that i post to come up on the ppage. so I pull the info from the db & load it into an array, then display through the template.

the template appears to only use hard coded info for loops… which seems totally useless.

to me, this:

$template->assign('items', array(
      array('name' => 'First'),
      array('name' => 'Second')

SHOULD cause the template to LOOP through each story with the title, post, time, etc. and present it on the page. (just like each post in this forum… 1 block after the other.)

I can use any template system but I can’t seem to grasp OOP, the only things I need a template to to are
1.) Be relatively easy to use
2.) Be easy to implement (I started using this one because it was simple to use, just include the class and go.)
3.) be able to Replace single variables, do block loop, and maybe handle if statements. (like if logged in show this - else show that.)

Honestly I’ve been away from php for over 5 yrs now & i’m really just thinking I can’t do this stuff anymore. I’m really about to give up. So i’m not really looking to go much further with this. It was just for fun & I don’t seem to be getting anywhere.

Anyway, thanks for the response. :+1:

someone elses help led to me figuring it out, I hacked this up to test & it worked

$names = array(‘Bob’, ‘Ralph’, ‘Mike’);
$names2 = array(‘Col.’, ‘Gen.’, ‘Adm.’);
$i = 0;

$template_array = array();

while($i < 3)
$template_array = array(‘NAME’ => $names[$i], ‘RANK’ => $names2[$i]);

$template->assign(‘people’, $template_array);

Just thought I’d let anyone here know before they spend any time on it. :wave: