  | 
              
	              
	                
  
    
	 
        สอบถามการเช็คเงื่อนไขของข้อมูลกับวันที่หน่อค่ะ รบกวนด่วนทีนะคะ     | 
   
  
    |   | 
   
 
 
 
	
		
			  | 
	   | 
	    | 
		
			  | 
	 
	
		
			  | 
		 		   | 
	  	    
          
            
			
	
			
			 
                เป็นรายการตารางเวรอ่ะค่ะ โดยกำหนด  
1 ตาราง group มี 
id , groupname 
เช่น 
1 = group A 
2 = group B 
3 = group C 
 
2. ตาราง student มี 
id , groupid , name ,datep 
เช่น 
 
id | groupid | name | datep 
1 , 1 , สมชาย , 2017-12-6 
2 , 2 , สมปอง , 2017-12-6 
3 , 2 , สมศรี , 2017-12-6 
 
จากตัวอย่างความต้องการคือ 
ตารางจะแสดง Group A ถึง Group C โดยที่ ตรวจสอบว่า ตาราง student ตรงกับวันนี้หรือไม่ ถ้าตรงก็แสดงหัว Group มาก่อน แล้วใส่รายชื่อนักเรียนที่เป็นเวรของ Group นั้น แล้วค่อยแสดง Group ถัดไป 
 
จากข้อมูลเบื้องต้น วันที่ 2017-12-6 จะไม่มี Group C ก็คือจะไม่แสดง 
ต้องเขียนยังไงคะ เพราะตอนนี้ที่เขียน มันแสดงทุก Group ค่ะ แต่ข้อมูลจะมีแค่ Group B และ C 
 
รบกวนทีนะคะ 
 
Code (PHP) 
<?
$sqlg = "select * from group  order by id asc";
$resultg = mysql_query($sqlg) or die(mysql_error());
while($rowg = mysql_fetch_array($resultg))
{
$idg = $rowg['id'];
$groupname = $rowg['groupname'];
// แสดงหัวข้อ GROUP
echo "------------ $groupname ----------------<br>";
$chkdate = date('Y-m-d');
$sqlstudent = "select * from student where groupid = '$idg' and datep = 'chkdate'";
$resultstu = mysql_query($sqlstudent) or die(mysql_error());
while($rowst = mysql_fetch_array($resultstu))
{
$idstu = $rowst['id'];
$name = $rowst['name'];
// แสดงรายชื่อนักเรียนที่อยู่ใน GROUP
echo "$name";
}
}
?>
 
 
  Tag : PHP, MySQL, HTML, HTML5               
                        | 
           
          
            
		
  ประวัติการแก้ไข 2017-12-06 13:26:23	
                             | 
           
          
            
              
                   | 
                   | 
                   | 
               
              
                   | 
                
                    
                      | Date :
                          2017-12-06 13:25:53 | 
                      By :
                          because | 
                      View :
                          817 | 
                      Reply :
                          5 | 
                     
                  | 
                   | 
               
              
                   | 
                   | 
                   | 
               
              | 
           
          
            | 
			 | 
           
         
	    
		             | 
		
			  | 
	 
	
		
			  | 
		  | 
		
			  | 
		
			  | 
	 
 
              
  
          
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ดันหน่อยค่ะ                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2017-12-06 14:07:19 | 
                        By :
                            because | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ตก $ 
$sqlstudent = "select * from student where groupid = '$idg' and datep = '$chkdate'"; 
 
ซึ่งจากโค๊ดมันไม่น่าจะแสดงอะไรออกมาเลย 
 
คิวรี่ที่ถูก 
Code (SQL) 
select g.id, s.name 
from (select groupid as id from student where datep='$chkdate') as g
left join student s on s.group_id=g.id
                         
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2017-12-06 14:44:02 | 
                        By :
                            Chaidhanan | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 รบกวนอีกทีนะคะ เอา code เต็มๆ มาให้ดูค่ะ 
 
Code (PHP) 
<?
require_once "connectdb.php";
$chk_d = date('Y-m-d');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="style.css" rel="stylesheet" type="text/css">
<title>Untitled Document</title>
</head>
<body>
<div align="center">วันที่ : <? echo "$chk_d"; ?><br />
  <?
$num=1;	  
$sqlgroup = "select * from grouptb  order by id asc";
$resultcg = mysql_query($sqlgroup) or die(mysql_error());
while($rowg = mysql_fetch_array($resultcg))
{
$idgroup = $rowg['id'];
$groupname = $rowg['groupname'];
?>
</div>
<table width="28%" border="0" align="center" cellpadding="6" cellspacing="0">
  <tr>
    <td height="33" bgcolor="#990000"><font color="#FFFF00"><? echo "$groupname"; ?></font></td>
  </tr>
  <tr>
    <td bgcolor="#99CCFF"><?
$today_chk = date('Y-m-d');
$num=1;	  
$sqlprg = "select * from studenttb where dateregist = '$today_chk' and groupid = '$idgroup'";
$resultpg = mysql_query($sqlprg) or die(mysql_error());
while($rowpg = mysql_fetch_array($resultpg))
{
$idstu = $rowpg['id'];
$name = $rowpg['name'];
$position = $rowpg['position'];
?>
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td height="35" bgcolor="#FFFFFF"><table width="100%" border="0" cellspacing="1" cellpadding="8">
            <tr>
              <td width="53%" height="23"><div align="left"><font class="txtkanda_r_18"><? echo "$name"; ?></font></div></td>
              <td width="47%"><div align="center"><font class="txtkanda_r_18"><? echo "$position"; ?></font></div></td>
              </tr>
          </table></td>
        </tr>
      </table>
    <?
	  $num=$num+1;
	  }
	  ?></td>
  </tr>
</table>
<?
	  $num=$num+1;
	  }
	  ?>
	  	  
</body>
</html>
 
 
วันนี้ 2017-12-06 จะมีข้อมูลแค่ กลุ่ม A และ กลุ่ม B แต่ กลุ่ม C ไม่มีข้อมูล เลยไม่อยากให้แสดงGroup นี้อ่ะค่ะ 
 
ผลลัพท์ที่ http://www.fiinn.co/stulist.php ค่ะ                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2017-12-06 16:09:00 | 
                        By :
                            because | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 เอา statement ที่ผมทำให้ดูไปลองใน phpmyadmin ว่ามันได้อะไรมาบ้าง ครบไหม 
แล้วเขียนโค๊ด php ใหม่ให้แสดงผลลัพธ์ตามต้องการ 
 
เพราะอัลกอลิธึม ของโค๊ดที่คุณทำไว้ มันไม่ให้ผลลัพธ์ที่ต้องการ 
พูดง่ายตีโจทย์ไม่ถูก 
 
ที่ต้องการกลุ่มของนักเรียนที่มี วันที่ในวันนั้น  
Code (SQL) 
select g,* from `student` s, `group` g
where g.id=s.groupid and s.datep='$chkdate'
group by g.id
  
แล้วค่อยเอา result ไปคิวรี่หานักเรียน 
Code (SQL) 
select * from student where groupid=$result[id]
  
 
ปล. ข้างบนเป็นวิธีที่ไม่ดีนักเพราะ ทำการคิวรี่ 2 ครั้ง แต่มันง่ายในการเขียนโค๊ด php 
จะดีกว่าถ้าคิวรี่ครั้งเดียวจากตัวอย่างแรก แล้วใช้ php จัด report                        
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2017-12-06 17:03:40              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2017-12-06 17:02:37 | 
                        By :
                            Chaidhanan | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
      		  
	
     | 
   
 
                 |