-1

I am creating a dynamic page that is echoing a lot of divs with a class and a unique id pulled from a database so they can be manipulated easily. I am unsure if I am doing it the best way or not it seems to be sloppy and hard to understand to me. I am hoping someone with more experience can help me figure out if there is a better way to accomplish the same thing. For example:

if ($comment['reply'] == 1) {
    echo '<div class="comment">';
    echo $comment['comment'];
    echo "</div>";
    echo '<BR /> <div class="replys" id="'.$comment['id'].'">';
    echo '</div>';
    echo '<div class="replyTo" id="'.$comment['id'].'reply">';
while ($replys = $stmt1->fetch(PDO::FETCH_ASSOC)) {
    if ($replys['order_by'] % 2 == 0){
        echo '<div class="replyToEven">';
        echo $replys['reply'];
        echo "</div>";
} else {
        echo '<div class="replyToOdd">';
        echo $replys['reply'];
        echo "</div>";
   }

}

The reason I have it echoing a div is because I want all the contents to be in separate divs, instead of one to separate the data from the MySQL query. Hopefully I have provided enough information just let me know if you need anymore.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Yamaha32088
  • 4,125
  • 9
  • 46
  • 97
  • 2
    You should consider using a template system like Smarty: http://www.smarty.net/ – Joren Oct 05 '13 at 20:03
  • I agree with @Joren to use a templating system. Although I'm not personally a fan of Smarty. – Boundless Oct 05 '13 at 20:04
  • Isn't it smarter use one echo to the whole text instead few echos? – Yotam Oct 05 '13 at 20:05
  • 1
    http://stackoverflow.com/questions/1100354/how-can-i-echo-html-in-php – Dinesh Oct 05 '13 at 20:10
  • @Yotam It would be smarter to write HTML and simply output the variables needed for readability reasons. +1 for the templating system, be it [Smarty](http://smarty.net), [Twig](http://twig.sensiolabs.org/) or plain PHP… – nietonfir Oct 05 '13 at 20:10

3 Answers3

1

You can condense the code down quite a lot if that's what you mean:

if($comment['reply'] == 1){
    echo '<div class="comment">' . $comment['comment'] . '</div> <br />' . 
              '<div class="replys" id="' . $comment['id'] . '">' . '</div>' . 
                 '<div class="replyTo" id="'.$comment['id'].'reply">';

    while($replys = $stmt1->fetch(PDO::FETCH_ASSOC)){
        if($replys['order_by'] % 2 == 0){
            echo '<div class="replyToEven">' . $replys['reply'] . "</div>";
        }else{
            echo '<div class="replyToOdd">' . $replys['reply'] . "</div>";
        }
    }
}
Scott Helme
  • 4,786
  • 2
  • 23
  • 35
1

One cool thing about PHP is that you can embed it into HTML. So what you can do is use an html layout and use inline php statements to process your values. I will show you an example with the php you posted.

<?php if(comment['reply'] == 1 { ?>
     <div class="comment">
        <?php echo $comment['comment']; ?>
     </div>
     <br/>
     <div class="replys" id="<?php echo comment['id']; ?>" >
     <div class="replyTo" id="<?php echo $comment['id']; ?>reply">
     <?php while ($replys = $stmt1->fetch(PDO::FETCH_ASSOC)) { ?>
       <?php if ($replys['order_by'] % 2 == 0){ ?>
          <div class="replyToEven">
             <? php echo $replys['reply']; ?>
          </div>
        <?php } else { ?>
          <div class="replyToOdd">';
             <?php echo $replys['reply']; ?>
          </div>
<?php
    }
}

?>

Then you can take this snippet and place it in a function called show_comments(); and in your main template where ever you want to show comments in this format you can call the function show_comments(); rather than typing this code over again.

Richard Christensen
  • 2,046
  • 17
  • 28
0

You should not have a column in your DB just for assigning alternate rows style to your DB. Instead you can introduce a variable and keep a check on it.

if ($comment['reply'] == 1) {
    echo '<div class="comment">';
    echo $comment['comment'];
    echo "</div>";
    echo '<BR /> <div class="replys" id="'.$comment['id'].'">';
    echo '</div>';
    echo '<div class="replyTo" id="'.$comment['id'].'reply">';
    $i = 0;
while ($replys = $stmt1->fetch(PDO::FETCH_ASSOC)) {
    if ($i % 2 == 0)
        $class = "replyToEven";
    else
        $class = "replyToOdd";

        echo '<div class="$class">';
        echo $replys['reply'];
        echo "</div>";
        $i++;
}

You can also write the tags as:

<?php
if ($comment['reply'] == 1) {
?>
<div class="comment"><?=$comment['comment']?></div><BR />
<div class="replys" id="<?=$comment['id']?>"></div>
<div class="replyTo" id="<?=$comment['id']?>reply">';
<?php
}
?>

This would make your code a lot more readable.

neeagl
  • 348
  • 1
  • 13
  • notice: the shortcut `=$var?>` isn't allowed by default < PHP 5.4.0. more: http://stackoverflow.com/questions/18989102/can-var-syntax-cause-problems/18989171#18989171 – Mr. B. Oct 05 '13 at 20:21