简体   繁体   中英

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.

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.

Upon calling this function is only returning last 1 record. 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 :

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 :

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.

  • Second, you do not need another foreach loop. Just push the values inside the while and then finally in the end return that value container.

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

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,

you will need to make this an array also, push() in all elements like how you did in the while loop

and return the array

or just return the $atradinghours which is all the data anyway

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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