F
F
feniksdv2018-03-28 17:51:09
PHP
feniksdv, 2018-03-28 17:51:09

How many variables can be combined into one (for loop)?

There is a cycle

for ($I =0; $I<1000; $I++) {
    isset($_GET['idoffers'.$I])?($_GET['idoffers'.$I]):'';
}

After it we get idoffers1..idoffers1000
now I need to do a type check:
if(!empty$_GET['idoffers5'] ||....$_GET['idoffers1000'])) {
....
}

What is not good, how to get by with one change
is also nonsense:
$test= $_GET['idoffers5']..$_GET['idoffers1000']
if(!empty($test)) {
....
}

there must be some simple way, but I don’t know it, tell me, and if you write code, then write it without abbreviations.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
P
Pavel Novikov, 2018-03-28
@feniksdv

what does single variable mean? read how the for loop works. Firstly, the for ($I =0; $I<1000; $I++) construct will return $i in the range from 0 to 999. Secondly, just check each variable for isset or empty and in case the condition is not met , go further through the loop thanks to the continue statement;
In reality it will look something like this

for ($i =0; $i<1000; $i++) {
    if (empty($_GET['idoffers'.$i])) {
       continue;
    }
   //ваша логика
}

T
Talyan, 2018-03-28
@flapflapjack

<form action=2.php>
<input name="idoffers[]">
<input name="idoffers[]">
<input name="idoffers[]">
<input name="idoffers[]">
<input name="idoffers[]">
</form>

2.php :
if(!empty($_GET['idoffers']))
{
foreach($_GET['idoffers'] as $key => $value)
{
print "$id = $value <br>";
}
}

L
leni_m, 2018-03-28
@leni_m

There is a
for ($I =0; $I<1000; $I++) {
isset($_GET['idoffers'.$I])?($_GET['idoffers'.$I]):'';
}
After it we get idoffers1..idoffers1000

How? I can't understand how you get $idoffers... variables with such a loop?
now I need to do a type check:
if(!empty($idoffers1.$idoffers1000)) {
....
}

"." - gluing lines. I don't understand what you want here either.
So you can also write without abbreviations? And then this piece
$test= idoffers1..idoffers1000
if(!empty($test)) {
....
}
in general, some kind of masochism.

M
Maxim Timofeev, 2018-03-28
@webinar

You already have them in one, in $_GET, which means:
But I would send these variables as nested $_GET['idoffers'][1], $_GET['idoffers'][2], etc. , then everything is simple and besides your idoffers in get there can be anything else, and you check only them.
Why do you need a cycle here at all

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question