0

I'm trying to loop this code, so that when the while statement on the php code start the database output will have an individual <li> per output

    <li class="bar<?php echo $id; ?>">
    <div align="left" class="post_box">
    <span style="padding:10px"><?php echo $msg; ?> </span>
    <span class="delete_button"><a href="#" id="<?php echo $id; ?>" class="delete_update">X</a></span>
    <span class='feed_link'><a href="#" class="comment" id="<?php echo $id; ?>">comment</a></span>
    </div>
    <div id='expand_box'>
    <div id='expand_url'></div>
    </div>
    <div id="fullbox" class="fullbox<?php echo $id; ?>">
    <div id="commentload<?php echo $id; ?>" >
    </li>

this is my php code

    </div>
    <div class="comment_box" id="c<?php echo $id; ?>">
    <form method="post" action="" name="<?php echo $id; ?>">
    <textarea class="text_area" name="comment_value" id="textarea<?php echo $id; ?>">
    </textarea><br />
    <input type="submit" value=" Comment " class="comment_submit" id="<?php echo $id; ?>"/>
    </form>
    </div>
    </div>

<?php
if($_POST['submit'] == "submit"){
   $username="xxx_admin";
   $password="xxx";
   $database="xxx_database";

//connect to mysql server
                $mysqli = new mysqli("localhost", $username, $password, $database);

                                        //check if any connection error was encountered
                                        if(mysqli_connect_errno()) {
                                            printf("Connect failed: %s\n", mysqli_connect_error());
                                            exit;
                                        }   
   $client_id = $_POST['client_id'];
   echo $client_id;
  $query="select * from messages where client_id='$client_id'";
  $result = $mysqli->query( $query );
  $row = $result->fetch_assoc();
                                        while ($row = $result->fetch_object())
                                        {
                                                // set up a row for each record
                                                echo "<li>";
                                                $mes = $row->message;
                                                $mes = nl2br($mes);

                                                $msg ="{$mes} <br> . {$row->date_post;}";

?>
<li class="bar<?php echo $id; ?>">
<div align="left" class="post_box">
<span style="padding:10px"><?php echo $msg; ?> </span>
<span class="delete_button"><a href="#" id="<?php echo $id; ?>" class="delete_update">X</a></span>
<span class='feed_link'><a href="#" class="comment" id="<?php echo $id; ?>">comment</a></span>
</div>
<div id='expand_box'>
<div id='expand_url'></div>
</div>
<div id="fullbox" class="fullbox<?php echo $id; ?>">
<div id="commentload<?php echo $id; ?>" >

</div>
<div class="comment_box" id="c<?php echo $id; ?>">
<form method="post" action="" name="<?php echo $id; ?>">
<textarea class="text_area" name="comment_value" id="textarea<?php echo $id; ?>">
</textarea><br />
<input type="submit" value=" Comment " class="comment_submit" id="<?php echo $id; ?>"/>
</form>
</div>
</div>
</li>
<?php                                   }

     $mysqli->close();
}
?>

I'm trying to loop the html code because it will be put inside an <ol></ol>

all rows in the database should have an individual <li></li>

1
  • It looks to me like your code should work, what's the problem? However, you're generating invalid HTML, since each each row has the same IDs. Commented Nov 1, 2012 at 11:58

1 Answer 1

1

In the official PHP site exists same examples of loops.

<?php
$arr = array("one", "two", "three");
reset($arr);
while (list(, $value) = each($arr)) {
    echo "Value: $value<br />\n";
}

foreach ($arr as $value) {
    echo "Value: $value<br />\n";
}
?> 

you should, instead of firstly render the html, load the messages into a array, and after this, iterate this array doing the html renderization

good luck!

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.