Value of variable not being output

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

  1. #1
    Hi everyone,

    The following template page displays a list of categories in a database:

    
    <?php include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/helpers.inc.php'; ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    		"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    	<head>
    	<title>Manage Categories</title>
    	<meta http-equiv="content-type"
    				content="text/html; charset=utf-8"/>
    </head>
    	<body>
    		<h1>Manage Categories</h1>
    		<p><a href="?add">Add new category</a></p>
    		<ul>
    			<?php foreach ($categories as $category): ?>
    				<li> 
    			  <form action="" method="post">
    						<div>
    							<?php htmlout($category['category']); ?>
                                
    					    <input type="hidden" name="catID" value="<?php
    									echo $category['catID']; ?>"/>
    							<input type="submit" name="action" value="Edit"/>
    							<input type="submit" name="action" value="Delete"/>
    						</div>
    					</form>
    				</li>
    			<?php endforeach; ?>
    		</ul>
    	</body>
    </html>
    
    
    PHP:
    The above code has an edit and delete submit button which when clicked loads a php controller page as follows:

    
    <?php
    if (isset($_POST['action']) and $_POST['action'] == 'Delete')
    {
    	include $_SERVER['DOCUMENT_ROOT'] . '/includes/dbAdmin.inc.php';
    	$id = mysqli_real_escape_string($link, $_POST['catID']);
    
    	// Get items with certain category
    	$sql = "SELECT itemID FROM items WHERE catID='$id'";
    	$result = mysqli_query($link, $sql);
    	
    	if (!$result)
    	{
    		$error = 'Error getting list of items to delete: ' . mysqli_error($link);
    		include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.inc.html.php';
    		exit();
    	}
    
            // assume that no match has been found
    	$recordsExist = false;
    
    	// check whether recordset found any matches
    	if (mysqli_num_rows($result) > 0) {
    
    		$recordsExist = true;
    		
    		$sql = "SELECT category FROM categories WHERE catID='$id'";
    		$result = mysqli_query($link, $sql);
    		if (!$result)
    		{
    			$error = 'Error getting category to display: ' . mysqli_error($link);
    			include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.inc.html.php';
    			exit();
    		}
    		
    		list($category) = mysqli_fetch_row($result);
    
    	 	include 'category_delete.html.php';
    		exit();
    
     	 }
    		else if (mysqli_num_rows($result) == 0) {
    		
    		list($category) = mysqli_fetch_row($result);
    			
    		include 'category_delete.html.php';
    		exit();
    			
    		// Delete the category
    		$sql = "DELETE FROM categories WHERE catID='$id'";
    		if (!mysqli_query($link, $sql))
    			{
    				$error = 'Error deleting category: ' . mysqli_error($link);
    				include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.inc.html.php';
    				exit();
    			}
    			
    			header('Location: .');
    			exit();
    		}
    }
    
    // Display category list
    include $_SERVER['DOCUMENT_ROOT'] . '/includes/dbAdmin.inc.php';
    $result = mysqli_query($link, 'SELECT catID, category FROM categories');
    if (!$result)
    {
    	$error = 'Error fetching categories from database! – ' . mysqli_error($link);
    	include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.inc.html.php';
    	exit();
    }
    
    while ($row = mysqli_fetch_array($result))
    {
    	$categories[] = array('catID' => $row['catID'], 'category' => $row['category']);
    }
    
    include 'categories.html.php';
    ?>
    
    
    PHP:

    Code in the above controller file checks to see whether the category associated with the delete button that was clicked on, already exists in an items table and if it has it includes the category_delete.html.php page to display a message saying that the category could not be deleted. The following code is what constitutes this category_delete.html.php page:


    
    <?php include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/helpers.inc.php'; ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    		"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    	<head>
    	<title>Manage Categories</title>
    	<meta http-equiv="content-type"
    				content="text/html; charset=utf-8"/>
    </head>
    	<body>
    		<h1>Delete Category</h1>
            
            <p>
        <?php
    if ($recordsExist) {
      echo '<p class="warning">'. $category . ' category has dependent records. Can\'t be deleted.</p>';
      }
    else {?>
    
    </p>
    <p class="warning">Please confirm that you want to delete the following record. This operation cannot be undone. </p>
    
            
    <ul>
    				<li>
    					<form action="" method="post">
    						<div>
    					    <?php htmlout($category); ?>
    							<input type="hidden" name="catID" value="<?php
    									echo $category['catID']; ?>"/>
    							<input type="submit" name="delete" value="Confirm deletion"/>
    						</div>
    					</form> 
    </li>
    		</ul><?php } ?>
    	</body>
    </html>
    
    PHP:
    If there is a category already in use then the code functions as it should and the delete page with the message displays correctly.

    If the category is available for deletion then the code in the controller again includes the category_delete.html.php page so that the user can confirm the deletion before it takes place. Currently, it includes the file but the form that displays the category isn't outputting the value of the variable $category.

    The following is the line (in the above delete page) that isn't being output:

    <?php htmlout($category); ?>

    There is no error appearing, it just seems not to evaluate that line.

    I wondered if someone could tell me what's going on?
     
    gwh, Jan 26, 2010 IP
  2. gwh

    gwh Active Member

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    93
    #2
    I've managed to work this out by moving some code blocks around.
     
    gwh, Jan 27, 2010 IP