tigerden
tigerden

Reputation: 728

dynamically generate div tags

I am modifying a website template to suit my needs, and to do that, I need to dynamically generate div tags, each of a different ID. I am using PHP to connect to my database.

<?php do { ?>
    //dynamically generated div tags
<?php } while ($row_all_courses = mysql_fetch_assoc($all_courses)); ?>

I followed Marc B's answer from here, but as there is much data between <div> and </div> tags, I can't do it in a single echo statement. Any ideas how to do it will be very helpful, thanks :)

I am posting my whole code, which is sort of temporary but can give some idea what I want to do :

<?php do { ?>
  <?php echo  "<div class='section section_with_padding'     id=".$row_courses['c_id']."'>"; ?>
    <h1><?php echo $row_courses['c_name']?></h1> 
    <div class="half left">
      <p><em><?php echo $row_courses['description']?></em></p>
    </div>
    <div class="half right">
      <div class="img_border img_nom">
      <a href="#gallery"><img src="images/templatemo_image_01.jpg" alt="image 1" /></a> 
    </div>
    <a href="#home" class="home_btn">home</a> 
    <?php echo "<a href='#".$row_courses['c_id']."' class='page_nav_btn     previous'>Previous</a>" ?>
    <a href="#gallery" class="page_nav_btn next">Next</a> 
  </div> <!-- END of Services -->
<?php } while ($row_courses = mysql_fetch_assoc($courses)); ?>   

Upvotes: 2

Views: 483

Answers (4)

Alireza Fallah
Alireza Fallah

Reputation: 4607

just echo id in php and leave everything else to be HTML .

so this is easier :

<?php do { ?>
    ...
    <div id="<?php echo "number".$id ?>">
     my content
    </div>
    ...
<?php } while ($row_courses = mysql_fetch_assoc($courses)); ?>

This way's advantage is that you can modify or debug Your HTML code very easier than echoing everything in php.

Upvotes: 1

DickieBoy
DickieBoy

Reputation: 4956

<?php $i = 1; ?>
<?php do { ?>
  <div id ="my_div_<?php echo $i; ?>">
    <?php //whatever you want in here ?>
  </div>
<?php $i++; ?>
<?php } while ($row_all_courses = mysql_fetch_assoc($all_courses)); ?>

Upvotes: 0

RafaSashi
RafaSashi

Reputation: 17205

while ($row_all_courses = mysql_fetch_assoc($all_courses)){

echo '<div id="'.$row_courses['c_id'].'">';

//do stuff....

echo '</div>';


}

Upvotes: 0

Realit&#228;tsverlust
Realit&#228;tsverlust

Reputation: 3953

If you cant echo it in one statement, echo it in 2?

echo "<div>";
//your stuff will be printed here
echo "</div>";

Upvotes: 1

Related Questions