0

I'm still very new to PHP, so I was wondering if I could get some help? I'm supposed to create a chart by iterating through an associative array using a while loop with a foreach loop inside it. But I keep getting an infinite loop and I'm not entirely sure how to fix it. I also suspect that the if statement conditions aren't helping, but I don't know either. What am I missing and is there a better way to go about this?

Here's the code in question:

<?php
 include 'davesinventory.php';
 $delimiter = " \n";
 $inventory['name'] = rtrim(strtok($data, $delimiter));
 $inventory['year'] = rtrim(strtok($delimiter));
 $inventory['serial'] = rtrim(strtok($delimiter));
 $inventory['seats'] = rtrim(strtok($delimiter));
 $inventory['charge'] = rtrim(strtok($delimiter));
 $inventory['days'] = rtrim(strtok($delimiter));
 $inventory['rev'] = rtrim(strtok($delimiter));
 $inventory['orig'] = rtrim(strtok($delimiter));
 $inventory['miles'] = rtrim(strtok($delimiter));
 $inventory['deprec'] = rtrim(strtok($delimiter));
 $inventory['freq'] = rtrim(strtok($delimiter));

 printf("<b> VEHICLE\tYEAR\t SERIAL#\t RENT COST\t DAYS OUT\t REVENUE\t ORIG. PRICE\t MILEAGE\t NOTES\n</b>");
?>
<hr>
<?php
 while($inventory['name']){
   foreach($inventory as $key => $vehicles){
         if ($key !== ['seats'] || ['deprec'] || ['freq'])
          {
             print $vehicles;
          }
          else if($key == ['seats'] || ['deprec'] || ['freq'])
          {
            echo " ";
          }
   }
}
?>
<pre>

Here is the file that the 'include' calls for:

<?php
$data =
"DodgeAvenger 2006 DA111-9 4 35.50 105 3727.50 21297.00 8795 .20 Monthly
Olds_Alero 2004 OA340-1 5 29.95 126 3773.70 23335.00 36010 .20 Monthly
Chry_PT_Crsr 2003 CPTC-MW2 4 37.95 26 986.70 15405.00 29020 .20 Weekly
Cadillac_Limo 1999 1999-01 18 142.50 4 570.00 38900.00 187419 .10 Weekly
Chev_M_Carlo 1997 CMC-21 6 27.30 55 1501.50 19437.50 113689 .20 Monthly
Chev_Suburban 1997 CSB-011 8 42.75 17 726.75 29999.00 137560 .20 Monthly
VW_Bus_T2A 1977 VWB-09 32 15.00 16 240.00 12000.00 397800 NA Daily
Ford_Stn_Wgn 1976 FSW-67 8 10.95 6 65.70 9899.00 149379 NA Whenever
Toy_Forklift 1997 6FGCU-45 1 61.25 65 3981.25 8795 732 .10 Yearly
Cat_Dozer(D7H) 1989 1989-11 1 98.00 5 490.00 67850.00 1304 .10 Yearly
";
?>
4
  • What is the purpose of while loop? Commented Sep 19, 2014 at 3:38
  • 2
    if ($key !== ['seats'] || ['deprec'] || ['freq']) should be if ($key !== ['seats'] || $key !== ['deprec'] || $key !== ['freq']) or something to this effect depending on what you are really wanting Commented Sep 19, 2014 at 3:40
  • Your loop will always. run as it gonna find condtion to be true. while($condtion) run the code. means run this untill the condtion is true if the condition is false always. Than it gonna run always. Commented Sep 19, 2014 at 3:40
  • The while loop was a requirement, so I had to find a way to utilize both it and the foreach loop. Commented Sep 19, 2014 at 3:45

2 Answers 2

2

No need to use while loop here. Foreach is enough

<?php    
    if(isset($inventory['name'])){
        foreach($inventory as $key => $vehicles){
            if ($key !== 'seats' || $key !=='deprec' || $key !=='freq')
            {
               print $vehicles;
            }
            else{
               echo " ";
            }
        }
    }
?>
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for taking the time to answer this!
It gives me the first result of the array, but I need the rest of it for it to really work. I think this where the while loop comes in, but I'm unsure how to go about it without getting the infinite loop. Would it help if I added the external php file that the include command calls for?
then please provide the complete array structure. I think i can help you then:)
The reason here for the infinite loop is while. Because while breaks the loop only when the condition used inside is wrong/false. Here you are checking the whether the $inventory['name'] is set or not. Its always set since it has a value. So if you have a parent array for $inventory use another foreach outside the current foreach loop.
1

Blank's answer should work for you. Mine should actually be a comment but it's much easier to format code in an answer.

If using while is just an annoying requirement for a homework or test (according to your comment...) you can simply work around it:

$finished = false;

while (!$finished) 
{
    //do all the work here - foreach loop and so on

    $finished = true;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.