0

I know jQuery append element, but I want to know is there any way so that I can append 50 similar elements using php with some loop so something else, The elements I want to append 50 times are given here,

<div class='test'>
  <span class='<?php echo $result[0]->cc1;?>'><?php echo $result[0]->dd1;?></span>
</div>
  
<div class='test'>
  <span class='<?php echo $result[0]->cc2;?>'><?php echo $result[0]->dd2;?></span>
</div>
  
<div class='test'>
  <span class='<?php echo $result[0]->cc3;?>'><?php echo $result[0]->dd3;?></span>
</div>
  
<div class='test'>
  <span class='<?php echo $result[0]->cc4;?>'><?php echo $result[0]->dd4;?></span>
</div>
  
//  and so on.. up to 50.

Tried this but not working,

<?php for ($i=0; $i <50; $i++) : ?> <span class='<?php echo $result[0]->cc'+i+';?>'><?php echo $result[0]->dd'+i+';?></span> <?php endfor; ?>

8
  • <?php for ($i=0; $i <50; $i++) : ?> your html <?php endfor; ?> Commented Jul 29, 2021 at 12:54
  • Sir, I have tried this but it is not working, <?php for ($i=0; $i <50; $i++) : ?> <span class='<?php echo $result[0]->cc'+i+';?>'><?php echo $result[0]->dd'+i+';?></span> <?php endfor; ?> Commented Jul 29, 2021 at 13:17
  • How is it Not Working. Please be specific Commented Jul 29, 2021 at 13:18
  • I am using it in wordpress template.php file. I have pasted exact code but it is not working Commented Jul 29, 2021 at 13:19
  • php concatenator is a . not a + Commented Jul 29, 2021 at 13:32

1 Answer 1

1

Create a loop

Then using the $result[0]->{'cc'.$i} syntax address all your properties

for( $i=1; $i<51; $i++) :
?>
    <div class='test'>
        <span class='<?php echo $result[0]->{'cc'.$i};?>'><?php echo $result[0]->{'dd'.$i};?></span>
    </div>
<?php
endfor;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.