从数据库中循环数组数据

时间:2013-08-16 07:46:23

标签: php odbc

我在php脚本中循环odbc_fetch_array有点问题... 问题是第二:我的数据库中有一些数据,我想在其中搜索特殊数据......我通过$row = odbc_fetch_array('$array')将数据输入到数组中,但我只是在这一行的最后一条记录中被困... 所以我想知道如何打印这个数组中的所有数据...

这是我的PHP代码:

while ($row = odbc_fetch_array($surnames))
                {
                    echo json_encode($row);

                    /*$f_name = trim(odbc_result($surnames, "F_NAME"));
                    $s_name = trim(odbc_result($surnames, "S_NAME"));*/

                    //getting birthdate from personal id
                    $bd = trim(odbc_result($surnames, "PERS_KOD"));

                    $day = substr($bd, 0, 2);
                    $month = substr($bd, 2, 2);
                    $year = substr($bd, 4, 2);

                    $birthdate = $day.".".$month.".".$year;

                    $table = "
                                <table>
                                    <th>Name</th>
                                    <th>Surname</th>
                                    <th>Birth Date</th>

                                    <tr>
                                        <td>".$row['f_name']."</td>
                                        <td>".$row['s_name']."</td>
                                        <td>".$birthdate."</td>
                                    </tr>
                                </table>
                             "; 
                    $data['result']['table'] = $table;
                }

EDIT1

这里是echo,而

json_encode(tr)<tr><td>ALEVTINA <\/td><td>KARPOVA <\/td><td>14.07.27<\/td><\/tr>"null"json_encode(tr)<tr><td>DACE <\/td><td>KARPOVA <\/td><td>08.10.77<\/td><\/tr>"nullnullnull"json_encode(tr)<tr><td>OLGA <\/td><td>KARPOVA

并且

以外是var_dump

array(65) { [0]=> string(89) "<tr><td>GA�INA </td><td>KARPOVA </td><td>08.03.56</td></tr>"

EDIT2

也许在我的html页面上有问题,我要回到这张桌子?这是代码$.ajax ({ data: { action: "search_patient_surname", surname: inserted_surname }, success: function(a) { console.log(a); if (a.errors.length > 0) { //vivodit input_error.val(a.errors.join(' | ')); } $('#Persons').html(a.result.table); } });

1 个答案:

答案 0 :(得分:1)

问题是你的$data['result']['table']在每次迭代时都会被覆盖,留下你最后的记录。

此外,我假设你想要一个包含多行而不是多个表的表。

试试这个:

$result_tr = array();
while ($row = odbc_fetch_array($surnames))
{
    echo json_encode($row);

    /*$f_name = trim(odbc_result($surnames, "F_NAME"));
    $s_name = trim(odbc_result($surnames, "S_NAME"));*/

    //getting birthdate from personal id
    $bd = trim(odbc_result($surnames, "PERS_KOD"));

    $day = substr($bd, 0, 2);
    $month = substr($bd, 2, 2);
    $year = substr($bd, 4, 2);

    $birthdate = $day.".".$month.".".$year;

    $tr = "<tr><td>{$row['f_name']}</td><td>{$row['s_name']}</td><td>{$birthdate}</td></tr>";
    $result_tr[] = $tr;
}

$table = "";
if (!empty($result_tr)) {
    $table = "<table><th>Name</th><th>Surname</th><th>Birth Date</th>";
    foreach ($result_tr as $row) {
        $table .= $row;
    }
    $table .= "</table>";
}

$data['result']['table'] = $table;