使用Count来获得总数(例如:10个中的2个,10个中的3个)

时间:2012-10-11 03:21:14

标签: php

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

<?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'],'';
}
?>

3 个答案:

答案 0 :(得分:1)

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

$count = get_count($artist_id);

$count现在是一个数组。

foreach ($count as $count){

但是$count现在已成为标量变量,消灭了数组。

答案 1 :(得分: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'];
}
?>

答案 2 :(得分: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;
}
?>