0

我希望能够显示专辑中包含的所有图像中的图像数量(每个艺术家一张专辑)。示例:专辑 2,共 10 张。然后转到下一张图片,我们将获得专辑 3,共 10 张,依此类推。我可以通过使用函数get_count然后在我的 HTML 页面上调用该函数和一个foreach语句来获取总数。但是,我不知道如何编写代码来获取个人编号。使用的代码:-

<?php
function get_count($artist_id) {
  $artist_id = (int)$artist_id;
  $count = array();
  $count_query = mysql_query("
      SELECT `image_album_id`, `artist_id`, COUNT(`image_album_id`) as `image_count` 
      FROM `album_images`
      WHERE `artist_id`=$artist_id AND `member_id`=".$_SESSION['member_id']);
  While ($count_row = mysql_fetch_assoc($count_query)) {
    $count[] = array(
        'id' => $count_row['image_album_id'],
        'album' =>  $count_row['artist_id'],
        'count' => $count_row['image_count']
        );
  }
  return $count;
}
?>

<?php
$count = get_count($artist_id);
foreach ($count as $count){
  echo '',$count['count'],'';
}
?>
4

3 回答 3

1

问题在于$count对数组和标量变量重用相同的变量名:

$count = get_count($artist_id);

$count现在是一个数组。

foreach ($count as $count){

$count现在已经变成了一个标量变量,清除了数组。

于 2012-10-11T03:24:47.307 回答
0

像这样试试。

<?php
function get_count($artist_id) {
$artist_id = (int)$artist_id;
$count = array();
$count_query = mysql_query("SELECT `image_album_id`, `artist_id`, COUNT
(`image_album_id`) as `image_count` 
FROM `album_images`
WHERE `artist_id`=$artist_id AND `member_id`=".$_SESSION['member_id']);
$count_img_row=1;
While ($count_row = mysql_fetch_assoc($count_query)) {
$count[] = array(
'id' => $count_row['image_album_id'],
'album' =>  $count_row['artist_id'],
'count' => $count_row['image_count'],
'image_ord'=>$count_img_row
);
$count_img_row++;
}
return $count;
}
?>

<?php
$count = get_count($artist_id);
foreach ($count as $imgcount){
echo $imgcount['image_ord']." of ".$imgcount['count'];
}
?>
于 2012-10-11T04:25:24.623 回答
0

然后像这样尝试

<?php
function get_count($artist_id) {
$artist_id = (int)$artist_id;
$count = array();
$count_query = mysql_query("SELECT `image_album_id`, `artist_id`
FROM `album_images`
WHERE `artist_id`=$artist_id AND `member_id`=".$_SESSION['member_id']);
$count_img_row=1;
$total_imgs=mysql_num_rows($count_query);
While ($count_row = mysql_fetch_assoc($count_query)) {
$count[] = array(
'id' => $count_row['image_album_id'],
'album' =>  $count_row['artist_id']
);
$count_img_row++;
}
return $count;
}
?>

<?php
$count = get_count($artist_id);
foreach ($count as $imgcount){
echo $imgcount['image_ord']." of ".$total_imgs;
}
?>
于 2012-10-11T10:48:47.087 回答