面对阵列的瓶颈

时间:2014-05-08 10:09:07

标签: php sql

我有以下代码显示正确的结果,但遗憾的是我无法将这些结果传递到具有特定返回值的HTML页面。

这是我的sql语句:

$stmt = "SELECT * FROM "._CONST_TBL_EVENT_COMMENT." WHERE event_id = $record";

if($rs = $db->Execute($stmt))
{
    while($rsa = $rs->FetchRow())
    {

        array_push($arrResult, array(
            "id" => $record,
            "name" => $alumni->getAlumniNameForEventComment($rsa['member_id']),
            "file_path" => $alumni->getAlumniImageForEventComment($rsa['member_id']),
            "content" => $rsa['text'],
            "date" => $rsa['modified_timestamp']
        ));
    }
}

这是我得到的结果:

Array ( [0] =>
        Array ( [id] => 1 [name] =>
             Array ( [0] => Array ( [name] => alisdfsadf ) )
             [file_path] =>Array ( [0]=> Array ( [file_path] => hydrangeas_23197.jpg ) ) 
             [content] => test test test test 
             [date] => 2014-05-08 00:00:00 ) 
        [1] => 
        Array ( [id] => 1 [name] => 
             Array ( [0] => Array ( [name] => billy ) ) 
             [file_path] => Array ( [0] => Array ( [file_path] => ) )
             [content] => sdfasdfas test
             [date] => 2014-05-22 00:00:00 )
)

但我希望我的结果显示如下:

Array ( [0] => Array([id] => 1 [name] => alisdfsadf 
[file_path] => hydrangeas_23197.jpg 
[content] => test test test test 
[date] => 2014-05-08 00:00:00 )

我该怎么做?我希望有人可以帮助我。谢谢你提前。

2 个答案:

答案 0 :(得分:1)

$ alumni类上的函数似乎是返回数组。 根据您提供的输出,并尝试逆向工程......

while($rsa = $rs->FetchRow())
{
    $allNamesArray = $alumni->getAlumniNameForEventComment($rsa['member_id']);
    $firstNameArray = $allNamesArray[0];
    $name = $firstNameArray['name'];

    // similar for file path.

    array_push($arrResult, array(
        "id" => $record,
        "name" => $name,
        "file_path" => $alumni->getAlumniImageForEventComment($rsa['member_id']),
        "content" => $rsa['text'],
        "date" => $rsa['modified_timestamp']
    ));
}

答案 1 :(得分:1)

由于getAlumniXXXForEventComment函数返回数组,因此必须将它们编入索引以获取所需的字符串。

if($rs = $db->Execute($stmt))
{
    while($rsa = $rs->FetchRow())
    {
        $nameArray = $alumni->getAlumniNameForEventComment($rsa['member_id']);
        $name = is_array($nameArray) ? $nameArray[0]['name'] : $nameArray;
        $fileArray = $alumni->getAlumniImageForEventComment($rsa['member_id']);
        $file = is_array($fileArray) ? $fileArray[0]['file_path'] : $fileArray;
        array_push($arrResult, array(
            "id" => $record,
            "name" => $name,
            "file_path" => $file,
            "content" => $rsa['text'],
            "date" => $rsa['modified_timestamp']
        ));
    }