从动态生成的列表项中获取列表ID

时间:2015-06-20 19:20:08

标签: php html arrays jquery-mobile

我试图用数组中的值获取动态创建的列表元素的id,但它不起作用。如何获取特定列表项的ID?

  

解析错误:语法错误,意外'' (T_ENCAPSED_AND_WHITESPACE)

数组:

  

数组([0] =>数组([id] => 61 [0] => 61 [autorid] => 13 [1] => 13 [日期] => 2015-06 -11 [2] => 2015-06-11 [经度] => 13.6483 [3] => 13.6483 [纬度] => 53.8249 [4] => 53.8249 [title] => uiuo [5 ] => uiuo [text] => oujo [6] => oujo))

<?php
include("ownposts.php");
?>  
    <div data-role="content">
    <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 ($results as $key => $row): ?>



                    <li id ="listtwo" name="listtwo" data-role="list-divider" role="heading" align="right"  class="ui-li ui-li-divider ui-bar-b ui-li-has-count ui-first-child" ><?php echo $row['date']; ?>
                    <a href="#popupdelete" data-rel="popup" data-position-to="window" class="ui-btn ui-corner-all ui-shadow ui-btn-inline ui-icon-check ui-btn-icon-left ui-btn-a" data-transition="pop">delete</a>
<div data-role="popup" id="popupdelete" data-theme="a" class="ui-corner-all">
    <form data-ajax="false" name="delete-form" class="login-form" action="./delete.php" method="post" style="padding:10px 20px;">
        <div style="padding:10px 20px;">
            <h3>Are you shure you want to delete this post?</h3>

            <button type="submit" class="ui-btn ui-corner-all ui-shadow ui-btn-b ui-btn-icon-left ui-icon-check">yes</button>
        </div>
    </form>

</div>
</li> 
                    <li data-corners="false" data-shadow="false" data-iconshadow="true" data-wrapperels="div" data-icon="arrow-r" data-iconpos="right" data-theme="c" ><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"><?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>

                    </a></li>


        <?php endforeach; ?>            
                </ul>
            </div>               

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


    ownposts.php



 <?php

$userid = $_COOKIE["userid"];

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



                try {
                        $dbh = new PDO("mysql:host=$hostname;dbname=localy",$user,$password);

                        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
                         $sql = "SELECT id, autorid, date, longitude, latitude, title, text, time 
FROM post 
WHERE  (autorid = $userid
    )
ORDER BY id DESC"; // oder (longitude between $loo and $lo or latitude  between $laa and $la) versuchen
  if ($res = $dbh->query($sql)) {// need to add this line in your code
      // then after fetchColumn
     $results = $res->fetchAll();


   }


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


    ?>          
    <?php
    if(isset($_POST["deletes"]))
    {

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



                try {
                        $dbh = new PDO("mysql:host=$hostname;dbname=loc",$user,$password);

                        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
                         $sql = "delete from posts 
where id = ".$row['id']."
limit 1"; // oder (longitude between $loo and $lo or latitude  between $laa and $la) versuchen



   }



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



    }
    ?>

2 个答案:

答案 0 :(得分:0)

以上代码示例是一个还是两个文件?如果以上是一个文件,则缺少开头

答案 1 :(得分:0)

替换您的try块,如下所示:

try {
                        $dbh = new PDO("mysql:host=$hostname;dbname=loc",$user,$password);

                        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
                         $sql = "delete from posts where id = ".$row['id']."  limit 1"; // oder (longitude between $loo and $lo or latitude  between $laa and $la) versuchen
   }