php - 如何使用 php 对 id 进行分组并内联日期

标签 php mysql loops

这是我的示例代码,我想对 ID 进行分组并内联日期,如下图所示

 $sql_e = mysqli_query($link,"SELECT * FROM tbl_attendees");
 while($sql_e_res = mysqli_fetch_array($sql_e)){
    echo'<tr>
    <td>'.$sql_e_res['s_usn'].'</td>
    <td>'.$sql_e_res['s_name'].'</td>
    ';
    $dt = ''.$sql_e_res['at_date'].'';
    $dt = strtotime(str_replace(',', '', $dt));
    $d = date('j',$dt);

    $currentdays = intval(date("t"));
    $i = 0;
    while ($i++ < $currentdays){
        if($i == $d){
            $ff='<td style="text-align:center">'.$d.'</td>';            
        }else{
            $ff='<td style="text-align:center">';
        }
            echo $ff;
    }
        echo'</tr>';                                                    
    }

Original

What i want

最佳答案

正如 xQbert 所说,您需要按 s_usn 对查询进行排序

编辑:我的代码期望处理的查询是 "SELECT * FROM tbl_attendees ORDER BY s_usn asc;"

<小时/> 我想这段代码有机会得到更加优化,但我试图让我的编码风格非常接近你的风格并重用你的 var 名称。我还尝试将代码的可读性放在首位。尝试这个代码,我几乎评论了所有内容

代码是一个关于抛出结果行的大循环,对于每一行,您将检查这是否是新学生。如果是新学生,那么您将在名为 $daysTDs 的字符串中构建前一个学生的参加日期。看起来像这样 "<td>1<td><td><td><td>3<td>...."

我们将从一个名为 $attendees 的数组构建这个字符串。包含该学生参加的所有天数,可能看起来像这样

  $attendees = [12,10]

当我们遇到新学生时,我们会回应$daysTDs前一个学生的行,并按 </tr> 关闭该行循环完成后,我们还将回显最后一个学生 $daysTDs字符串并按 </tr> 关闭它的行

$lastId = "";  //var to check the new students
$daysTDs = ""; // a string holding the TDs of the student e.g '<td>1<td><td><td><td>3<td>....'
$attendees = []; //array to hold the days 
$currentdays = intval(date("t"));

//start query result loop
 while($sql_e_res = mysqli_fetch_array($sql_e)){
    if($sql_e_res['s_usn']!=$lastId){//if new student
        $i = 0;
        //new student ? then build the $daysTDs string from $attendees attay for the previous student
        while ($i++ < $currentdays){ //fot the first student it will be empty and will not get echoed
            if(in_array($i, $attendees)){$daysTDs .= "<td>$i</td>";}
            else{$daysTDs .= "<td></td>";}
        }
        if($lastId!=""){
            echo $daysTDs;//if not first student append the $daysTDs
            echo'</tr>'; //if not first student, then close the row </tr>       
        }
        $attendees = []; // flush the array for the next student
        $daysTDs=""; // empty the TDs string for the next student
        echo'<tr>
        <td>'.$sql_e_res['s_usn'].'</td>
        <td>'.$sql_e_res['s_name'].'</td>';
    }
    $lastId=$sql_e_res['s_usn'];

    $dt = ''.$sql_e_res['at_date'].'';
    $dt = strtotime(str_replace(',', '', $dt));
    $d = date('j',$dt);
    $attendees[]=$d; //push that day to this student attendees array                                               
}
//after the loop is ended finish the last student row
$i=0;
while ($i++ < $currentdays){
    if(in_array($i, $attendees)){$daysTDs .= "<td>$i</td>";}
    else{$daysTDs .= "<td></td>";}
}
echo $daysTDs;
echo'</tr>';

检查代码并告诉我它是否达到了预期的结果

关于php - 如何使用 php 对 id 进行分组并内联日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40613202/

相关文章:

php - 为什么我不能在 MySQL 升级后向列中插入 NULL 值?

php - 在wordpress中使背景图像动态化

MySQL 两个表最小值和分组依据

php - 使用MySQL在插入时生成半随机字符串

python - 由于循环而导致列表越界

javascript - JSON 数据未正确填充 Bootstrap 表

javascript - 如何使 jQuery/Javascript 在 PHP 语句中工作?

mysql - Sql select 一起删除

C# 增量数组

java - 我如何将这个 for every 转换为 for 循环?