【发布时间】:2018-01-14 08:43:48
【问题描述】:
我有一个奇怪的问题。我使用 PHP MySQL 使用 while 循环获取表值并将它们存储在数组中。然后我在一个页面中一个接一个地显示它。
$sql = "select * from newsfeed order by id DESC";
$result = $db->query($sql);
$posts_array = array(); // Storing the result to an Custom Array
$index = 0;
if($result->num_rows > 0)
{
while($row = $result->fetch_assoc()) {
$posts_array[$index] = $row;
$index++;
}
}
我已使用以下代码成功地将帖子的 $post_id 设置到 HTML 元素上。
<?php
$array_size = sizeof($posts_array);
for($i = 0; $i < $array_size; $i++){
?>
<div class='wrapper' id='wrapper'>
<div class='user'>
<!-- Some Code -->
<?php
$post_id = $posts_array[$i][id];
?>
<input type='image' name='like' id='like' width='40' height='40' src='like_btn.png' value='<?php echo ($post_id);'?> />
<?php } ?>
现在我想使用 Jquery 获取该值并做一些数据库工作。我已经使用以下代码访问了该值:
<script type="text/javascript">
function justsomething() {
alert($(this).val());
}
</script>
问题是我只为每个帖子获取最后一个帖子的 id 作为 alert() ,而不是获取不同的 id 。 请帮我。我很困惑每次单击并调用 justsomething() 函数时是否正在加载 PHP 脚本。
注意:我可以单独显示每个帖子的 $post_id 没有任何问题。
【问题讨论】:
-
请显示while循环。
-
-
您是在循环内创建函数吗?提供minimal reproducible example
-
评论里的代码是怎么关联的?请显示上面提到的 while 循环,它会为
$post_id创建值。不过,看起来您会有多个具有相同值的ids。你不应该。 -
不要将代码块放入 cmets。使用格式化代码编辑问题
标签: javascript php jquery html mysql