Jump to content

Undefined variable error


gwh

Recommended Posts

Hi everyone,

 

I have the following code in a controller file:

 

<?php
if ($_SERVER['HTTP_HOST'] != "newsite.com") { 
   define ('__ROOT__', $_SERVER['DOCUMENT_ROOT'] . '/new_site');
} else {
   define ('__ROOT__', $_SERVER['DOCUMENT_ROOT']);
}

include_once(__ROOT__ . "/includes/db.inc.php");

$sql = 'SELECT
	items.itemID, 
	itemTitle, 
	itemSKULadies, 
	itemSKUMen, 
	itemDescLadies, 
	itemDescMen,  
	itemPriceBoth,
	itemPriceFemale,
	itemPriceMale,
	itemColoursBoth,
	itemColoursFemale,
	itemColoursMale,
	itemTypes.itemType,
	sizesMen.size AS Msize, 
	sizesLadies.size AS Lsize, 
	itemSwatchBoth,
	itemSwatchFemale,
	itemSwatchMale,
	itemImage
	FROM items
	LEFT JOIN sizesMen ON sizesMen.sizeMenID=items.sizeMenID 
	LEFT JOIN sizesLadies ON sizesLadies.sizeLadiesID=items.sizeLadiesID
	LEFT JOIN itemTypes ON itemTypes.itemTypeID=items.itemTypeID
	WHERE itemTypes.itemTypeID=1';

$result = mysqli_query($link, $sql);
if (!$result)
{
$error = 'Error fetching suits: ' . mysqli_error($link);
include 'error.html.php';
exit();
}

while ($row = mysqli_fetch_array($result))
{
$items[] = array('itemTitle' => $row['itemTitle'], 'itemSKULadies' => $row['itemSKULadies'], 'itemSKUMen' => $row['itemSKUMen'], 'itemDescLadies' => $row['itemDescLadies'], 'itemDescMen' => $row['itemDescMen'], 'itemPriceBoth' => $row['itemPriceBoth'], 'itemPriceFemale' => $row['itemPriceFemale'], 'itemPriceMale' => $row['itemPriceMale'], 'itemColoursBoth' => $row['itemColoursBoth'], 'itemColoursFemale' => $row['itemColoursFemale'], 'itemColoursMale' => $row['itemColoursMale'], 'Lsize' => $row['Lsize'], 'Msize' => $row['Msize'], 'itemType' => $row['itemType'], 'itemSwatchBoth' => $row['itemSwatchBoth'], 'itemSwatchFemale' => $row['itemSwatchFemale'], 'itemSwatchMale' => $row['itemSwatchMale'], 'itemImage' => $row['itemImage']);
}

include '../../catalogue_suits.php';

?>

 

After the above code is run, it includes a file called catalogue_suits.php. The following code block is a part of that file:

 

    <ul id="sec_1" title="Suits"><?php foreach ($items as $item): ?>
      <li onclick="openDMXzoneLightbox('../../../../images/corp/corporate/suits/<?php echo $item['itemImage']; ?>', {title:'Casual Suit', width:650, height:776}, window);return document.MM_returnValue">
        <a href="javascript:void(0);" onclick="openDMXzoneLightbox('../../../../images/corp/corporate/suits/<?php echo $item['itemImage']; ?>', {title:'Casual Suit', width:650, height:776}, window);return document.MM_returnValue">
        <img src="../../../../images/corp/corporate/suits/<?php echo $item['itemImage']; ?>" border="0" alt="" />
        </a>
      </li>
      <li title="<?php echo htmlout($item['itemTitle']); ?>">
                <h4><?php echo htmlout($item['itemTitle']); ?></h4>
                <p>Style Number: <?php echo htmlout($item['itemSKULadies']); ?> </p>
			<p>Description: <?php include(__ROOT__ . "/includes/item_desc.inc.php"); ?></p>
                <p>Price: $<?php echo htmlout($item['itemPriceBoth']); ?></p>
                <p>Colours: <?php echo htmlout($item['itemColoursBoth']); ?></p>
                <p>Sizes: <?php echo htmlout($item['Lsize']); ?></p>
      </li><?php endforeach; ?>
    </ul>

 

When I test, I get the following error:

 

Notice: Undefined variable: items in /Applications/MAMP/htdocs/new_site/business/catalogue/catalogue_suits.php on line 52

 

Line 52 of the above included file is:

 

<ul id="sec_1" title="Suits"><?php foreach ($items as $item): ?>

 

Can anyone see what the problem is here? I've looked at it over and over and I can't see what it is. I know that in the controller file I have the following items array:

 

while ($row = mysqli_fetch_array($result))
{
$items[] = array('itemTitle' => $row['itemTitle'], 'itemSKULadies' => $row['itemSKULadies'], 'itemSKUMen' => $row['itemSKUMen'], 'itemDescLadies' => $row['itemDescLadies'], 'itemDescMen' => $row['itemDescMen'], 'itemPriceBoth' => $row['itemPriceBoth'], 'itemPriceFemale' => $row['itemPriceFemale'], 'itemPriceMale' => $row['itemPriceMale'], 'itemColoursBoth' => $row['itemColoursBoth'], 'itemColoursFemale' => $row['itemColoursFemale'], 'itemColoursMale' => $row['itemColoursMale'], 'Lsize' => $row['Lsize'], 'Msize' => $row['Msize'], 'itemType' => $row['itemType'], 'itemSwatchBoth' => $row['itemSwatchBoth'], 'itemSwatchFemale' => $row['itemSwatchFemale'], 'itemSwatchMale' => $row['itemSwatchMale'], 'itemImage' => $row['itemImage']);
}

 

I can't work it out. Would really appreciate any advice.

 

Link to comment
https://forums.phpfreaks.com/topic/192019-undefined-variable-error/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.