2015-09-27 169 views
1

我尝试开发评论系统。 foreach行$ result包含来自名为posts的数据库的变量。对于评论,功能是弹出窗口。该帖子的ID是AUTO_INCREMENT,帖子的作者和评论被提交给一个文件,该文件将三个值插入名为comments的数据库中。第二个foreach循环$ comments,应该在用户单击弹出按钮时显示注释。这不起作用,因为ID <input type="hidden" name="id" id="id" value="<?PHP echo $row['id']; ?>" type="text" />不是唯一的,并且每个列表项的评论都变得不正确,但帖子具有唯一的ID。所有评论都显示出来。我怎样才能改变它在每个新创建的列表项唯一的ID?foreach循环不会生成唯一的列表项目ID

<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(); 
       } 

    } 
+0

我不知道理解你想做的事,但clickng的poppup按钮时,只显示一个评论,您应该使用:<?PHP的echo $ ** rowComment [“身份证”] ** ; ?>而不是<?php echo $ row ['id']; ?> –

+0

如果您包含来自showcomments.php的相关代码,您的代码将更易于理解。 –

+0

为什么不用'$ key'来生成唯一的id。如果你没有在'$ result'数组中使用特殊的'key',它将是从'0'开始的数字。 – KeepMove

回答

0

尝试下面的代码,

创建一个名为$i;

变量,它迭代,每次再加入到ID

<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 $i = 0; ?> 
      <?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<?php echo $i; ?>" 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 $i++; ?> 
      <?php endforeach; ?> 
     </ul> 
    </div> 
</div> 
<!-- div content --> 
</div> 
</div> 
+0

感谢您的关注,但这不会帮助:( – Bodoppels

+0

@Bodoppels你有这个活的版本? – jlocker

+0

没有对不起:(我希望编辑我的问题帮助。:) – Bodoppels