1

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 }

Jack King
  • 251
  • 1
  • 4
  • 14

2 Answers2

3

+ 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
}
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
1

Another solution is:

<?php for ($x = 0; $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 endfor; ?>

or

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

or

for ($x=1; $x <= 100; $x++) {
   echo '<ons-list-item id="p' . $x . '" onclick="fn.load(\'s' . $x . '.html\');ofsc();" tappable></ons-list-item>'; 
}
Vural
  • 8,666
  • 11
  • 40
  • 57