PDA

View Full Version : Foreach in plugin


Dr.CustUmz
05-02-2020, 04:38 PM
So I am trying to use a foreach in my plugin and call it in my template with a variable, since my foreach is open it's breaking my product. How can I go about calling a foreach and include it in my template?


My before product, raw php:
<?php
$i=0;
foreach($result as $row) if ($i < 10+1) {
?>
<div id="something">
<div class="really"><b>Cool:</b> <?php echo number_format($row['repeat']);?></div>
</div>
<?php
$i +=1;
}
?>




so I have my plugin like this (and yes the other variables are defined, this is just where the is)

$i=0;
$drc_4ch-st = foreach($result as $row) if ($i < 10+1) {;
$i +=1;
$drc_4ch-ed = };

$drc_rep = number_format($row['repeat']);


and the template like this
$drc_4ch-st
<div id="something">
<div class="really"><b>Cool:</b> $drc_rep</div>
</div>
$drcc_4ch-ed

and it breaks, the variable$drc_repworks on its own, but nothing works when i add the portion above it.

Seven Skins
05-02-2020, 05:06 PM
I am no expert but …
1 - there is an extra semicolon at line 2 of your php code
2 - PHP variables, I think underscore should be used not dashes

Dave
05-02-2020, 06:33 PM
Also PHP doesn't have any type of macro support like that (unlike C++ macro definitions using #define). You can't assign portions of a function to a variable and then execute the function as usual by calling the PHP variables in order.

PinkMilk
05-02-2020, 09:01 PM
Template:
<div id="something">
<div class="really"><b>Cool:</b>$drc_rep</div>
</div>

Plugin:
hook : parse_templates
$i=0;

foreach($result as $row) if ($i < 10+1) {

$drc_rep = number_format($row['repeat']);

eval('$mytemplate = "' . fetch_template('mytemplate') . '";');

$i +=1;

}

Note: This is just a randon guess!

Dr.CustUmz
05-02-2020, 09:32 PM
Template:
<div id="something">
<div class="really"><b>Cool:</b>$drc_rep</div>
</div>

Plugin:
hook : parse_templates
$i=0;

foreach($result as $row) if ($i < 10+1) {

$drc_rep = number_format($row['repeat']);

eval('$mytemplate = "' . fetch_template('mytemplate') . '";');

$i +=1;

}

Note: This is just a randon guess!

thanks pink, i did something similar, been reading through alot of articles on here to find anything similar. I ended up doing it all in the plugin since im calling multiple varibles throughout the html code as well.

so my end results ended up being along these lines:
$i=0;
foreach($result as $row) if ($i < 10+1)
{
$cantTellYa .= '<div class="creep-country">
<div class="creep-col creep-countrycol">
<span class="creep-col-country">'.$row['country'].'</span>
</div>
</div>
' . $i +=1;
}

then just used the $cantTellYa variable in the template, this seems to work well, there are multiple spans and variables in the code, i just shortened everything to display it here.