我正在制作一个带有向上和向下按钮的图库,数组$ imageids包含所有图像ID。我想用它来访问这些值 JavaScript如下,
PHP:
require"./connect.php";
$imgquery = mysql_query("SELECT * FROM press INNER JOIN subscriptions ON press.userid=subscriptions.to_u WHERE subscriptions.from_u='30' ORDER BY press.id DESC ");
$countrow = mysql_num_rows($imgquery);
$imageids = array();
while($fimgq = mysql_fetch_assoc($imgq)) {
$imgid = $fimgq['id'];
array_push($imageids,$imgid);
}
JavaScript的:
$(document).ready(function() {
var i = -1;
var cr = <?php echo $countrow ?>;
$('#down').click(function() {
if (i < cr-1) {
i = i + 1;
var ard = <?php echo $imageids[i]?>;
alert(ard);
}
});
$('#up').click(function() {
if(i>0) {
i = i - 1;
var aru = <?php echo $imageids[i]?>;
alert(aru);
}
});
});
我希望通过在$imageids
数组中放置一个javascript变量来获取$imageids[]
元素,例如
var ard = <?php echo $imageids[i]?>;
alert(ard); // Which doesn't work
答案 0 :(得分:0)
正如@ chris85和@Khalos在评论中指出的那样,一旦JavaScript运行,PHP变量都已经死了,所以你需要将所有ID:s输出到一个JavaScript数组中。它会像这样:
MainActivity.this