dongzhitao4839 2015-09-27 20:50
浏览 194

foreach循环插入列表项的$ id

I try to develop a simple comment system. The foreach row $result contains variables from a database named posts.

The id of the post(every post is a list item) that is AUTO_INCREMENT and every post have a unique $id, the author of the post and the comment become submitted to a file that insert the three values into a database named comments.

The second foreach loop, $comments, should show the comments when the user click the popup button. That does not works because the id <input type="hidden" name="id" id="id" value="<?PHP echo $row['id']; ?>" type="text" /> wont become right inserted(comments.php should do that).

The result is that all comments become showed in every list item and not the only these comments who are e.g. at list item 2. How can I insert the $id of the list items into the database?

<div data-role="content">
    <?php include( "list.php"); ?>
    <div data-demo-html="true">
        <ul data-role="listview" data-inset="true" class="ui-listview ui-listview-inset ui-corner-all ui-shadow">

            <?php foreach ($result as $key=> $row): ?>

            <li id="listone" name="listone" data-role="list-divider" role="heading" class="ui-li ui-li-divider ui-bar-b ui-li-has-count ui-first-child">
                <?php echo $row[ 'date']; ?>
            </li>
            <li data-corners="false" data-shadow="false" data-iconshadow="true" data-wrapperels="div" data-icon="arrow-r" data-iconpos="right" data-theme="a">
                <div class="ui-btn-inner ui-li">
                    <div class="ui-btn-text">
                        <a class="ui-link-inherit">
                            <p class="ui-li-aside ui-li-desc"><strong></strong>
                                <?php echo $row[ 'time']; ?>
                            </p>
                            <p class="ui-li-desc"><strong><?php echo $row['title']; ?></strong>
                            </p>
                            <p class="ui-li-desc">
                                <?php echo $row[ 'text']; ?>
                            </p>

                            <p class="ui-li-desc"><strong><?php echo $row['town']; ?></strong>
                            </p>
                            <a href="#popupcomment" data-rel="popup" data-position-to="window" data-transition="pop">comment</a>
                            <div data-role="popup" id="popupcomment" data-theme="a" class="ui-corner-all">
                                <form data-ajax="false" name="login-form" class="login-form" action="./comments.php" method="post" style="padding:20px 40px;">
                                    <div class="content">
                                        <?php include( "showcomments.php"); ?>
                                        <?php foreach ($comments as $keyComment=> $rowComment): ?>
                                        <p class="ui-li-desc"><strong><?php echo $rowComment['username']; ?></strong>
                                        </p>
                                        <p class="ui-li-desc">
                                            <?php echo $rowComment[ 'comment']; ?>
                                        </p>
                                        <?php endforeach; ?>
                                        <input type="hidden" name="id" id="id" value="<?php echo $row['id']; ?>" type="text" />
                                        <input type="hidden" name="autorpost" id="autorpost" value="<?php echo $row['autor']; ?>" type="text" />
                                        <!-- autor des posts -->
                                        <textarea rows="1" name="text" id="text" class="foo"></textarea>
                                    </div>
                                    <div class="footer">
                                        <input type="submit" name="save" value="comment" class="button" data-theme="a" />
                                    </div>
                                </form>
                            </div>

                        </a>
            </li>

            <?php endforeach; ?>

        </ul>
        </div>
        </div>
        <!-- div content -->
    </div>
</div>

showcomments.php:

$hostname='localhost';
$user='root';
$password='';

$id = $row['id'];
try {
     $dbh = new PDO("mysql:host=$hostname;dbname=searchfood",$user,$password);

     $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
     $sql = "SELECT id, username, comment, time 
             FROM comments 
             WHERE  id_post = $id 
             ORDER BY id DESC"; 
     // oder (longitude between $loo and $lo or latitude  between $laa and $la) versuchen
     if ($com = $dbh->query($sql)) {// need to add this line in your code
      // then after fetchColumn
     $comments = $com->fetchAll();       
     }                        
}
catch(PDOException $e) {
    echo $e->getMessage();
}      

comments.php:

$hostname='localhost';
$user='root';
$password='';

if(isset($_POST["id"])){                    
    try {
        $dbh = new PDO("mysql:host=localhost;dbname=searchfood", $user, $password);

        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
        // prepare your query
        $query = 'INSERT INTO comments 
                    (username, autorpost, comment, id_post, time) 
                  VALUES (?, ?, ?, ?, now())';

        $stmt = $dbh->prepare($query);
        // bind variables
        $stmt->execute(array($_COOKIE['username'], $_POST['autorpost'], $_POST['text'], $_POST['id']));
        // pull last insert id
        $new = $dbh->lastInsertId(); 

        // show success message or redirect, whatever you want
        echo "New Record Inserted Successfully";
        $message['success'] = 'Neuer Benutzer (' . htmlspecialchars($_POST['username']) . ') wurde angelegt, <a href="login.php">weiter zur Anmeldung</a>.';
        header("Location: http://".$_SERVER['HTTP_HOST']."/lendsth/main.php", true, 302);

    }
    catch(PDOException $e) {
        echo $e->getMessage();
    }

}
  • 写回答

1条回答 默认 最新

  • donglun7151 2015-09-27 23:48
    关注

    Basically, you need to make a column in your database that stores which comment is posted under which "id_post" -- Ideally, you'd create a table for the posts and a table for the comments. That, or if you aren't storing the posts in a db, each post is going to need to have a unique identifier of some sort that you store with each comment.

    Your table would be set up as such: First Table -> Stores the "posts" that will have comments

    Table - Posts: post_id, post_content

    Table - Comments: comment_id, comment_content, post_id (This is the ID of the post that the user posted under -- You could store this in the hidden post_id input, and pass it along using POST to the form processing.

    Your select would look like this

    "SELECT * FROM comments WHERE post_id = '{your post id}' 
    

    I don't know if this will help but might lead you down the right path.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。