/    Sign up×
Community /Pin to ProfileBookmark

Watchedneed 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: https://github.com/isRuslan/php-template

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.

(guess you can only post code once in a post, lets quote it)

>

$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. πŸ™‚

to post a comment
PHP

5 Comments(s) ↴

Copy linkTweet thisAlerts:
@NogDogMay 29.2020 β€”Β Use this forum's ... tags for multiple lines of code. ;)
Copy linkTweet thisAlerts:
@NogDogMay 29.2020 β€”Β Since I know nothing about that software, this is just a guess, but I think you'd have to build those individual arrays where each value is a string, not an array. Maybe something like this:
<i>
</i>$names = array('test1', 'test2', 'test3');
$names2 = array('test2 1', 'test2 2', 'test2 3');

$template_array = array();
foreach($names as $name) {
$template_array[] = array('name' =&gt; $name);
}
foreach($names2 as $name2) {
$template_array[] = array('name2' =&gt; $name2);
}
$template-&gt;assign('people', $template_array);

But that's speculation on my part.
Copy linkTweet thisAlerts:
@whofarted75authorMay 29.2020 β€”Β @NogDog#1618904

Thats really messed up because the author of the class gives the example I showed but the one you gave works...... 🤣

A Big Thanks man! This has been driving me up a wall. 😅
Copy linkTweet thisAlerts:
@whofarted75authorMay 29.2020 β€”Β hmm.... spoke to soon. I can only get one array at a time to work. I was trying to get like story title, storycontent, story author. & loop it through together.

I think I'm gonna give up. I just don't understand OPP & how to use it. I've been frustrated for days over just this. I think it's time to just forget about it. Thanks anyway man.
Copy linkTweet thisAlerts:
@whofarted75authorMay 29.2020 β€”Β somehow I got this to work finally. I'll stop spamming the board but I didn't want anyone to come to this and spend any time on it.

This little hackup test 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]);

> $i++;

> }
>
>
>
> $template->assign('people', $template_array);
Γ—

Success!

Help @whofarted75 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 β€” BETA 4.23,
whats_new: community page,
up_next: more Davinciβ€’003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinciβ€’003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...