undefined variable error

Discussion in 'PHP' started by gwh, Feb 26, 2010.

  1. #1
    Hi everyone,

    The following code block is an extract from a controller file that I have:

    	// Get list of site sections containing this item
    	$sql = "SELECT siteSectionID FROM item_to_siteSection WHERE itemID='$itemID'";
    	$result = mysqli_query($link, $sql);
    	if (!$result)
    	{
    		$error = 'Error fetching list of selected site sections.';
    		include 'error.html.php';
    		exit();
    	}
    
    	while ($row = mysqli_fetch_array($result))
    	{
    		$selectedSiteSections[] = $row['siteSectionID'];
    	}
    
    	// Build the list of all site sections
    	$sql = "SELECT siteSectionID, siteSection FROM siteSections";
    	$result = mysqli_query($link, $sql);
    	if (!$result)
    	{
    		$error = 'Error fetching list of site sections.';
    		include 'error.html.php';
    		exit();
    	}
    
    	while ($row = mysqli_fetch_array($result))
    	{
    		$siteSections[] = array(
    				'siteSectionID' => $row['siteSectionID'],
    				'siteSection' => $row['siteSection'],
    				'selected' => in_array($row['siteSectionID'], $selectedSiteSections));
    	}
    
    PHP:
    The following is code in a related template file that outputs the above and allows the user to press an edit button in order to edit the contents of that record:

    			<fieldset>
    				<legend>Site Section:</legend>
    				<?php foreach ($siteSections as $siteSection): ?>
    					<div><label for="siteSection<?php htmlout($siteSection['siteSectionID']);
    							?>"><input type="checkbox" name="siteSections[]"
    							id="siteSection<?php htmlout($siteSection['siteSectionID']); ?>"
    							value="<?php htmlout($siteSection['siteSectionID']); ?>"<?php
    							if ($siteSection['selected'])
    							{
    								echo ' checked="checked"';
    							}
    							?>/><?php htmlout($siteSection['siteSection']); ?></label></div>
    				<?php endforeach; ?>
    			</fieldset>
    
          		<div>
    				<input type="hidden" name="itemID" value="<?php
    						htmlout($itemID); ?>"/>
    				<input type="submit" value="<?php htmlout($button); ?>"/>
    			</div>
    
    
    
    PHP:
    When I test this edit button, I get the following error:

    Notice: Undefined variable: selectedSiteSections in /Applications/MAMP/htdocs/new_site/admin/catalogue/items/index.php on line 471

    Warning: in_array() [function.in-array]: Wrong datatype for second argument in /Applications/MAMP/htdocs/new_site/admin/catalogue/items/index.php on line 471

    Line 471 is the following line:

    	'selected' => in_array($row['siteSectionID'], $selectedSiteSections));
    
    PHP:
    I've been trying to troubleshoot for hours but can't see why it's saying 'selectedSiteSections' is an undefined variable.

    Can anyone see where the problem is?

    Appreciate any help.
     
    gwh, Feb 26, 2010 IP
  2. gwh

    gwh Active Member

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    93
    #2
    This issue has since been solved. Thanks
     
    gwh, Feb 27, 2010 IP
  3. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #3
    For anyone else that had that error it comes from where a variable has not been set and you are trying to use it. You can remove notices in the error setting, I think it's

    
    error_reporting(E_ALL ^ E_NOTICE);
    
    PHP:
    That will report all errors and ignore the notices.
     
    HuggyStudios, Feb 27, 2010 IP
  4. HostingProvider

    HostingProvider Active Member

    Messages:
    1,480
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    95
    #4
    You can also use this syntax:
    error_reporting(E_ALL && ~E_NOTICE);
    PHP:
     
    HostingProvider, Feb 27, 2010 IP