PHP中的最后一个值为空,但是Jquery从表中获取最后一行

时间:2017-01-30 09:48:23

标签: php jquery ajax

我正在尝试从html / php将变量传递给jquery for ajax。

这是html / php:

<?php
    $propertyid = $data['property_id'];
    $select5 = $con->prepare("SELECT favorite_properties_id, favorite_properties_property_id FROM tbl_favorite_properties where favorite_properties_user_id = '".$_SESSION['user_id']."' AND favorite_properties_property_id='$propertyid;'");
    $select5->setFetchMode(PDO::FETCH_ASSOC);
    $select5->execute();

    while($data5=$select5->fetch()){
        echo $data5['favorite_properties_id'];
        $favorite_properties_id = $data5['favorite_properties_id'];
    }
?>

<a href="#">
    <img class="addtofavoritebutton" pid="<?php echo $propertyid; ?>" fpid="<?php echo $favorite_properties_id;?>" src="../images/system/addtofavorite.png">
</a>
<?php echo $data['property_id']; ?>

这是jquery:

$('.addtofavoritebutton').click(function() {    
    var property_id = $(this).attr('pid');
    var favorite_properties_id = $(this).attr('fpid');

    alert(property_id);
    alert(favorite_properties_id);      
});

tbl_favorite_properties中有两行,但是jquery正在为fpid拾取最后一行,即使没有数据也是如此。当我在php中回显时,该值为空(应该是这样)但是在jquery中它是在最后一行并重复该值时,它无法找到。我怎样才能让jquery像php那样获取空值或空值?

3 个答案:

答案 0 :(得分:0)

PLease将你的标签放在循环中

<?php
$propertyid = $data['property_id'];
$select5 = $con->prepare("SELECT favorite_properties_id, favorite_properties_property_id FROM tbl_favorite_properties where favorite_properties_user_id = '".$_SESSION['user_id']."' AND favorite_properties_property_id='$propertyid;'");
$select5->setFetchMode(PDO::FETCH_ASSOC);
$select5->execute();

while($data5=$select5->fetch()) {
    echo $data5['favorite_properties_id'];
    $favorite_properties_id = $data5['favorite_properties_id'];
    echo '<a href="#"><img class="addtofavoritebutton" pid="'.$propertyid.'" fpid="'.$favorite_properties_id.'" src="../images/system/addtofavorite.png"></a>';
}
?>
<?php echo $data['property_id']; ?>

答案 1 :(得分:0)

实际上你在完成while循环后使用$favorite_properties_id,变量$favorite_properties_id将包含while循环的最后一次迭代的值。

<?php
    $propertyid = $data['property_id'];
    $select5 = $con->prepare("SELECT favorite_properties_id, favorite_properties_property_id FROM tbl_favorite_properties where favorite_properties_user_id = '".$_SESSION['user_id']."' AND favorite_properties_property_id='$propertyid;'");
    $select5->setFetchMode(PDO::FETCH_ASSOC);
    $select5->execute();

    $favorite_properties_id = null;
    while($data5=$select5->fetch()){
        echo $data5['favorite_properties_id'];
        $favorite_properties_id = $data5['favorite_properties_id'];
    }
    ?>

    <a href="#"><img class="addtofavoritebutton" pid="<?php echo $propertyid; ?>" fpid="<?php echo $favorite_properties_id;?>" src="../images/system/addtofavorite.png"></a>

    <?php echo $data['property_id']; ?>

答案 2 :(得分:0)

<?php
                $propertyid = $data['property_id'];
                $select5 = $con->prepare("SELECT favorite_properties_id, favorite_properties_property_id FROM tbl_favorite_properties where favorite_properties_user_id = '".$_SESSION['user_id']."' AND favorite_properties_property_id='$propertyid;'");
                $select5->setFetchMode(PDO::FETCH_ASSOC);
                $select5->execute();
                ?>

                <a href="#"><img class="addtofavoritebutton" pid="<?php echo $propertyid; ?>" fpid="<?php while($data5=$select5->fetch()){echo $data5['favorite_properties_id'];} ?>" src="../images/system/addtofavorite.png"></a>

现在正在运作。