簡體   English   中英

通過唯一ID分組對PHP進行編號

[英]PHP numbering by unique ID grouping

我想按用戶組自動編號

查詢:

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

但是,顯示的值是這樣的:

在此處輸入圖片說明

我想要這樣顯示的值:

在此處輸入圖片說明

如您所見,姓氏中的數字正在變化,該怎么辦? 對不起,我的語法不好。 之前感謝:)

您的代碼有點混亂,但是我想我理解您的意思。 您可以這樣做:

$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