Jump to content

NULL value at end of for() loop


RyanMinor

Recommended Posts

I have a for loop that I am looping through. When I use var_dump() I get 10 results (which is what I should be getting). However, when I loop through it using a for() loop I am getting an extra 11th value that is blank. What would be causing this? My code is below:

 

$legacy = new Legacy();
	// Loop through each film in the shopping cart
	for ($i = 0; $i < $myShoppingCartInfo['row_count']; $i++) {
		// Determine if the film is of a legacy series
		if ($legacy->isLegacyCheckout($myShoppingCartInfo['data']['PRODUCTSKUID'][$i])) {
			// Declare array to hold all legacy film entity_id values
			$legacyEntityIds = array();
			$legacyEntityIds = $legacy->getLegacyEntityIds($myShoppingCartInfo['data']['PRODUCTSKUID'][$i]);
			var_dump($legacyEntityIds);
			// Loop through all legacy video entity_id's
			for ($i = 0; $i < $legacyEntityIds['data']['ENTITY_ID']; $i++) {
				// Grant streaming access to each entity id
				echo $legacyEntityIds['data']['ENTITY_ID'][$i] . '<br />';
				// Insert the product into the order items table
				/*$query = "INSERT INTO tblOrderItems (OrderItemID, ProductSKUID, RentalDate, OrderID) 
					VALUES (##newID##, 
						'".$myShoppingCartInfo['data']['PRODUCTSKUID'][$i]."', 
						".($myShoppingCartInfo['data']['RENTALDATE'][$i] === NULL ? 'NULL' : 
						"'".$myShoppingCartInfo['data']['RENTALDATE'][$i]."'").", ".$newOrderID.")";
				$cartItemInserted = counter_id_insert($query,'OrderItemID');
				$curr_user->addNewEntityLicense($legacyEntityIds['data']['ENTITY_ID'][$i]);*/
			}
		}

 

When I view the source code of the output page in a browser, this is what I see:

array(5) {

  ["row_count"]=>

  int(10)

  ["col_count"]=>

  int(1)

  ["col_names"]=>

  array(1) {

    [0]=>

    string(9) "entity_id"

  }

  ["col_types"]=>

  array(1) {

    [0]=>

    string(3) "int"

  }

  ["data"]=>

  array(1) {

    ["ENTITY_ID"]=>

    array(10) {

      [0]=>

      string(7) "1681799"

      [1]=>

      string(7) "1681872"

      [2]=>

      string(7) "1681871"

      [3]=>

      string(7) "1681870"

      [4]=>

      string(7) "1681869"

      [5]=>

      string(7) "1681868"

      [6]=>

      string(7) "1681867"

      [7]=>

      string(7) "1681866"

      [8]=>

      string(7) "1681865"

      [9]=>

      string(7) "1681864"

    }

  }

}

1681799<br />1681872<br />1681871<br />1681870<br />1681869<br />1681868<br />1681867<br />1681866<br />1681865<br />1681864<br /><br />

Link to comment
Share on other sites

All I see is an extra '<br>' tag at the end. Are you sure that tag isn't being echo'd to the page in some code after your for() loop?

 

You can easily verify if it is due to an empty/null value magically being produced in your array. Change the line that produces the output to this

echo "[{$legacyEntityIds['data']['ENTITY_ID'][$i]}]<br />\n";

 

If the problem is due to an empty item being added to the array, then your output will look like this (note the [] in the last line):

[1681799]<br />
[1681872]<br />
[1681871]<br />
[1681870]<br />
[1681869]<br />
[1681868]<br />
[1681867]<br />
[1681866]<br />
[1681865]<br />
[1681864]<br />
[]<br />

 

However, if that last BR tag is not generated in that loop then the last lines will look like this

[1681867]<br />
[1681866]<br />
[1681865]<br />
[1681864]<br />
<br />

Link to comment
Share on other sites

OK, I just took a closer inspection of your code and saw this

				var_dump($legacyEntityIds);
			// Loop through all legacy video entity_id's
			for ($i = 0; $i < $legacyEntityIds['data']['ENTITY_ID']; $i++) {

You have a for() loop that continues while $i < $legacyEntityIds['data']['ENTITY_ID'] but $legacyEntityIds['data']['ENTITY_ID'] is an array - it is not the length of the array. Beside, you should NOT (normally) be using a for() loop with a dynamic variable for the index to process an array. You should be using a foreach loop. Also, running queries in loops is very poor implementation.

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.