-1

I can't figure out a solution for this so I'm asking here if someone can help me figure this out!

This is how I make loops now when I need a increasing number somewhere in the code. So I tell before how many times i want the loop to run, so in this example 1000 times.

$itemsqueue = 1000;
for ($qu = 1 ; $qu <= $itemsqueue; $qu++){

}

But sometimes I don't know how many lines the "csv" database is, it might be 10,000 lines or 5 lines. So then I would like to have something that makes the loop stop if something in the the actual loop is empty or not set.

Of course the example code below don't work, but this is kind of what I'm thinking I need.

if (empty($queue[$qu][1]) ){
  for ($qu = 1 ; $qu++){
  }
}

Hopefully I made some sense.

mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • 3
    Have a look at `foreach()` loops, or if reading a csv , have a look at other example code - https://stackoverflow.com/questions/2805427/how-to-extract-data-from-csv-file-in-php – Nigel Ren Aug 16 '18 at 06:48
  • 2
    Convert csv data into array & then use count() for condition – Ashu Aug 16 '18 at 06:49
  • 2
    Possible duplicate of [Can you 'exit' a loop in PHP?](https://stackoverflow.com/questions/588892/can-you-exit-a-loop-in-php) – DarkBee Aug 16 '18 at 06:49
  • Was hoping for a quick fix :) I guess your right its best to rewrite how i fetch the actual csv. As this is feach within the data that is already fetched from the actual csv data. Thanks Michel, again! – user1296114 Aug 16 '18 at 06:54
  • Well, a quickfix could be checking if the current value you would be trying to use is *set* or *not*, and then **break** the loop. But that's extra work that's unnecessary when using `foreach`. – Rafael Aug 16 '18 at 07:04

1 Answers1

1

Thanks Ash for getting me in the right direction. I added this to the code that reads the csv firstly!

$queuecount = count($data);

And now i know how many lines each CSV is!

So now i can just add that on the item queue!

for ($qu = 1 ; $qu <= $queuecount; $qu++){
}

Sweet!