当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Looping array data from database

问题描述:

I have a little problem with looping odbc_fetch_array in php script...

problem is second: I have some data in my DB, and i want to search special data in it... I get data into array throught $row = odbc_fetch_array('$array'), but I am stucked only at last record in this row...

So i want to know how to print all data from this array...

here is my php code:

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

here is echo inside while

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

and here is var_dump outside while

array(65) {

[0]=>

string(89) "<tr><td>GA�INA </td><td>KARPOVA </td><td>08.03.56</td></tr>"

EDIT2

maybe there is a problem on my html page, where I am getting back this table? here is the code $.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); } });

网友答案:

The problem is your $data['result']['table'] is getting overwritten on every iteration leaving you will the last record.

Also I assume you want one table with multiple rows not multiple tables.

Try this:

$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;
分享给朋友:
您可能感兴趣的文章:
随机阅读: