Undefined index product_id

I have a very simple listing for motor parts

 <ol class="product_<?=$display?>">
	<?php foreach ( $producten as $product ):
		$photo_url = '/images/product_fotos/';
		if (is_null($product['foto'])){$photo_url .= 'no_pic.jpg';}else{url .= $product['foto'];}?>
  		<li>
        	<form action="" method="post" name="product-form" id="product-form" class="product-form">
         	<input name="categorie" type="hidden" value="<?php echo $categorie; ?>">
         	<input name="conditie" type="hidden" value="<?php echo $conditie; ?>">
         	<input name="page" type="hidden" value="<?php echo $pagination->get_page(); ?>">
         	<input name="product_id" type="hidden" value="<?php echo $product['product_id']; ?>">
         	<input name="artikel_nummer" type="hidden" value="<?php echo $product['artikel_nummer']; ?>">
         	<input name="product_naam" type="hidden" value="<?php echo $product['product_naam']; ?>">
        	<img src="<?php echo $photo_url; ?>">
           	<h3><?php echo $product['product_naam']; ?><span>&#8364;&nbsp;<?php echo $product['product_prijs']; ?></span></h3>
                    	
            <p class="product_prijs">&#8364;&nbsp;&nbsp;<?php echo $product['product_prijs']; ?>
            <span><button type="submit" class="btn"><i class="fa fa-cart-plus"></i></button></span></p>
            </form>
        </li>
  	<?php endforeach; ?>
 </ol>

The query to reach this is:

  SELECT P.*
       , PF.foto
    FROM producten P
    JOIN product_fotos PF ON P.product_id = PF.product_id
   WHERE categorie_id = ?
     AND product_status = ?
ORDER BY artikel_nummer ASC
   LIMIT $start, $per_page

So far I don’t have any problems. But certain parts will fit on multiple models so
I actually also need to display the different models. To accomplish that I tried the following query:

	$sql = "SELECT *
	           FROM modellen M
	      LEFT JOIN product_modellen PM ON M.model_id = PM.model_id
		  WHERE PM.product_id = ?";
			 
	$stmt = $pdo->prepare($sql);
	$stmt->execute(array($product_id));
	
	$modellen = $stmt->fetchAll();

But when I try to output this within the foreach loop:

<?php foreach($results as $model): ?>
<li><?php echo $modellen['model']; ?></li>
<?php endforeach; ?>

I get the Undefined index error. What am I doing wrong?

Thank you in advance

Try this:

<?php foreach($results as $model): ?>

<?php /* DEBUG */  echo '<pre>', print_r( $modellen,1),  '</pre>'; die; ?>

<li><?php echo $modellen['model']; ?></li>
<?php endforeach; ?>


// Simplified version:
<?php 
    foreach($results as $model): 
      /* DEBUG <pre> formats output and makes easier reading  */ 
     echo '<pre>', print_r( $modellen,1),  '</pre>'; die; 
   
      echo '<li>' . $modellen['model'] .'</li>';
   endforeach; 
?>


Edit:
Changes made using desktop

Hi John. Thank you for the reaction. I tried your suggestion but there is no difference. But I think I made a thinking mistake alltogether. The product_id’s are obviously in an array but each model combination should be attached to just one product_id so I made a few changes. In the model:

public function get_modellen(array $ids)
{
    $ids = implode(', ', $ids);
    $sql = "SELECT *
              FROM modellen M
         LEFT JOIN product_modellen PM ON M.model_id = PM.model_id
             WHERE PM.product_id IN ($ids) ";
                  
    $stmt = $this->pdo->query($sql);        
    return $stmt->fetchAll();        
}

and in the controller:

$producten       = $this->artikelen->get_products($categorie,$conditie,$start,$per_page);
$ids             = array();
foreach ($producten as $product)
{
    $ids[]         = $product['product_id'];
}    
$modellen        = $this->artikelen->get_modellen($ids );

and the html

<ul>
    <?php foreach ($modellen as $model): ?>
    <li><?php echo $model['model']; ?></li>
    <?php endforeach; ?>
</ul>

But this is not the right approach either. Like i said each model can have a different model combination attached to it so product 1 can have models 1, 2, and 3 while Product 2 can have models 1, 4 and 6. But the way it is now all products adapt the same models. Right now all products have models 1, 2 and 3.

Edit: To visualize what I mean I made a screenshot. In the database only the first product has models attached (A35, Flexer and PackR) but as you see do all products adapt the same models

What am I doing wrong.

Thank you in advance

Where are the values for $product_id transferred into there from the $_POST array, after having been validated?

Hi SpacePhoenix. Sorry for the ignorance but what do you mean?

I don’t see where $product_id is set.
The undefined index means $product_id was never set.
kind of like.

echo $name; // does nothing and produces undefined index

whereas,

$name = 'loren'
echo $name;

works as expected
Before the code I quoted, echo out $product_id (as it is not an array) and see what you get.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.