gwh Posted February 25, 2010 Share Posted February 25, 2010 Hi everyone, The following code contains a foreach loop that surrounds two li elements: <div class="blue" id="Corporate" style="width:656px;height:396px;"> <div class="header"> <h3>Corporate Wear</h3> <div class="section_nav"> <a href=sec_1"><?php echo $item['itemType'] ?></a> <div style="clear:both"></div> </div> <div style="clear:both;"></div> </div> <div class="layout-left"> <ul id="sec_1" title="knitwear"><?php foreach ($items as $item): ?> <li onclick="openDMXzoneLightbox('../../images/<?php echo $item['itemImage'] . '.jpg'; ?>', {title:'<?php echo $item['itemTitle']; ?>', width:650, height:776}, window);return document.MM_returnValue"> <a href="javascript:void(0);" onclick="openLightbox('../../images/<?php echo $item['itemImage']; ?>', {title:'<?php echo $item['itemTitle'] . '.jpg'; ?>', width:650, height:776}, window);return document.MM_returnValue"> <img src="../../images/<?php echo $item['itemImage'] . '.jpg'; ?>" border="0" width="650" height="776" alt="" /> </a> </li> <li title="<?php echo htmlout($item['itemTitle']); ?>"> <h4><?php echo htmlout($item['itemTitle']); ?></h4> <?php include(__ROOT__ . "/includes/item_style.inc.php"); ?> <?php include(__ROOT__ . "/includes/item_desc.inc.php"); ?> <?php include(__ROOT__ . "/includes/item_price.inc.php"); ?> <?php include(__ROOT__ . "/includes/item_colours.inc.php"); ?> <?php include(__ROOT__ . "/includes/item_swatches.inc.php"); ?> <?php include(__ROOT__ . "/includes/item_sizes.inc.php"); ?> </li><?php endforeach; ?> </ul> </div> So, <?php foreach ($items as $item): ?> and <?php endforeach; ? surround the list elements. All of the data outputs successfully in this section, however above this is another piece of data that needs to be output, ie. the 'itemType' being echoed in the following link: a href=sec_1"><?php echo $item['itemType'] ?></a> Since the php code in this link is above the code surrounded by the foreach loop, it's not being output. Instead I'm getting an "undefined variable error" that refers to the above line. Like the code in the foreach loop, the variable is coming from an array that was defined in a controller file and the controller file includes this template page. This is the array in the controller file: $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']); } So to eliminate the undefined variable error, I'm assuming I have to define it further up in the code but I'm not sure how to do this. Can someone tell me how to do this? Appreciate any help. Link to comment https://forums.phpfreaks.com/topic/193341-undefined-variable-error/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.