  | 
		 		   | 
	  	    
          
            
			
	
			
			 
                คือทำให้มัน export เป็นไฟล์ excel ได้แล้วนะคะ แต่มันออกมาทั้ง database เลยอะคะ  อยากให้มันออกมาแค่เฉพาะที่เรากดค้นหาอะคะ รบกวนช่วยดูโค้ดให้หน่อยนะคะ 
 
ล่างนี้คือฟอร์มหน้าค้นหาคะ 
  
 
โค้ดหน้าฟอร์มค้นหาคะ 
Code (PHP) 
<?php require_once('../Connections/Connect.php'); ?>
<?php
session_start();
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }
  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
$colname_member1 = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_member1 = $_SESSION['MM_Username'];
}
mysql_select_db($database_Connect, $Connect);
$query_member1 = sprintf("SELECT * FROM tb_member WHERE m_username = %s", GetSQLValueString($colname_member1, "text"));
$member1 = mysql_query($query_member1, $Connect) or die(mysql_error());
$row_member1 = mysql_fetch_assoc($member1);
$totalRows_member1 = mysql_num_rows($member1);
$colname_show_register = "-1";
if (isset($_POST['re_type'])) {
  $colname_show_register = $_POST['re_type'];
}
mysql_select_db($database_Connect, $Connect);
$query_show_register = sprintf("SELECT * FROM tb_register as r INNER JOIN tb_faculty as f ON r.re_type=f.fa_id WHERE fa_type LIKE %s", GetSQLValueString("%" . $colname_show_register . "%", "text"));
$show_register = mysql_query($query_show_register, $Connect) or die(mysql_error());
$row_show_register = mysql_fetch_assoc($show_register);
$totalRows_show_register = mysql_num_rows($show_register);
$colname_show_typefac = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_show_typefac = $_SESSION['MM_Username'];
}
mysql_select_db($database_Connect, $Connect);
$query_show_typefac = sprintf("SELECT fa_type FROM tb_faculty WHERE fa_username = %s and fa_status='' ORDER BY fa_id DESC", GetSQLValueString($colname_show_typefac, "text"));
$show_typefac = mysql_query($query_show_typefac, $Connect) or die(mysql_error());
$row_show_typefac = mysql_fetch_assoc($show_typefac);
$totalRows_show_typefac = mysql_num_rows($show_typefac);
?>
<form method="post" action="form-delete-register.php" name="form1">
  <div class="form-group row">
    <label for="fa_faculty1" class="col-sm-2 col-form-label">กรุณาเลือกชื่อโครงการที่ต้องการค้นหา :</label>
    <div class="col-md-3">
    <label for="inputPassword2" class="sr-only"></label>
    <select name="re_type" size="1" id="re_type" class="form-control-sm col-md-12">
      <option value="<?php echo $row_show_register['re_type']; ?>"><?php echo $row_show_register['fa_type']; ?></option>
      <?php
do {  
?>
      <option value="<?php echo $row_show_typefac['fa_type']?>"><?php echo $row_show_typefac['fa_type']?></option>
      <?php
} while ($row_show_typefac = mysql_fetch_assoc($show_typefac));
  $rows = mysql_num_rows($show_typefac);
  if($rows > 0) {
      mysql_data_seek($show_typefac, 0);
	  $row_show_typefac = mysql_fetch_assoc($show_typefac);
  }
?>
    </select>
  </div>
  <button type="submit" class="btn btn-primary mb-2 btn-sm">ค้นหา</button>
    
   </div>
</form>
<form method="post" action="export.php">
									 
                                      <button type="submit" name="export" class="btn btn-success btn-sm"><i class="fa fa-file-excel-o"></i> บันทึกไฟล์ Excel</button>
