mysql - SESSION variable in while loop not being sent correctly to second .php file -
i'm trying create simple topic , posting system , have 2 tables in mysql database; topics table , posts table. in posts table have post_parentid field links topic_id field in topics table.
here how start query selecting (and displaying) topics in topic table. save topic_id field variable:
<?php $sql = "select * topics inner join members on topics.topic_by = members.id"; $topics = $mysqli->query($sql); if ($topics->num_rows > 0) { // output data of each topic while($row = $topics->fetch_assoc()) { //saving topicid variable $topic_id = $row["topic_id"]; $_session['topicid'] = $topic_id;?>
right after display html each topic, start new while loop display each post within each topic:
<?php $sql = "select * posts inner join members on posts.post_by = members.id post_parentid = $topic_id"; $posts = $mysqli->query($sql); if ($posts->num_rows > 0) { // output data of each post while($row = $posts->fetch_assoc()) { ?>
right after this, display html each post , display form-control enter post, directed send_post.php file:
<div class="chat-message-form"> <div class="form-group"> <form action="includes/send_post.php" method="post"> <textarea class="form-control message-input" name="post_content" placeholder="enter message text"></textarea> <input type="submit"> </form> </div> </div>
when call $topic_id in send_post.php file this:
$topic_id = $_session['topicid'];
it returns last topic_id in topics table instead of current topic_id in while loop. logic right or should doing different?
by doing in loop:
$_session['topicid'] = $topic_id;
you're overwriting value of topicid everytime loop. don't know how want topicid possible way avoid be:
$_session['topicid'] = array(); while($row = $topics->fetch_assoc()) { $_session['topicid'][] = $topic_id; ...
Comments
Post a Comment