开发者

Generating divs in php?

开发者 https://www.devze.com 2023-03-25 15:38 出处:网络
Say I have the following div: <div id=\"nav\"> <ul> <li class=\"navButton\"><a href=\"index.html\">Home</a></li>

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 hr开发者_Go百科ef="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


Just use the foreach or while like this:

foreach($array as $value){
?>
<div>
whatever <?php  other php here ?>
</div>
<?php
}


<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


<? 
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>';
}
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消