looping foreach for certain steps(every request user sends) - php

I want to write a foreach that loops on a wanted certain step so for example if a user send me 2 rooms it just loops twice if the user sends 5 room it loops 5 times and here is what I tried:
$room_count = [1,2,3] //room ids for example
foreach($room_count as $room_counts){
echo 'echo on eachstep'
}
I want to get the requests from $request->all() so I am not sure about saving that in array is a right thing to use for example here or not so I just need to do it in an array for now.

Try this:
for ($i = 0; $i < count($room_count); $i++) {
//your loop body here
}
if you want to loop for certain amount of rooms.
You can't
get the requests from $request->all()
, $request->all() contains only Request parameters like user's input.

Related

Taking user input and inserting it into an associative array using PHP

I need to put user input into an associative array. The program asks the user how many people are in their group. Then it asks each person's name along with their order. It then proceeds to print out the group member's names along with their orders. My idea was to use a for loop to add each data set into the array. I think I have it all figured out except how to put the data into the array. Here is my code so far:
<?php
$people=readline("How many orders?");
$orders=[];
for($i=0; $i<$people; $i++)
{
print("Order ");
print($i);
print(" name: ");
$name=readline();
print("Order ");
print($i);
print(" order: ");
$food=readline();
}
print("Total order:\n");
foreach($orders as $names=>$orders)
{
print($names);
print(": ");
print($orders);
print("\n");
}
?>
PHP has some of the best documentation of any programming language, so I would advise taking a peak at the array intro, which will show you exactly what you need to know.
As a tip, because you're going to be keeping track of multiple datums per order (name, food), and you will want to be able to associate these with each other later, you will be adding arrays to your $orders array. For example:
$orders[$i] = [$name, $food];
This can be accessed later using numeric indexing:
$orders[$i][0] // value of $name for order $i
$orders[$i][1] // value of $food for order $i
But, in this case, I would actually advise taking the next step and using associative arrays instead:
$orders[$i] = ['name' => $name, 'food' => $food]
This makes access much easier to remember and read:
$orders[$i]['name'] // value of $name for order $i
$orders[$i]['food'] // value of $food for order $i

Coontaining Nested Loop

I am using WordPress and attempting to nest loops. In the parent loop I want to display regular post and every 3rd post inject a post from the inner loop. The problem is as long as the parent loop has posts then the child loop will spit out its post again which is causing duplicates. Is there a way to only display one post of the child loop at a time and to only show post while it has_posts?
Easy solution, don't nest loops. Make two different queries and a counter, loop the first query and when i%3==0 add one from second query. See if this helps:
$apples = get_posts('post_type=apple');
$oranges = get_posts('post_type=orange');
for ($i=0; i<count($apples); $i++) {
$apple = $apples[$i];
// do something with $apple
// every 3rd apple
if ($i%3 === 0) {
$orange = array_shift($oranges);
// do something with $orange
}
}
At some point you'd have to check if there are enough oranges or not enough apples to print all oranges.
Check docs on get_posts for more info on how to use it.

How do I split a cookie using php

I have a cookie which I'm trying to split. The cookie is in this format:
key = val1,val2,val3 (where each value is separated by commas)
is there a way for me to split this in a loop so that I can directly access val3?
I've tried using the explode() function with no success.
for ($i = 0; $i < count($_COOKIE); $i++)
{
$ind = key($_COOKIE);
$data = $_COOKIE[$ind];
//I try and slit the cookie here
$cookie_temp = explode(",",$_COOKIE[$ind]);
//Here is where I wanted to display Val3 from the cookie
print $cookie_temp[2];
next($_COOKIE);
}
my code works fine but I then end up with all my Val3 in a large array. For example, my val3's are numbers and they get put in an array. Can I split this even further?
First of all, I'm hoping you know the name of the cookie you're trying to get the value of. Let's call it mycookie in the rest of my answer.
Second, just scrap the whole loop thing and just access $_COOKIE['mycookie'] directly.
Then, you can now call explode(",",$_COOKIE['mycookie']) to get the separate values.
Next, just get index 2 with [2] as you are in your current code.
As a shortcut, if the second one is the only one you need:
list(,,$val) = explode(",",$_COOKIE['mycookie']);
Assuming you are looping because you have multiple comma-separated cookie key/value groups, use a foreach() instead and with list() you can retrieve the third value with a direct assignment.
foreach ($_COOKIE as $key=>$value) {
list($v1, $v2, $v3) = explode("," $value);
echo $v3;
}
If you have only one cookie value to access, there is no need for the loop, and you can directly call explode(",", $_COOKIE['key'])
PHP 5.4 allows array dereferencing, whereby you can directly access the array element off of the explode() call, but this won't work in earlier PHP versions.
echo explode(",", $_COOKIE['key'])[2];

Getting next item in an array of values

I'm trying to get every output out of a json file. So far I have:
$json_feed = file_get_contents($indy_feed_url);
$json_items = json_decode($json_feed, TRUE);
$individual = $json_items['indy'][0];
and then echo things out.
This works fine for item 0. However, I want to do all items.
The number of items in the json file varies from time to time so I cannot just enter like 10 here because sometimes there might be 20 or 5 etc.
How can I do this in a loop so that it counts the number of items and loops that many times?
Use the foreach statement:
foreach($json_items['indy'] as $item) {
// do whatever you want with $item
}

$_POST multidemensional array looping

So I've created a form wich, by the post function, send me several values.
$_POST[groups] sends me, using 4 different checkboxes values 1, 2, 4 and 8.
$_POST[retailgroup] does the same, but only the values 1, 2 and 4.
The problem is that I want these to add up together and then send the sum of the values to a MySQL table.
I've managed to do one using the following foreach loop:
foreach ( $_POST as $val ) {
foreach ( $val as $key=>$final_val ) {
$group = $group+$final_val;
}
}
The problem starts when I need to have these two apart from each other since they are going into separate columns in the table.
To be clear I've assigned different groups different values (always taking the prev value times two, just like counting binary) like 1, 2, 4 and 8. By adding them together I can determine membership in the groups by doing a subtraction "backwards". Since there are different kinds of groups I want them into two separate fields of the table.
I hope everything is clear.
Any idea on how to do this?
you want to use:
$groups_sum = array_sum($_POST['groups']);
$rgroups_sum = array_sum($_POST['retailgroups']);
This will be faster and clearer than a foreach loop in this case.
try
foreach ( $_POST[groups] as $key=>$final_val ) {
//do stuff for first group
}
foreach ( $_POST[retailgroup] as $key=>$final_val ) {
//do stuff for second group
}
and after you can make whatever you want with the variables got from loops
$groups = array_sum($_POST['groups']);
$retailGroups = array_sum($_POST['retailgroup']);
You can check membership like this instead of subtracting backwards:
$groupAdmin = 4;
if($groups & $groupAdmin) {
// is admin
}

Categories