Location: PHPKode > projects > ArticleSetup > ArticleSetup-v1-1/upload/admin/categories.php
<?php 
	session_start();
	include ('../config.php');
	include ('secureadmin.php');
	
	$page = "cat";		
	
	if(isset($_POST['add'])) { 

		$newtitle = mysql_real_escape_string($_POST['title']);
		$newparent = $_POST['parentid'];
		if (!$newparent) {
		$newparent = "NULL";
		}
		
		$sql = "INSERT into categories VALUES (null, ".$newparent.", '".$newtitle."');";
				$query = mysql_query($sql);
				header('Location: categories.php?add=true');
				exit();
	}

	
	if(isset($_POST['update'])) { 

		$newtitle = mysql_real_escape_string($_POST['title']);
		$newparent = $_POST['parentid'];
		$catid  = $_POST['catid'];
		if (!$newparent) {
		$newparent = "NULL";
		}
		
		$sql = "UPDATE categories SET name='".$newtitle."', parentid=".$newparent." where id =".$catid;
				$query = mysql_query($sql);
				header('Location: categories.php?update=true');
				exit();
	}

	
	if(isset($_POST['delete'])) { 

		$newcat = $_POST['newcat'];
		$catid  = $_POST['catid'];
		
		$query = "select * from articles where categoryid =".$catid;
		$result = mysql_query($query,$connection) or die(mysql_error());
		$num_results = mysql_num_rows($result);
		
		$catquery = "select * from categories where id =".$newcat;
		$catresult = mysql_query($catquery,$connection) or die(mysql_error());
		$info = mysql_fetch_assoc($catresult);
		$parentid = $info['parentid'];
		
		if(!$parentid) {
		for ($i=0; $i <$num_results; $i++) { 

			$row = mysql_fetch_assoc($result); 
			$sql = "UPDATE articles SET categoryid = ".$newcat.", parentid=NULL where id =".$row['id'];
			$query = mysql_query($sql);
		}
		} else {
		for ($i=0; $i <$num_results; $i++) { 

			$row = mysql_fetch_assoc($result);
					
			$sql = "UPDATE articles SET categoryid = ".$newcat.", parentid=".$parentid." where id =".$row['id'];
			$query = mysql_query($sql);
		}
		}			
				
		$sql = "DELETE from categories where id =".$catid;
		$query = mysql_query($sql);

		header('Location: categories.php?delete=true');
		exit();
	}


	$metatitle = "Category Management - Admin Control Panel";
	include ('includes/document_head.php');
?>
		<div id="wrapper">
			<?php include 'includes/topbar.php'?>		
			<?php include 'includes/sidebar.php'?>
			<div class="main_container container_16 clearfix">
				
					
			<div class="flat_area grid_16">
			
				<h2>Category Management</h2>
				<p>This page contains a listing of all categories on this article directory. You can easily <strong>add, delete, and edit your categories</strong> by using the table below. Simply click the <strong>category title</strong> or the <strong>Edit/Delete link</strong> to make changes to your categories. To add a new category, click the button below:</p>
		
		<a href="categoryedit.php?id=new"><button  class="skin_colour round_all"><img width="24" height="24" src="images/icons/small/white/Create Write.png"><span>Add New Category</span></button></a>

		
			</div>
			
<?php 
	if($_GET["update"] == "true") {
	echo '<center><p style="color: red;"><b>Category Updated</b></p></center>';
	} 
	if($_GET["delete"] == "true") {
	echo '<center><p style="color: red;"><b>Category Deleted</b></p></center>';
	} 
?>
			<div class="box grid_16 round_all">
				<table class="display table"> 
					<thead> 
						<tr> 
							<th>id</th> 
							<th>Name</th> 
							<th>Parent</th> 
							<th># of Articles</th> 
							<th>Actions</th> 
						</tr> 
					</thead> 
					<tbody> 
					
	<?php // Populates the Dropdown list with all categories and subcats
	$query = "select * from categories where parentid is null;"; 
	$result = mysql_query($query,$connection) or die(mysql_error());
	$num_results = mysql_num_rows($result);
	
	for ($i=0; $i <$num_results; $i++) { 
		$row = mysql_fetch_assoc($result);
	
		$totalquery = "select * from articles where categoryid = ".$row['id']; 
		$totalresult = mysql_query($totalquery,$connection) or die(mysql_error());
		$totalarticles = mysql_num_rows($totalresult);
	
		echo "<tr> <td>".$row['id']."</td> 
			 <td><a href=\"categoryedit.php?id=".$row['id']."\">".$row['name']."</a></td> 
			 <td>--</td>
			 <td>".$totalarticles."</td>
			 <td><a href=\"categoryedit.php?id=".$row['id']."\">Edit/Delete</a></td></tr>";
			 
			// Loop through all subcategories 
			$query = "select * from categories where parentid =".$row['id'].";"; 
			$sub_result = mysql_query($query,$connection) or die(mysql_error());
			$sub_num_results = mysql_num_rows($sub_result);
			
			$parentname = $row['name'];
			$parentid = $row['id'];			
			for ($x=0; $x <$sub_num_results; $x++) { 
				$subrow = mysql_fetch_assoc($sub_result);
			
				$subtotalquery = "select * from articles where categoryid = ".$subrow['id']; 
				$subtotalquery = mysql_query($subtotalquery,$connection) or die(mysql_error());
				$subtotalarticles = mysql_num_rows($subtotalquery);

				echo "<tr> <td>".$subrow['id']."</td> 
			 		<td><a href=\"categoryedit.php?id=".$subrow['id']."\">".$subrow['name']."</a></td> 
			 		<td><a href=\"categoryedit.php?id=".$parentid."\">".$parentname."</a></td>
			 		<td>".$subtotalarticles."</td>
			 		<td><a href=\"categoryedit.php?id=".$subrow['id']."\">Edit/Delete</a></td></tr>";

			}	

	}
	?>
					</tbody> 
				</table>
			</div>


				</div>				
			
			
			


			
		</div>	
<?php include 'includes/closing_items.php'?>
Return current item: ArticleSetup