简体   繁体   English

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

[英]How do i return all elements in while loop?

I have a function which grabs logs from database, but whenever im trying to echo this. 我有一个功能,可以从数据库中获取日志,但是每当我试图回显此信息时,都可以。 I am only getting first item. 我只得到第一项。

        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;
        }

Add

$result = "";

At the top of your function to define it (before the while loop), then change your 在函数的顶部进行定义(在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> ';

To

$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> ';

Note the dot next to equals, this will add to and not overwrite your variable. 请注意,等号旁的点将添加到您的变量中,而不会覆盖您的变量。

You have a few options. 您有几种选择。
Echo them in the loop (that does not "return" them) so that really doesn't answer your question, but since it's a table you build, at some point I assume it has to be echoed. 在循环中回显它们(不会“返回”它们),这样实际上并不能回答您的问题,但是由于它是您构建的表,因此在某些时候我认为必须将其回显。

   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>                                           
        ';
    }
    }

Or you can build an array with the results: 或者,您可以使用结果构建一个数组:

   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));

Or you can build a string with all the results: 或者,您可以使用所有结果构建一个字符串:

    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;
    }

These variables which you gave the database row's results isn't useful or you cannot benifit them because all of them are local variables or they're deleted and recreated after loop repeated because you created them in the loop, you should create them outside the loop, (eg) i think you just need the result so i just add the result outside the loop:- 您赋予数据库行结果的这些变量无用,或者不能使它们受益,因为它们都是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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM