Jack King Jack King - 2 months ago 13
PHP Question

echo html code with php loop

in my project i have many id and every id have number like p1,p2,p3 ...


</ons-list-item>
</list-item>


so i create php for generate code like

<?php
for ($x = 0; $x <= 100; $x++) {
echo " <ons-list-item id="p+$x" onclick="fn.load('s+$x.html');ofsc();" tappable>

</ons-list-item>";
}
?>


for rsult:

<ons-list-item id="p1" onclick="fn.load('s1.html');ofsc();" tappable>

</ons-list-item>
<ons-list-item id="p2" onclick="fn.load('s2.html');ofsc();" tappable>

</ons-list-item>


but dont working and display error {Parse error: syntax error, unexpected 'p' (T_STRING), expecting ',' or ';' in
}

Answer

+ is not a concatenation operator, . is. Also, your for loop should be like this:

for ($x = 1; $x <= 100; $x++) {
    echo "<list-item id='p" . $x . "'></list-item>"; 
}

Update(1):

for ($x = 1; $x <= 100; $x++) {
    ?>
    <ons-list-item id='p<?php echo $x; ?>' onclick="fn.load('s<?php echo $x; ?>.html');ofsc();" tappable>
    </ons-list-item>
    <?php
}