如何在while循环中返回所有元素?

时间:2018-07-22 19:52:37

标签: php database function pdo

我有一个从数据库中获取日志的功能,但是每当我试图回显此信息时,都可以。我只有第一项。

        function getCoins($odb) {
        $website = new website();
        $SQLGetCoinsLog = $odb -> query("SELECT * FROM `user_logs` ORDER BY `id` DESC");
        while ($getInfo = $SQLGetCoinsLog -> fetch(PDO::FETCH_ASSOC)) {
            $id = $getInfo['id'];
            $userid = $getInfo['userid'];
            $nazwa = $getInfo['nazwa'];
            $cena = $getInfo['cena'];
            $transid = $getInfo['transid'];
            $data = $website->converTimestamp($getInfo['data']);
            $result = '
                <tr class="text-center">
                    <td><a href="https://steamcommunity.com/profiles/'.$userid.'" target="_blank">'.$userid.'</a></td>
                    <td>'.$nazwa.'</td>
                    <td>'.$cena.'PLN</td>
                    <td>'.$transid.'</td>
                    <td>'.$data.'</td>
                </tr>                                           
            ';
        }
        return $result;
        }

3 个答案:

答案 0 :(得分:0)

添加

$result = "";

在函数顶部定义它(在while循环之前),然后更改

$result = ' <tr class="text-center"> <td><a href="https://steamcommunity.com/profiles/'.$userid.'" target="_blank">'.$userid.'</a></td> <td>'.$nazwa.'</td> <td>'.$cena.'PLN</td> <td>'.$transid.'</td> <td>'.$data.'</td> </tr> ';

收件人

$result .= ' <tr class="text-center"> <td><a href="https://steamcommunity.com/profiles/'.$userid.'" target="_blank">'.$userid.'</a></td> <td>'.$nazwa.'</td> <td>'.$cena.'PLN</td> <td>'.$transid.'</td> <td>'.$data.'</td> </tr> ';

请注意,等号旁边的点将添加到您的变量中,而不会覆盖您的变量。

答案 1 :(得分:0)

您有一些选择。
在循环中回显它们(不会“返回”它们),这样实际上并不能回答您的问题,但是由于它是您构建的表,因此在某些时候我认为必须将其回显。

   function getCoins($odb) {
    $website = new website();
    $SQLGetCoinsLog = $odb -> query("SELECT * FROM `user_logs` ORDER BY `id` DESC");
    while ($getInfo = $SQLGetCoinsLog -> fetch(PDO::FETCH_ASSOC)) {
        $id = $getInfo['id'];
        $userid = $getInfo['userid'];
        $nazwa = $getInfo['nazwa'];
        $cena = $getInfo['cena'];
        $transid = $getInfo['transid'];
        $data = $website->converTimestamp($getInfo['data']);
        echo'
            <tr class="text-center">
                <td><a href="https://steamcommunity.com/profiles/'.$userid.'" target="_blank">'.$userid.'</a></td>
                <td>'.$nazwa.'</td>
                <td>'.$cena.'PLN</td>
                <td>'.$transid.'</td>
                <td>'.$data.'</td>
            </tr>                                           
        ';
    }
    }

或者您可以使用结果构建一个数组:

   function getCoins($odb) {
    $website = new website();
    $SQLGetCoinsLog = $odb -> query("SELECT * FROM `user_logs` ORDER BY `id` DESC");
    while ($getInfo = $SQLGetCoinsLog -> fetch(PDO::FETCH_ASSOC)) {
        $id = $getInfo['id'];
        $userid = $getInfo['userid'];
        $nazwa = $getInfo['nazwa'];
        $cena = $getInfo['cena'];
        $transid = $getInfo['transid'];
        $data = $website->converTimestamp($getInfo['data']);
        $result[] = '
            <tr class="text-center">
                <td><a href="https://steamcommunity.com/profiles/'.$userid.'" target="_blank">'.$userid.'</a></td>
                <td>'.$nazwa.'</td>
                <td>'.$cena.'PLN</td>
                <td>'.$transid.'</td>
                <td>'.$data.'</td>
            </tr>                                           
        ';
    }
    return $result;
    }

//To echo them in main program:
Echo implode("\n", getCoins($odb));

或者您可以使用所有结果构建一个字符串:

    function getCoins($odb) {
    $website = new website();
    $SQLGetCoinsLog = $odb -> query("SELECT * FROM `user_logs` ORDER BY `id` DESC");
    $result = "";
    while ($getInfo = $SQLGetCoinsLog -> fetch(PDO::FETCH_ASSOC)) {
        $id = $getInfo['id'];
        $userid = $getInfo['userid'];
        $nazwa = $getInfo['nazwa'];
        $cena = $getInfo['cena'];
        $transid = $getInfo['transid'];
        $data = $website->converTimestamp($getInfo['data']);
        $result .= '
            <tr class="text-center">
                <td><a href="https://steamcommunity.com/profiles/'.$userid.'" target="_blank">'.$userid.'</a></td>
                <td>'.$nazwa.'</td>
                <td>'.$cena.'PLN</td>
                <td>'.$transid.'</td>
                <td>'.$data.'</td>
            </tr>                                           
        ';
    }
    return $result;
    }

答案 2 :(得分:0)

您为数据库行的结果提供的这些变量无用,或者您无法受益,因为它们都是local variables,或者在重复循环后将它们删除并重新创建,因为您是在循环中创建的,应该在循环外创建它们,(例如)我认为您只需要结果,所以我只在循环外添加结果:-

function getCoins($odb) {
    $website = new website();
    $SQLGetCoinsLog = $odb -> query("SELECT * FROM `user_logs` ORDER BY `id` DESC");
    $result = array();
    while ($getInfo = $SQLGetCoinsLog -> fetch(PDO::FETCH_ASSOC)) {
        $id = $getInfo['id'];
        $userid = $getInfo['userid'];
        $nazwa = $getInfo['nazwa'];
        $cena = $getInfo['cena'];
        $transid = $getInfo['transid'];
        $data = $website->converTimestamp($getInfo['data']);
        array_push($result, '
            <tr class="text-center">
                <td><a href="https://steamcommunity.com/profiles/'.$userid.'" target="_blank">'.$userid.'</a></td>
                <td>'.$nazwa.'</td>
                <td>'.$cena.'PLN</td>
                <td>'.$transid.'</td>
                <td>'.$data.'</td>
            </tr>                                           
        ');
    }
    return $result;
    }