简体   繁体   English

PHP打印使用While循环和数组返回的所有值

[英]PHP print all values returned using While Loop and Arrays

I am having problem with the following code which is returning only last 1 record. 我遇到以下代码的问题,它只返回最后1条记录。

Using while loop I am able to retrieve all the values and add it to arrays and get it over for each loop but I am unable to return all the values. 使用while循环我能够检索所有值并将其添加到数组并为每个循环获取它但我无法返回所有值。

Upon calling this function is only returning last 1 record. 在调用此函数时,仅返回最后1条记录。 Can anyone please help me fix this code in a way that it will return all the values. 任何人都可以帮助我修复此代码,它将返回所有值。 Thanks 谢谢

echo gettradinghours("54");
function gettradinghours($storeid){

  $select_tradinghours = mysqli_query("SELECT * FROM `tradinghours` where `storeid`='54' ORDER BY FIELD(openday, 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY');");

 $atradinghours = array();
 while($fetch_tradinghours = mysqli_fetch_array($select_tradinghours)){
     array_push($atradinghours, $fetch_tradinghours['openday']. ' ' .$fetch_tradinghours['starttime']. ' - ' .$fetch_tradinghours['endtime']);
  }
  foreach($atradinghours as $atradinghoursr){
       $getval = $atradinghoursr;
  }

  return $getval;
}

( 1 ) Return Array : (1)返回数组:

function gettradinghours($storeid){
                $getval = array();
                $select_tradinghours = mysqli_query("SELECT * FROM `tradinghours` where `storeid`='54' ORDER BY FIELD(openday, 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY');");

               $atradinghours = array();
               while($fetch_tradinghours = mysqli_fetch_array($select_tradinghours)){
                     array_push($atradinghours, $fetch_tradinghours['openday']. ' ' .$fetch_tradinghours['starttime']. ' - ' .$fetch_tradinghours['endtime']);
                }
                foreach($atradinghours as $atradinghoursr){
                     $getval[] = $atradinghoursr;
                }
       return $getval;
    }

( 2 ) Return String : (2)返回字符串:

function gettradinghours($storeid){
                $getval = '';
                $select_tradinghours = mysqli_query("SELECT * FROM `tradinghours` where `storeid`='54' ORDER BY FIELD(openday, 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY');");

               $atradinghours = array();
               while($fetch_tradinghours = mysqli_fetch_array($select_tradinghours)){
                      $getval.= $fetch_tradinghours['openday']. ' ' .$fetch_tradinghours['starttime']. ' - ' .$fetch_tradinghours['endtime'];
                }

       return $getval;
    }
  • First thing to point out is that mysqli_query() 's first argument is you need to feed it with a mysqli connection. 首先要指出的是mysqli_query()的第一个参数是你需要用mysqli连接来提供它。

  • Second, you do not need another foreach loop. 其次,你不需要另一个foreach循环。 Just push the values inside the while and then finally in the end return that value container. 只需按下while内的值,然后最后返回该值容器。

function gettradinghours($storeid){
    // 1st point! connection!
    $connection = mysqli_connect('localhost', 'username', 'password', 'database_base');
    $select_tradinghours = mysqli_query($connection, "SELECT * FROM `tradinghours` WHERE `storeid`='54' ORDER BY FIELD(openday, 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY')");

    $atradinghours = array();
    // no need for that array push thing, just push it normally and return in the end
    while($fetch_tradinghours = mysqli_fetch_array($select_tradinghours)){
         $atradinghours[] = $fetch_tradinghours['openday']. ' ' .$fetch_tradinghours['starttime']. ' - ' .$fetch_tradinghours['endtime'];
    }
    return $atradinghours; // return the gathered/pushed values
}

print_r(gettradinghours("54")); // use the function
  • Lastly your parameter that you feed inside your function is not used. 最后,不使用您在函数内输入的参数。 And use prepared statements instead. 而是使用预备语句。
function gettradinghours($storeid){

    // connection!
    $connection = mysqli_connect('localhost', 'username', 'password', 'database_base');
    // use prepared statements!
    $stmt = $connection->prepare("
        SELECT openday, starttime, endtime FROM `tradinghours` WHERE`storeid` = ? 
        ORDER BY FIELD(openday, 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY')
    ");
    $stmt->bind_param('i', $storeid); // bind the paramter input!
    $stmt->execute();
    $stmt->bind_result($openday, $starttime, $endtime);

    $atradinghours = array();
    while($stmt->fetch()) { // as usual push the values
        $atradinghours[] = $openday. ' ' .$starttime. ' - ' .$endtime;
    }

    return $atradinghours; // return the gathered values
}

print_r(gettradinghours(54));

Sidenote: 边注:

If you do not want such result (an array) you could build a string instead: 如果您不想要这样的结果(数组),您可以构建一个字符串:

$atradinghours = '';
while($stmt->fetch()) { // as usual push the values
    $atradinghours .=  $openday. ' ' .$starttime. ' - ' .$endtime . '<br/>';
}

Then in the end, you could now echo properly a string: 然后最后,您现在可以正确回显一个字符串:

echo gettradinghours(54);

Why again an foreach 为什么再一个foreach

function gettradinghours($storeid){
            $select_tradinghours = mysqli_query("SELECT * FROM `tradinghours` where `storeid`='54' ORDER BY FIELD(openday, 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY');");

           $atradinghours = array();
           while($fetch_tradinghours = mysqli_fetch_array($select_tradinghours)){
                 array_push($atradinghours, $fetch_tradinghours['openday']. ' ' .$fetch_tradinghours['starttime']. ' - ' .$fetch_tradinghours['endtime']);
            }

   return $atradinghours;
}

You are looping through the array and returning the last array element, 您循环遍历数组并返回最后一个数组元素,

Where as you want whole array. 在哪里你想要整个阵列。

foreach($atradinghours as $atradinghoursr){
                 $getval = $atradinghoursr;
            }

Should only be 应该只是

$getval = $atradinghours;
return $atradinghours;

the problem is in $getval you are just storing the last element, 问题出在$getval你只是存储最后一个元素,

you will need to make this an array also, push() in all elements like how you did in the while loop 你需要把它作为一个数组, push()在所有元素中,就像你在while循环中所做的那样

and return the array 并返回数组

or just return the $atradinghours which is all the data anyway 或者只返回$atradinghours ,无论如何都是数据

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

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