Looping through array of different lengths in PHP - php

I have a order form with inputs like this:
<input type="text" name="booking[Sandwich][Roastbeef][qty]" />
<input type="text" name="booking[Sandwich][Cheese][qty]" />
<input type="text" name="booking[Pizza][Classic][qty]" />
<input type="text" name="booking[Pizza][Special][qty]" />
<input type="text" name="booking[Coffee][qty]" />
I'm having trouble looping through the arrays correctly.
Here is what kind of output I would like to have:
<h2>Sandwich</h2>
<p><strong>Roastbeef:</strong> 10</p>
<p><strong>Cheese:</strong> 5</p>
<hr>
<h2>Coffee</h2>
<p><strong>Quantity:</strong> 15</p>
If all the pizza input is empty, the heading "Pizza" should not be printed! The same with the "coffee" or "Sandwich" group. If the order does not contain anything in the group, the heading should not be printed.
I can't write specific tests for every input, as I have 200 of them.
Here is what I have tried to do:
$booking = $_POST['booking'];
//First check if there is one or more input that is not empty
if (!empty($booking)) {
foreach ($booking as $type => $items) {
if (count(array_filter($items))) {
$order .= "<hr>\n<h2>" . ucfirst($type) . ":</h2>\n";
}
foreach ($items as $name => $qty) {
if ($qty > "0"){
$order .= "<p><strong>" . ucfirst($name) . ":</strong> " . $qty . "</p>\n";
}
}
}
}
This code only works when the array has a length of two keys. I can't seem to wrap my brain around as how to deal with other lengths. Any help would be great!
Edit:
With the answer from #treegarden, I have almost what I need. Now I just need to have some sort of check if the "group" is empty, then the <h2> should not be printed. if (count(array_filter($entry))) works in not printing any thing if the group is empty, but just for those input that only have two keys.
if (!empty($booking)) {
foreach($booking as $key=>$entry) {
if (count(array_filter($entry))) {
echo "<h2>$key</h2>"; //Should only be printed if one or more inputs in the group are not empty
foreach($entry as $key=>$subEntry) {
if(is_array($subEntry) && $subEntry['qty'] > 0) {
echo "<p><strong>$key:</strong>" . $subEntry['qty'] . "</p>";
} elseif(!is_array($subEntry) && $subEntry > 0) {
echo "<p><strong>Quantity:</strong> $subEntry</p>";
}
}
echo '<hr/>';
}
}
}

Maybe try recursion, from example snippet:
<?php
class RecursiveArrayOnlyIterator extends RecursiveArrayIterator {
public function hasChildren() {
return is_array($this->current());
}
}
?>
else a simple forward way is to assume you have three or more nested loops,
keep checking is $value in $kv using is_array(), which is done by invoking a function.

Try this
$booking = $_POST['booking'];
if (!empty($booking)) {
foreach ($booking as $type => $items) {
if (count(array_filter($items))) {
$order .= "<hr>\n<h2>" . ucfirst($type) . ":</h2>\n";
}
foreach ($items as $name => $qty) {
if (is_array($qty)) {
foreach ($qty as $qt) {
if ($qty > "0"){
$order .= "<p><strong>" . ucfirst($name) . ":</strong> " . $qt. "</p>\n";
}
}
} else {
if ($qty > "0"){
$order .= "<p><strong>" . ucfirst($name) . ":</strong> " . $qty . "</p>\n";
}
}
}
}
}

Related

Unable to use an array as an array?

