简体   繁体   English

通过唯一ID分组对PHP进行编号

[英]PHP numbering by unique ID grouping

I want to auto numbering but by user group 我想按用户组自动编号

Query : 查询:

$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));

Result : 结果:

$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']++;
}

But, the value showing like this : 但是,显示的值是这样的:

在此处输入图片说明

I want the value showing like this : 我想要这样显示的值:

在此处输入图片说明

As you can see the number is changing in the next name, how to do it ? 如您所见,姓氏中的数字正在变化,该怎么办? Sorry for my bad grammar. 对不起,我的语法不好。 Thanks before :) 之前感谢:)

Your code is a bit confusing but I think I understand what you mean. 您的代码有点混乱,但是我想我理解您的意思。 You can do it as such: 您可以这样做:

$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']++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM