Jump to content

Shopping Cart Part 6 - Function Not Looping Through Array


zacball

Recommended Posts

Hi and thank you in advance for reading this.

 

This function ->

 

function product_exists($productId){
   foreach (get_xml_catalog() as $product){
           if($product->id == $productId){
           return true;
       }
       return false;
   }
}

 

is killing me. It works fine for the first item in the $product array, but its not looping through the array. I know the get_xml_catalog() function is working because my catalog loads fine, and the id's for the ADD ITEM links are set proper. However when that productId is put into this function it will only return true if it matches the first id in the xml file, it doesnt appaear to go past that.

 

<?php 
   $productId = $_REQUEST['id'];

   if(product_exists($productId)){
       echo 'product exists';
   }else{
       echo 'product not found';
   }
?>

 

This is the section that sends the the data to the product_exists function.

Link to comment
Share on other sites

I added this to see what was in the array and its only showing the TOMATOSOUP

 

 

 

function product_exists(){
   foreach (get_xml_catalog() as $product){
        $product = $product->id;
        return $product;        
   }

}

echo product_exists();

 

The only product making it into the array is the first product. Why is this thing not looping?

Link to comment
Share on other sites

In regards to the code in post #1:

 

You'd need to compare this to the source code that was included with the video tutorial series... I'm guessing that you have the "return false" statement one line too high in the source code. It most likely needs to come after the closing "}" of the foreach loop, like this:

 

function product_exists($productId){
   foreach (get_xml_catalog() as $product){
           if($product->id == $productId){
           return true;
       }
   }
   return false;
}

 

This is in regards to the code you posted in post #2 of this topic:

 

The issue is that the loop only runs once because the first time the code loops, it immediately returns the $product variable.

 

Basically, what happens is this:

-- the foreach loop starts

-- $product is set

-- on the next line, $product is immediately returned, which causes PHP to jump out of the loop. Thus, it only loops once, and only the first product is captured.

 

My guess is that you probably need to do the "return $product" line after the closing "}" of the foreach loop:

 

foreach (get_xml_catalog() as $product){
        $product = $product->id;       
   }
return $product; 

 

I'm not completely sure on my comments on post #2 though -- I haven't personally done this particular course, so it's just an educated guess on my part.

Link to comment
Share on other sites

no you were right. man that's frustrating. Ive been fighting with this for hours now wondering why im the only one with this problem because no one else had complained, and was clearly convinced i was right. i was jumping out of the loop too soon. its pretty obvious now.

 

thanks

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...