I am in the process of building a software download site for my company.
However, I have come across a problem that I am unsure how to get past.
I am retrieving all the information from a table about a particular software release and placing them in a multidimensional array. I am then trying to run a foreach (I have even tried a for loop) against that array and I get an error shown below:
When I run var dump against the original array, I get this:
So I am really confused as I don't know what I'm missing or where I am going wrong. The reason why I want to run this is so that I can filter the array into a one dimensional array.
Below is the code for the main web page
<?php
//Displays list of companies in 2 columns
$versionAccess = VersionAccess::findAccess($relId);//This gets set earlier in the webpage
$count = count($versionAccess);
var_dump($versionAccess);
foreach ($versionAccess as $va)
{
if ($va->company_access != '0')
{
$versionAcc[] = $va->comapny_id;
}
}
foreach ($company as $compAccess)
{
$compAccessId = $compAccess->company_id;
if (in_array($compAccessId, $versionAcc))
{
$access = 'checked disabled';
}
else { $access = 'disabled'; }
$accessName = 'access'.$compAccessId;
if ($ctr % 2 == 0)
{
echo '<td>'.$compAccess->company_name.':</td>';
echo '<td><label class="switch"><input type="checkbox" name="'.$accessName.'" value="1" '.$access.'><span class="slider round"></span></label></td>';
echo '</tr>';
}
else
{
if ($ctr < $compCount)
{
echo '<tr>';
echo '<td>'.$compAccess->company_name.':</td>';
echo '<td><label class="switch"><input type="checkbox" name="'.$accessName.'" value="1" '.$access.'><span class="slider round"></span></label></td>';
}
else
{
echo '<tr>';
echo '<td>'.$compAccess->company_name.':</td>';
echo '<td><label class="switch"><input type="checkbox" name="'.$accessName.'" value="1" '.$access.'><span class="slider round"></span></label></td>';
echo '</tr>';
}
}
$ctr++;
}
?>
The function that brings the data from the database is:
public static function findAccess($accessId)
//find the version access in database
{
return self::findQuery("SELECT * FROM version_access WHERE version_id = '$accessId'");
}
The findQuery method:
public static function findQuery($sql)
{
global $database;
$resultSet = $database->query($sql);
$objectArray = array();
while ($row = mysqli_fetch_array($resultSet))
{
$objectArray[] = self::instant($row);
}
return $objectArray;
}
I am still relatively new and any help is greatly appreciated.

Trying to change statement so create_radio function is only called once (PHP)

I have a function that creates a radio button in PHP:
// This function creates a radio button.
// The function takes two arguments: the value and the name.
// The function also makes the button "sticky".
function create_radio($value, $name = 'gallon_price')
{
// Start the element:
echo '<input type="radio" name="' .
$name .'" value="' . $value . '"';
// Check for stickiness:
if (isset($_POST[$name]) && ($_POST[$name] == $value))
{
echo ' checked="checked"';
}
// Complete the element:
echo " /> $value ";
} // End of create_radio() function.
I then leave the PHP form to create an html form and call the function three times with values that represent three different gas prices.
<span class="input">
<?php
create_radio('3.00');
create_radio('3.50');
create_radio('4.00');
?>
</span>
I am wondering how I could change this code so it would be possible to get the same output and only make one call to the create_radio function.
Thanks!
function create_radio($value, $name = 'gallon_price')
{
$output = "";
if (is_array($value)) {
while (count($value) > 0) {
$arr_value = array_pop($value);
$output .= create_radio($arr_value);
}
} else {
// Start the element:
$output .= '<input type="radio" name="' .
$name .'" value="' . $value . '"';
// Check for stickiness:
if (isset($_POST[$name]) && ($_POST[$name] == $value))
{
$output .= ' checked="checked"';
}
// Complete the element:
$output .= " /> $value ";
}
return $output;
}
A quick bit of recursion will allow the function to work for arrays and non arrays. Note: in the html you will need to echo the call to create_radio not just call it.
you could make $value an array create_radio(array('3.00','3.50','4.00')); just loop inside the function:
function create_radio($value,$name = 'gallon_price'){
foreach($value as $v){
// Start the element:
$out. = '<input type="radio" name="'.$name.'" value="'.$v.'"';
// Check for stickiness:
if(isset($_POST[$name])&&($_POST[$name]==$v)){
$out .= ' checked="checked"';
}
// Complete the element:
$out .= " /> $v ";
}
return $out;
} // End of create_radio() function.
call it:
echo create_radio(array('3.00','3.50','4.00'));
it is usually better not to echo inside the function.

