0

Say I have the following div:

 <div id="nav">
     <ul>
      <li class="navButton"><a href="index.html">Home</a></li>
      <li class="navButton"><a href="http://www.freewebsitetemplates.com">Guitar 
          Bodies</a></li>
      <li class="navButton"><a href="http://www.freewebsitetemplates.com">Guitar 
          Pickguards</a></li>
      <li class="navButton"><a href="http://www.freewebsitetemplates.com">Contact Us</a></li>
    </ul>
  </div>

I would want this to be repeated a certain number of times so the resulting html would look like:

 <div id="nav">
     <ul>
      <li class="navButton"><a href="index.html">Home</a></li>
      <li class="navButton"><a href="http://www.freewebsitetemplates.com">Guitar 
          Bodies</a></li>
      <li class="navButton"><a href="http://www.freewebsitetemplates.com">Guitar 
          Pickguards</a></li>
      <li class="navButton"><a href="http://www.freewebsitetemplates.com">Contact Us</a></li>
    </ul>
  </div> <div id="nav">
     <ul>
      <li class="navButton"><a href="index.html">Home</a></li>
      <li class="navButton"><a href="http://www.freewebsitetemplates.com">Guitar 
          Bodies</a></li>
      <li class="navButton"><a href="http://www.freewebsitetemplates.com">Guitar 
          Pickguards</a></li>
      <li class="navButton"><a href="http://www.freewebsitetemplates.com">Contact Us</a></li>
    </ul>
  </div> <div id="nav">
     <ul>
      <li class="navButton"><a href="index.html">Home</a></li>
      <li class="navButton"><a href="http://www.freewebsitetemplates.com">Guitar 
          Bodies</a></li>
      <li class="navButton"><a href="http://www.freewebsitetemplates.com">Guitar 
          Pickguards</a></li>
      <li class="navButton"><a href="http://www.freewebsitetemplates.com">Contact Us</a></li>
    </ul>
  </div>

If the number was 3. What php command is used to do this sort of thing? (I know I use a for loop, but I mean the code in between)

Essentially what I will eventually have is something like this:

for each product
div product
text from product file
close div product

Thanks

2
  • Do you have a better example of what the result should look like? I presume you don't actually want 3 identical divs, but 3 divs with the same structure but different content? (Please explain why if you really do. There might be other ways to solve your problem. And you should be using a class instead of an id, at the least then.) Commented Aug 1, 2011 at 17:16
  • Yes, same structure different content. Commented Aug 1, 2011 at 17:17

3 Answers 3

3

Just use the foreach or while like this:

foreach($array as $value){
?>
<div>
whatever <?php  other php here ?>
</div>
<?php
}
Sign up to request clarification or add additional context in comments.

Comments

1
<div class='nav'>
<ul>
<?php foreach ($products as $product): ?>
<li class='navbutton'><?php echo $product->text; ?>
</li>
<?php endforeach; ?>
</ul>
</div>

if you have multiple different divs, make a foreach loop for that too, if you want same div to repeat 3 times, wrap it around a for loop

1 Comment

you can use <?=$product->text?> instead of <?php echo $product->text; ?> but it will be deprecated in the next versions of php.
0
<? 
for ($results as $value) {

echo '<div id="nav">';
echo '     <ul>';
echo '      <li class="navButton"><a href="index.html">Home</a></li>';
echo '      <li class="navButton"><a href="http://www.freewebsitetemplates.com">'.$value['name1'].'</a></li>';
echo '      <li class="navButton"><a href="http://www.freewebsitetemplates.com">'.$value['name2'].'</a></li>';
echo '      <li class="navButton"><a href="http://www.freewebsitetemplates.com">Contact Us</a></li>';
echo '    </ul>';
echo '  </div>';
}
?>

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.