Random PHP Page of Mine

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
CoolAsCarlito
Forum Contributor
Posts: 192
Joined: Sat May 31, 2008 3:27 pm
Contact:

Random PHP Page of Mine

Post by CoolAsCarlito »

Just seeing if anyone suggests a different/easier way to write some or any of my code.

Code: Select all

<?php 
if (isset($_REQUEST['option'])) {
switch ($_REQUEST['option']) {
case 0:
?>
<h1 class=backstage>Menus Management</h1><br />
<h2 class="backstage">Menus:: <a href="#" onclick="ajaxpage('backstage_libs/menustructures.php?option=1', 'content'); return false;">Add New</a></h2><br />
<?php    
$query = "SELECT * FROM efed_list_menus ORDER BY `menu`";
$result = mysql_query ( $query ); 
$rows = mysql_num_rows($result);
if ($rows > 0)  {
print'<table width="100%" class="table1">
<tr class="rowheading">
<td>&nbsp;</td>
<td>Menu</td>
<td>&nbsp;</td>
</tr>';
$i = 0;
while ( $row = mysql_fetch_array ( $result, MYSQL_ASSOC ) ) {
$sClass = 'row2';
if ($i++ % 2) $sClass = 'row1';
printf ( "<tr class=\"%s\">", $sClass );
print "<td valign=\"top\" align=\"center\" width=\"30\"><a href=\"#\" onclick=\"ajaxpage('backstage_libs/menustructures.php?option=2&id=$row[id].', 'content'); return false;\">Edit</a></td>";
printf ( "<td valign=\"top\">%s</td>", $row ['menu'] );
print "<td valign=\"top\" align=\"center\" width=\"80\"><a href=\"#\" onclick=\"ajaxpage('backstage_libs/menustructures.php?option=3&id=$row[id].', 'content'); return false;\">Items</a></td>";
echo '</tr>';
}
echo '</table><br>';
} else {
echo '<span>There are no menu structures.</span><br /><br />';
}
returnmain();
footercode();
break;
case 1:
require_once('../backstageconfig.php');
require_once('../backstagefunctions.php');
?>  
<h1 class="backstage">Menu Management</h1><br />
<h2 class="backstage">Add Menu</h2><br />
<form name="addmenu" method="post">
<input type="hidden" name="action" value="menustructures" />
<table width="100%" class="table2">
<tr>
<td width="120" class="rowheading" valign="center">Menu:</td><td class="row3"><input type="text" name="menu" class="fieldtext490"></td>
</tr>
</table><br />
<input type="hidden" name="newadded" value="true">
<input type="submit" value="Save Menu" class="button"></form><br />
<input type="button" value="Return to Menu List" class="button200"><br />
</form><br />
<?php
returnmain();
break;
case 2:
require_once('../backstageconfig.php');
require_once('../backstagefunctions.php');
$id = $_GET['id'];
$query = mysql_query("SELECT * FROM `efed_list_menus` WHERE `id` = '" . $id . "'");
$row = mysql_fetch_array($query);   
?>
<h1 class="backstage">Menu Management</h1><br />
<h2 class="backstage">Edit Menu</h2><br />
<form name="editmenu" method="post">
<input type="hidden" name="action" value="menustructures" />
<table width="100%" class="table2">
<tr>
<td width="120" class="rowheading" valign="center">Menu Name:</td><td class="row3"><input type="text" name="menu" class="fieldtext490" value="<?php echo $row['menu'];?>"></td>
</tr>
</table><br />
<input type="hidden" value="True" name="editmenu" />
<input type="checkbox" name="deletemenu"><span class="table1heading">Delete Menu and Menu Items?</span><br /><br />
<input type="hidden" name="oldmenu" value="<?php echo $row['id']; ?>">
<input type="submit" value="Edit Menu" class=button><br /><br />
<input type="button" value="Return to Menu List" class="button200"><br />
</form><br />
<?php
returnmain();
break;
case 3:
require_once('../backstageconfig.php');
require_once('../backstagefunctions.php');
$id = $_GET['id'];
$query = mysql_query("SELECT * FROM `efed_list_menus` WHERE `id` = '" . $id . "'");
$row = mysql_fetch_array($query);   
?>
<h1 class=backstage>Menu Management</h1><br />
<h2 class=backstage><?php echo $row['menu']; ?> Items:: <a href="#" onclick="ajaxpage('backstage_libs/menustructures.php?option=4&id=<?php echo $row['id']; ?>', 'content'); return false;">Add New</a></h2><br />
<?php
$query = "SELECT * FROM `efed_list_menu_items` WHERE `menu_id` = '" . $id . "'";
$result = mysql_query ( $query ); 
$rows = mysql_num_rows($result);
if ($rows > 0)  {
print'<table width="100%" class="table1">
<tr class="rowheading">
<td>&nbsp;</td>
<td>Item Name</td>
<td align=center>Item URL</td>
</tr>';
$i = 0;
while ( $row = mysql_fetch_array ( $result, MYSQL_ASSOC ) ) {
$sClass = 'row2';
if ($i++ % 2) $sClass = 'row1';
printf ( "<tr class=\"%s\">", $sClass );
print "<td valign=\"top\" align=\"center\" width=\"30\"><a href=\"#\" onclick=\"ajaxpage('backstage_libs/menustructures.php?option=5&id=$row[id].', 'content'); return false;\">Edit</a></td>";
printf ( "<td valign=\"top\">%s</td>", $row ['itemname'] );
printf ( "<td align=\"center\" valign=\"top\">%s</td>", $row ['itemurl'] );
echo '</tr>';
}
echo '</table><br>';
} else {
echo '<span>There are no items for this menu.</span><br /><br />';
}
returnmain();
break;
case 4:
require_once('../backstageconfig.php');
require_once('../backstagefunctions.php');
$id = $_GET['id']; 
?>  
<h1 class="backstage">Menu Management</h1><br />
<h2 class="backstage">Add New Menu Item</h2><br />
<form name="addmenuitem" method="post">
<input type="hidden" name="action" value="menustructures" />
<table width="100%" class="table2">
<tr>
<td width="120" class="rowheading" valign="center">Item Name:</td><td class="row3"><input type="text" name="itemname" class="fieldtext490"></td>
</tr>
<tr>
<td class="rowheading">Application:</td><td class="row3">
<select name="application" class="selection">
<option value="0">- Select -</option>
<?php
$query = 'SELECT * FROM efed_site_bio_templates';
$result = mysql_query ( $query );
while ( $row = mysql_fetch_assoc ( $result ) ) 
{
    print "<option value=\"".$row['id']."\">".$row['application_name']."</option>\r";
}
?>
</select></td>
</tr>
<tr>
<td class="rowheading">News Category:</td><td class="row3">
<select name="newscat" class="selection">
<option value="0">- Select -</option>
<?php
$query = 'SELECT * FROM efed_list_newscategory';
$result = mysql_query ( $query );
while ( $row = mysql_fetch_assoc ( $result ) ) 
{
    print "<option value=\"".$row['id']."\">".$row['categoryname']."</option>\r";
}
?>
</select></td>
</tr>
<tr>
<td class="rowheading">Content Page:</td><td class="row3">
<select name="content" class="selection">
<option value="0">- Select -</option>
<?php
$query = 'SELECT * FROM efed_site_content';
$result = mysql_query ( $query );
while ( $row = mysql_fetch_assoc ( $result ) ) 
{
    print "<option value=\"".$row['id']."\">".$row['description']."</option>\r";
}
?>
</select></td>
</tr>
<tr>
<td width="120" class="rowheading" valign="center">Item URL:</td><td class="row3"><input type="text" name="itemurl" class="fieldtext490"></td>
</tr>
<tr>
<td width="120" class="rowheading" valign="center">Sort Order:</td><td class="row3"><input type="text" name="sortorder" class="fieldtext490"></td>
</tr>
</table><br />
<input type="hidden" name="newadded2" value="true">
<input type="hidden" name="menuid" value="<?php echo $id; ?>">
<input type="submit" value="Save Menu Item" class="button"><br /><br />
<input type="button" value="Return to Menu Item List" class="button200"><br />
</form><br />
<?php
returnmain();
break;
case 5:
require_once('../backstageconfig.php');
require_once('../backstagefunctions.php');
$menuitemid = $_GET['id']; 
$query = mysql_query("SELECT * FROM `efed_list_menu_items` WHERE `id` = '" . $menuitemid . "'");
$row = mysql_fetch_array($query); 
?>
<h1 class="backstage">Menu Management</h1><br />
<h2 class="backstage">Edit Menu Item</h2><br />
<form name="editmenuitem" method="post">
<input type="hidden" name="action" value="menustructures" />
<table width="100%" class="table2">
<tr>
<td width="120" class="rowheading" valign="center">Item Name:</td><td class="row3"><input type="text" name="itemname" class="fieldtext490" value="<?php echo $row['itemname']; ?>"></td>
</tr>
<tr>
<td class=rowheading>Menu:</td><td class="row3">
<select name="menu" class="selection">
<option value="0">- Select -</option>
<?php
$query = 'SELECT * FROM efed_list_menus';
$result = mysql_query ( $query );
while ( $menu_row = mysql_fetch_assoc ( $result ) ) 
{
    print "<option value=\"".$menu_row['id']."\" ";
    if($menu_row['id'] == $row['menu_id']) {
        print " SELECTED";
    }
    print ">".$menu_row['menu']."</option>\r";
}
?>
</select></td>
</tr>
<tr>
<td class=rowheading>Application:</td><td class="row3">
<select name="application" class="selection">
<option value="0">- Select -</option>
<?php
$query = 'SELECT * FROM efed_site_bio_templates';
$result = mysql_query ( $query );
while ( $application_row = mysql_fetch_assoc ( $result ) ) 
{
    print "<option value=\"".$application_row['id']."\" ";
    if($application_row['id'] == $row['application_id']) {
        print " SELECTED";
    }
    print ">".$application_row['application_name']."</option>\r";
}
?>
</select></td>
</tr>
<tr>
<td class=rowheading>News Category:</td><td class="row3">
<select name="newscat" class="selection">
<option value="0">- Select -</option>
<?php
$query = 'SELECT * FROM efed_list_newscategory';
$result = mysql_query ( $query );
while ( $newscat_row = mysql_fetch_assoc ( $result ) ) 
{
    print "<option value=\"".$newscat_row['id']."\" ";
    if($newscat_row['id'] == $row['newscat_id']) {
        print " SELECTED";
    }
    print ">".$newscat_row['categoryname']."</option>\r";
}
?>
</select></td>
</tr>
<tr>
<td class=rowheading>Content Page:</td><td class="row3">
<select name="content" class="selection">
<option value="0">- Select -</option>
<?php
$query = 'SELECT * FROM efed_site_content';
$result = mysql_query ( $query );
while ( $content_row = mysql_fetch_assoc ( $result ) ) 
{
    print "<option value=\"".$ncontent_row['id']."\" ";
    if($content_row['id'] == $row['content_id']) {
        print " SELECTED";
    }
    print ">".$content_row['description']."</option>\r";
}
?>
</select></td>
</tr>
<tr>
<td width="120" class="rowheading" valign="center">Item URL:</td><td class="row3"><input type="text" name="itemurl" class="fieldtext490" value="<?php echo $row['itemurl']; ?>"></td>
</tr>
<tr>
<td width="120" class="rowheading" valign="center">Sort Order:</td><td class="row3"><input type="text" name="sortorder" class="fieldtext490" value="<?php echo $row['sortorder']; ?>"></td>
</tr>
</table><br />
<center>
<input type="checkbox" name="deletemenuitem"><span class="table1heading">Delete Menu Item?</span><br /><br />
<input type="hidden" name="menuitemid" value="<?php echo $row['id']; ?>">
<input type="hidden" value="true" name="editmenuitem" />
<input type="submit" value="Edit Menu Item" class="button"><br /><br />
<input type="button" value="Return to Menu Item List" class="button200"><br /><br />
</form>
<?php
returnmain();
break;
}
}