Foreach loop echo

The last foreach loop doesn't seem to echo the list I want, it doesn't print anything. How can I fix this? I know it's getting quite complicated but any help would be appreciated.
$allTroopsList = array();
$allMissionsList = array();
while ($mytroops = $alltroops->fetch_assoc())
{
$allTroopsList []= $mytroops;
}
while ($mymissions = $allmissions->fetch_assoc())
{
$allMissionsList []= $mymissions;
}
while($userintroop = $allUsersintroops->fetch_assoc())
{
if($userintroop['userid'] == $_SESSION['userid'])
{
echo "<ul class='troop'>";
echo "<li>" . $userintroop['troopid']. " </li>";
foreach($allTroopsList as $mytroops)
{
if($userintroop['troopid'] == $mytroops['troopid'])
{
echo "<li> Troop description: " . $mytroops['description']. " </li>";
foreach($allMissionsList as $mymissions)
{
if($mytroops['missionid'] == $mymissions['missionid'])
{
echo "<li> Missionname: " . $mymissions['missionname']. " </li>";
echo "<br/>";
echo "</ul>";
}
}
}
}
}
}
foreach($allUsers as $myotherusers)
{
if($userintroop['userid'] == $myotherusers['userid'])
{
echo "<li> other users: " . $myotherusers['username']. " </li>";
echo "<br/>";
}
}
This line:
while($userintroop = $allUsersintroops->fetch_assoc())
is fetching results from a database, right? That'd mean at some point you run out of rows to fetch, and $userintroop will become a boolean FALSE.
Then later on you try to do
if($userintroop['userid'] == $myotherusers['userid'])
which is wrong on two levels - $userintroop will NOT be an array at this point, so it could never have a ['userid'] parameter. And since it's the result of a failed fetch operation, never could have any value OTHER than a FALSE.
So that last loop does execute, but will never produce anything since the conditions for producing output are literally impossible to meet.

Bad array? Bad foreach?

Sorry for the huge ignorance on the topic, but I really have no idea where to look other than this website when I come into trouble with my PHP.
What I'm trying to do here is use pre-designated IDs to call particular movies from a database. But all I get is an 'Invalid argument supplied for foreach()' message on the second and third foreach's below.
Here's my code in the head:
//Custom lists of movies to bring in
//New Releases list
$films_new_releases = array(40805, 46705, 41630, 44564, 39451, 20352, 43933, 49009, 49797, 42194);
//Most Popular list
$films_most_popular = array(27205, 16290, 10138, 41733, 37799, 18785, 19995, 17654, 10140, 12162);
//Get information from address bar
$list = $_GET['l'];
if ($list == 'new releases') {
$list_chosen = $films_new_releases;
}
elseif ($list == 'most popular') {
$list_chosen = $films_most_popular;
}
else {
$list_chosen = $films_new_releases;
}
And in amongst the body:
// Loop through each film returned
foreach ($list_chosen as $list_chosen_film) {
$films_result = $tmdb->getMovie($list_chosen_film);
$film = json_decode($films_result);
// Set default poster image to use if film doesn't have one
$backdrop_url = 'images/placeholder-film.gif';
// Loop through each poster for current film
foreach($film->backdrops as $backdrop) {
if ($backdrop->image->size == 'poster') {
$backdrop_url = $backdrop->image->url;
}
}
echo '<div class="view-films-film">
<img src="' . $backdrop_url . '" alt="' . $film->name . '" />
<div class="view-films-film-snippet">
<h2>' . $film->name . '</h2>';
if ($film->certification != null) {
echo '<img src="images/bbfc-' . strtolower($film->certification) . '.png" alt="" />';
}
echo ' <h3>Starring</h3>
<p>';
$num_actors = 0;
foreach ($film->cast as $cast) {
if ($cast->job == 'Actor') {
echo '' . $cast->name . ' ';
$num_actors++;
if ($num_actors == 5)
break;
}
echo ' </p>
<h3>Director</h3>
<p>';
foreach ($film->cast as $cast) {
if ($cast->job == 'Director') {
echo '' . $cast->name . ' ';
}
}
echo ' </p>
</div>
</div>';
}
// End films
}
The little testing I've done is checking what $list_chosen, $list_chosen_film, $films_result and $film actually contain by printing them at the bottom of the page.
$list_chosen shows - Array, $list_chosen_film shows - 42194, $films_result shows the entire JSON string, $film shows - Array.
Try adding:
print_r($film->backdrop);
before the second foreach() loop. Before the error message it won't be an array or it will contain zero elements (not allowed). If you also add:
echo $films_result;
you will be able to debug it and fully understand what is wrong. If not, post the whole output in your question.
This happens, because - as error displayed by PHP informed you - you have provided wrong parameter to foreach loop, probably null or some other value. Make sure you are providing array to foreach.
Also, every time you use foreach, do it like that:
if (count($some_list) > 0) {
foreach ($some_list as $list_item) {
// code for each item on the list
}
} else {
// code when there is nothing on the list
}
This will ensure you will not see errors just because there is nothing on the list.
EDIT:
On the documentation page you can find some tip how to avoid such errors if the collection you are trying to iterate through is empty. Just cast the collection to array type:
foreach ((array) $some_list as $list_item) {
// code for each item on the list
}
Can you provide a dump of $film? The error is telling you that you are pointing to an object that cannot be iterated through (most likely null).

