将数据打印到函数

时间:2017-08-02 04:34:28

标签: php

我创建了一个从表中获取数据的函数。

现在我想使用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);
    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是数据库连接。

5 个答案:

答案 0 :(得分: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>';

答案 1 :(得分:1)

问题是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>';
?>

答案 2 :(得分: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;

答案 3 :(得分:0)

问题在这里。

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

尝试收集某些数据结构中的所有数据(例如数组),并在最后一次迭代后返回变量。

答案 4 :(得分: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;