function menustructures() {
if ((!empty($_POST['newadded']))) {
$menu = mysql_real_escape_string($_POST['menu']); 
$query = "INSERT INTO `efed_list_menus` (menu) VALUES ('".$menu."')";
mysql_query($query);
}
if ((!empty($_POST['newadded2']))) {
$menuid = mysql_real_escape_string($_POST['menuid']);
$content = mysql_real_escape_string($_POST['content']);
$newscat = mysql_real_escape_string($_POST['newscat']);
$application = mysql_real_escape_string($_POST['application']);
$itemurl = mysql_real_escape_string($_POST['itemurl']);
$sortorder = mysql_real_escape_string($_POST['sortorder']);
$itemname = mysql_real_escape_string($_POST['itemname']);
$query = "INSERT INTO `efed_list_menu_items` (`menu_id`, `content_id`, `newscat_id`, `application_id`, `itemname`, `itemurl`, `sortorder`) VALUES ('".$menuid."', '".$content."', '".$newscat."', '".$application."', '".$itemname."', '".$itemurl."', '".$sortorder."')";
mysql_query($query);
}
if ((!empty($_POST['editmenuitem']))) {
$menuitemid = mysql_real_escape_string($_POST['menuitemid']);
$menu = mysql_real_escape_string($_POST['menu']);
$content = mysql_real_escape_string($_POST['content']);
$newscat = mysql_real_escape_string($_POST['newscat']);
$application = mysql_real_escape_string($_POST['application']);
$itemurl = mysql_real_escape_string($_POST['itemurl']);
$sortorder = mysql_real_escape_string($_POST['sortorder']);
$itemname = mysql_real_escape_string($_POST['itemname']);
$query = "UPDATE `efed_list_menu_items` SET `content_id`='".$content."', `newscat_id`='".$newscat."', `application_id`='".$application."', `itemname`='".$itemname."', `itemurl`='".$itemurl."', `sortorder`='".$sortorder."' , `menu_id`='".$menu."' WHERE `id` = '".$menuitemid. "'"; 
mysql_query($query);
if (isset($_POST['deletemenuitem'])){
$query = "DELETE FROM `efed_list_menu_items` WHERE `id` =  '".$menuitemid."' LIMIT 1"; 
mysql_query($query);
}
}
if ((!empty($_POST['editmenu']))) { 
$menu = mysql_real_escape_string($_POST['menu']);
$oldmenu = mysql_real_escape_string($_POST['oldmenu']); 
$query = "UPDATE efed_list_menus SET `menu` = '".$menu."' WHERE `id` = '".$oldmenu. "'";  
mysql_query($query);
if (isset($_POST['deletemenu'])){
$query = "DELETE FROM `efed_list_menus` WHERE `menu` =  '".$menu."' LIMIT 1"; 
mysql_query($query);
$query2 = "DELETE FROM efed_list_menu_items WHERE `menu_id` =  '".$oldmenu."'";
mysql_query($query2);
}
}
}
?>
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Random PHP Page of Mine

Post by Jonah Bron »

Non-exhaustive list:
  • Indent your code
  • Stick to a standard syntax
  • Try to keep as much HTML out of the PHP as possible:

    Code: Select all

    // wrong:
    <?php
    if (true) {
    echo '<span>Text here</span>';
    }
    ?>
    // right:
    <?php
    if (true) {
    ?>
    <span>Text here</span>
    <?php
    }
    ?>
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Random PHP Page of Mine

Post by Jenk »

I have to ask... is this a joke?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Random PHP Page of Mine

Post by josh »

Put the html in a template file
Post Reply