Nested foreach statements are invalid. Help?

Please forgive the extreme-beginner style of coding. I'm working with PHP and JSON strings from an API for the first time.
What I'm trying to do here, which obviously doesn't work if you look through it, is print out multiple movie results that the user is searching for. This is a search results page - with a movie poster and some key details next to each item.
One of the things I want next to each result is a list of the first 5 actors listed in the movie, as "Starring" and then any directors listed in the movie as "Director".
The problem is no doubt the fact that I've nested foreach statements. But I don't know any other way of doing this.
Please please the simplest answer would be the best for me. I don't mind if it's bad practice, just whatever solves it quickest would be perfect!
Here's the code:
// Check if there were any results
if ($films_result == '["Nothing found."]' || $films_result == null) {
}
else {
// Loop through each film returned
foreach ($films as $film) {
// Set default poster image to use if film doesn't have one
$backdrop_url = 'images/placeholder-film.gif';
// Loop through each poster for current film
foreach($film->backdrops as $backdrop) {
if ($backdrop->image->size == 'thumb') {
$backdrop_url = $backdrop->image->url;
}
}
echo '<div class="view-films-film">
<img src="' . $backdrop_url . '" alt="' . strtolower($film->name) . '" />
<div class="view-films-film-snippet">
<h2>' . strtolower($film->name) . '</h2>
<img src="images/bbfc-' . strtolower($film->certification) . '.png" alt="" />
<h3>Starring</h3>
<p>';
$num_actors = 0;
foreach ($films[0]->cast as $cast) {
if ($cast->job == 'Actor') {
echo '' . $cast->name . ' ';
$num_actors++;
if ($num_actors == 5)
break;
}
}
echo ' </p>
<h3>Director</h3>
<p>';
foreach ($films[0]->cast as $cast) {
if ($cast->job == 'Director') {
echo '' . $cast->name . ' ';
}
}
echo ' </p>
</div>
</div>';
// End films
}
}
An example of the result would be this:
with line 120 being foreach ($films[0]->cast as $cast) { and line 131 being foreach ($films[0]->cast as $cast) {
Why are you using $films[0] when you're in a foreach ($films as $film)? You should be using $film->cast right? And you should dump $film at the start of your loop with var_dump and inspect it - make sure $cast is an array and is populated. If it's not, work back from there.

Categories