2017-04-25 44 views
0

我要自动编号,而是由用户组PHP的编号通过唯一的ID分组

查询:

$query = "select distinct(DATE_FORMAT(a.in_time, '%Y%m%d')) as in_time, a.user_id, a.notes, b.nama from attendance a left join dat_login b on(a.user_id=b.ID) where DATE_FORMAT(a.in_time, '%Y')='2017' and DATE_FORMAT(a.in_time, '%M')='April' and id_shop!=2 order by b.nama asc"; 
$querynm = "select distinct(b.nama) as nama from attendance a left join dat_login b on(a.user_id=b.ID) where DATE_FORMAT(a.in_time, '%Y')='2017' and DATE_FORMAT(a.in_time, '%M')='April' group by b.nama order by b.nama asc"; 
$stid = mysqli_query($conn, $query) or die (mysqli_error($conn)); 
$stnm = mysqli_query($conn, $querynm) or die (mysqli_error($conn)); 

结果:

$alphabet_start = 'A'; 
while ($data = mysqli_fetch_assoc($stid)) { 
$data['user_id'] += 1; 
$no_cell = 11; 
echo $data['nama'] . " "; 
echo $data['user_id'] . " "; 
    if ($data['notes']=="") { 
    echo chr(ord("A") + date('d', strtotime($data['in_time']))) . $no_cell . " - " . chr(ord("A") + date('d', strtotime($data['in_time']))) . ". " . "Masuk <br>"; 
    } else if ($data['notes']=="S") { 
    echo chr(ord("A") + date('d', strtotime($data['in_time']))) . $no_cell . " - " . chr(ord("A") + date('d', strtotime($data['in_time']))) . ". " . "Sakit <br>"; 
    } else if ($data['notes']=="L") { 
    echo chr(ord("A") + date('d', strtotime($data['in_time']))) . $no_cell . " - " . chr(ord("A") + date('d', strtotime($data['in_time']))) . ". " . "Libur <br>"; 
    } 
$alphabet_start++; 
$no_cell++; 
$data['user_id']++; 
} 

但是,该值显示是这样的:

enter image description here

我想要的值显示如下:

enter image description here

正如你所看到的数量在接下来的名称改变,该怎么办呢? 对不起,我的语法错误。以前感谢:)

回答

1

你的代码有点混乱,但我想我明白你的意思。 你可以这样做:

$alphabet_start = 'A'; 
$userID = ""; 
$cells = array(); 
$cellNum = 11; 

while ($data = mysqli_fetch_assoc($stid)) { 
    $data['user_id'] += 1; 

    if (in_array($data['user_id'], $cells)) { 
     $no_cell = $cellNum; 
    } else { 
     $no_cell = ($cellNum += 1); 
     $cells[] = $data['user_id']; 
    } 

    echo $data['nama'] . " "; 
    echo $data['user_id'] . " "; 
    if ($data['notes'] == "") { 
     echo chr(ord("A") + date('d', strtotime($data['in_time']))) . $no_cell . " - " . chr(ord("A") + date('d', strtotime($data['in_time']))) . ". " . "Masuk <br>"; 
    } else if ($data['notes'] == "S") { 
     echo chr(ord("A") + date('d', strtotime($data['in_time']))) . $no_cell . " - " . chr(ord("A") + date('d', strtotime($data['in_time']))) . ". " . "Sakit <br>"; 
    } else if ($data['notes'] == "L") { 
     echo chr(ord("A") + date('d', strtotime($data['in_time']))) . $no_cell . " - " . chr(ord("A") + date('d', strtotime($data['in_time']))) . ". " . "Libur <br>"; 
    } 
    $alphabet_start++; 
    $no_cell++; 
    $data['user_id']++; 
} 
+0

谢谢你的答案先生。 styl3r,但user_id值是从数据库中获得的,第一个值并不总是52,而user_id值非常多。我不能一个一个地在开关盒里做。 –

+0

你如何计划增加'$ no_cell'? – styl3r

+0

$ no_cell在每个名称中递增,一个名称为一个名称 –