</form>
<br />
<table class="table table-bordered" id="table_re">
  <thead align="center" class="table-primary">
    <tr>
      <th scope="col">รหัสผู้เข้าอบรม</th>
      <th scope="col">หัวข้อโครงการ</th>
      <th scope="col">ชื่อ - นามสกุล</th>
      <th scope="col">หน่วยงาน</th>
      <th scope="col">ตำแหน่ง</th>
      <th scope="col">เบอร์โทรศัพท์</th>
      <th scope="col">#</th>
    </tr>
    <?php do { ?>
  </thead>
  <tbody>
    <tr>
      <td align="center"><?php echo $row_show_register['re_id']; ?></td>
      <td align="center"><?php echo $row_show_register['fa_type']; ?></td>
      <td align="center"><?php echo $row_show_register['re_no']; ?>
	  <?php echo $row_show_register['re_name']; ?><br />
      สมัครวันที่ : <?php echo $row_show_register['re_date']; ?>
      </td>
      <td align="center"><?php echo $row_show_register['re_company'];?><br />
	  <?php echo $row_show_register['re_address']; ?>
      </td>
      <td align="center"><?php echo $row_show_register['re_position']; ?></td>
      <td align="center"><?php echo $row_show_register['re_tel']; ?></td>
      <td align="center"><a href="update_register.php?re_id=<?php echo $row_show_register['re_id']; ?>" onClick="javascript:return confirm('คุณต้องการลบข้อมูลใช่หรือไม่');" title="ลบรายชื่อผู้ลงทะเบียน"><i class="far fa-trash-alt"></i></a></td>
  </tbody>
  <?php } while ($row_show_register = mysql_fetch_assoc($show_register)); ?>
</table>
<?php
mysql_free_result($member1);
mysql_free_result($show_register);
mysql_free_result($show_typefac);
?>
</body>
</html>
 
 
 
ส่วนอันนี้คือโค้ดหน้า export.php คะ 
Code (PHP) 
<?php require_once('../Connections/Connect.php'); ?>
<?php  
$output = '';
	
if(isset($_POST["export"]))
{
$query_show_register = "SELECT r.*,f.fa_type 
FROM tb_register as r
INNER JOIN tb_faculty as f 
ON r.re_type=f.fa_id
WHERE r.re_type=f.fa_id
ORDER BY r.id_re ASC";
$show_register = mysql_query($query_show_register, $Connect);
 if(mysql_num_rows($show_register) > 0)
 {
  $output .= '
   <table class="table table-bordered" id="table_re">
		                                    	
		<tr>
			<th>หัวข้อโครงการอบรม</th>
			<th class="center">รหัสผู้เข้าร่วมอบรม</th>
			<th class="center">ชื่อ - นามสกุล</th>
			<th class="center">หน่วยงาน</th>
			<th class="center">ตำแหน่ง</th>
			<th class="center">เบอร์โทรศัพท์</th>
			<th class="center">อีเมล์</th>
		</tr>
  ';
  while($row_show_register = mysql_fetch_array($show_register))
  {
   $output .= '
    <tr>  
            <td>'.$row_show_register["fa_type"].'</td>  
            <td>'.$row_show_register["re_id"].'</td>  
            <td>'.$row_show_register["re_no"].''.$row_show_register["re_name"].'</td>  
       		<td>'.$row_show_register["re_company"].'</td>  
       		<td>'.$row_show_register["re_position"].'</td>
			<td>'.$row_show_register["re_tel"].'</td>
			<td>'.$row_show_register["re_email"].'</td>
      </tr>
   ';
  }
  $output .= '</table>';
  header('Content-Type: application/xls');
  header('Content-Disposition: attachment; filename=download.xls');
  echo $output;
 }
}
?>
 
 
  Tag : PHP, MySQL, XAMPP, Windows               
                        | 
           
          
            
		
  ประวัติการแก้ไข 2019-02-08 09:46:03 2019-02-08 09:47:24 2019-02-08 09:48:12 2019-02-08 16:12:39	
                             | 
           
          
            
              
                   | 
                   | 
                   | 
               
              
                   | 
                
                    
                      | Date :
                          2019-02-08 09:45:41 | 
                      By :
                          memyzelf00 | 
                      View :
                          1127 | 
                      Reply :
                          2 | 
                     
                  | 
                   | 
               
              
                   | 
                   | 
                   | 
               
              | 
           
          
            | 
			 | 
           
         
	    
		             | 
		
			  |