2017-08-02 70 views
-1

我創建了一個功能,從表中獲取數據。打印數據轉換成while循環從功能

現在我想返回的數據打印到使用while循環的表。
幾乎一切就OK了,但是當我調用該函數,它將只打印第一行。

$usr = $_SESSION['MM_UID']; 
function getData($leavetype,$eo, $usr) 
{ 
    $get = "SELECT * FROM `leaveregister` WHERE employee = '$usr' AND `leaveType`='$leavetype' "; 
    $getdata = mysqli_query($eo, $get); 
    while($geta = mysqli_fetch_assoc($getdata)) 
    { 
     return($geta); 
    } 
} 
echo '<strong>Annual Leaves</strong>'; 
echo '<table class="table basicTable"><thead><th>Leave Duration</th><th>Start Date</th><th>End Date</th></thead>'; 
echo '<tbody>'; 
$getb = getData('annual',$eo,$usr); 
echo '<tr><td>'.$getb['leaveDays'].'</td><td>'.$getb['startDate'].'</td><td>'.$getb['endDate'].'</td><td>'.$getb['remarks'].'</td></tr>'; 

echo '<tr><strong></tr>'; 
echo '</tbody></table>'; 

請注意:$eo是數據庫連接。

回答

1

您可以使用while循環: 下面是示例代碼:

$usr = $_SESSION['MM_UID']; 
function getData($leavetype,$eo, $usr) 
{ 
    $get = "SELECT * FROM `leaveregister` WHERE employee = '$usr' AND `leaveType`='$leavetype' "; 
    $getdata = mysqli_query($eo, $get); 
    return($getdata); 
} 
echo '<strong>Annual Leaves</strong>'; 
echo '<table class="table basicTable"><thead><th>Leave Duration</th><th>Start Date</th><th>End Date</th></thead>'; 
echo '<tbody>'; 
$getc = getData('annual',$eo,$usr); 
while($getb = mysqli_fetch_assoc($getc)){ 
    echo '<tr><td>'.$getb['leaveDays'].'</td><td>'.$getb['startDate'].'</td><td>'.$getb['endDate'].'</td><td>'.$getb['remarks'].'</td></tr>'; 
} 

echo '<tr><strong></tr>'; 
echo '</tbody></table>'; 
0

嘗試foreach

function getData($leavetype,$eo, $usr) 
    { 
    $get = "SELECT * FROM `leaveregister` WHERE employee = '$usr' AND `leaveType`='$leavetype' "; 
    $getdata = mysqli_query($eo, $get); 
    return $geta = mysqli_fetch_assoc($getdata))   
    } 



$getb = getData('annual',$eo,$usr); 
$table = ''; 
foreach($getb as $b){ 
    $table .='<tr><td>'.$b['leaveDays'].'</td><td>'; 
    $table .=$b['startDate'].'</td><td>'.$b['endDate'].'</td>'; 
    $table .='<td>'.$b['remarks'].'</td></tr>'; 

} 
echo $table; 
0

問題是在這裏。

while($geta = mysqli_fetch_assoc($getdata)) 
     { 
      return($geta); 
     } 
     //return here enter code here 

嘗試在一些數據結構收集的所有數據(例如陣列)和最後一次迭代返回變量之後。

+0

我該怎麼辦? – Lanka

0

的問題是,getData返回一個結果。讓我們來解決這個問題:

function getData($leavetype,$eo, $usr) 
{ 
    $get = "SELECT * FROM `leaveregister` WHERE employee = '$usr' AND `leaveType`='$leavetype' "; 
    $getdata = mysqli_query($eo, $get); 
    $result = array(); 
    while($geta = mysqli_fetch_assoc($getdata)) 
    { 
     $result[] = $geta; 
    } 
    return $result; 
} 

現在你可以調用它並循環遍歷你的表。

$results = getData('annual',$eo,$usr); 
?> 
<strong>Annual Leaves</strong> 
<table class="table basicTable"> 
    <thead> 
     <th>Leave Duration</th> 
     <th>Start Date</th> 
     <th>End Date</th> 
    </thead> 
    <tbody> 

<?php foreach($results as $getb) { 
    echo '<tr><td>'.$getb['leaveDays'].'</td><td>'.$getb['startDate'].'</td> 
    <td>'.$getb['endDate'].'</td><td>'.$getb['remarks'].'</td></tr>'; 
} 
echo '<tr><strong></tr>'; 
echo '</tbody></table>'; 
?> 
0

您返回結果而不檢索所有數據。改變你的while語句,你的問題就會解決。

$get = "SELECT * FROM `leaveregister` WHERE employee = '$usr' AND `leaveType`='$leavetype' "; 
$result = array(); 
$getdata = mysqli_query($eo, $get); 
while($geta = mysqli_fetch_assoc($getdata)) 
{ 
    $result[] = $geta; 
} 
return $result;