Now we need to build the management page so we can create new articles.
Let's create "login.php":
<?php session_start(); $auid = isset($_POST['auid']) ? $_POST['auid'] : $_SESSION ['auid']; $pwd = isset($_POST['pwd']) ? $_POST['pwd'] : $_SESSION ['pwd']; if(!isset($auid)) { loginform(); exit; } if( $auid != $aduser || $pwd != $adpass ){ unset($_SESSION['auid']); unset($_SESSION['pwd']); loginform(); exit; }else if( $auid == $aduser && $pwd == $adpass ){ $_SESSION['auid'] = $auid; $_SESSION['pwd'] = $pwd; } ?> <? function loginform(){ global $sitename; ?> <CENTER> <BR> <FONT FACE=verdana color=#FFFFFF size=5><B><?=$sitename? > Login</B></FONT> <BR> <TABLE cellspacing=0> <FORM method=post> <TR><TD>Username: <TD><INPUT type=text name=auid size=16> <TR><TD>Pass: <TD><INPUT type=pass name=pwd size=16> <TR><TD colspan=2><INPUT type=submit value="Log In >>"></TD> </FORM> </TABLE> <BR> <? } ?>
This uses the $aduser and $adpass variables that were set in the config.php file to let you log in to your management panel.
Next, we create a file called "admin.php." This file is the main article management page.
<? include("common.php"); include("login.php"); $mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : ''; switch($mode){ case "new": case "edit": include("cmsform.php"); break; case "save": if($_REQUEST['id']){ UpdatePage($_REQUEST['id']); }else{ AddPage(); } header("Location: admin.php"); break; case "delete": if($_REQUEST['id']){ DeletePage($_REQUEST['id']); } header("Location: admin.php"); break; default: include("cmslist.php"); break; } ?>
Now, let's create "cmslist.php", where we display the articles:
<table width=100% border=1> <? echo listPages(0);
function listPages($parent=0){ $out = ""; $pages = getPages('parent',$parent); $i = 1; foreach($pages as $id=>$page){ $out .= "<tr>"; $out .= "<td>{$i}</td>"; $out .= "<td><a href='admin.php?mode=edit&id={$id}'>{$page ['title']}</a></td>"; if($page['id'] != 1){ $out .= "<td><a href='admin.php?mode=delete&id= {$id}'></a></td>"; } $out .= "</tr>"; $out .= "<tr>"; $out .= "<td> </td>"; $out .= "<td colspan=2><table width=100%>".listPages ($id)."</table></td>"; $out .= "</tr>"; $i++; } return $out; } ?> <tr> <td colspan=2 align=center> <a href='admin.php?mode=new'>Add New Article</a> </td> </tr> </table>
We now have a page that lets us both list and manage articles.
Our next file is "cmsform.php", which is the add/edit form.
<table> <form method="POST" action="account.php?mode=save<?=( isset ($_REQUEST['id']) ? "&id={$_REQUEST['id']}" : null )?>"> <? $pageInfo = ""; if( isset($_REQUEST['id']) ){ $pageInfo = getPage('id',$_REQUEST['id']); } ?> <tr> <td>Title:</td> <td><input type='text' name='title' value='<?=$pageInfo ['title']?>'></td> </tr> <tr> <td>Summary:</td> <td><input type='text' name='summary' value='<?=$pageInfo ['summary']?>'></td> </tr> <tr> <td>Body:</td> <td><textarea name='body' rows=20 cols=80><?=$pageInfo['body']? ></textarea> </tr> <tr> <td>Parent</td> <td> <select name='parent'> <option value='0'>--- <?=buildDropDown(0,1,$_REQUEST['id']);?> </select> </td> </tr> <tr> <td colspan=2><input type='submit' value='Save'></td> </tr> </form> </table> <? function buildDropDown($parent,$level = 1,$articleId){ $sep = ''; if( $level > 0 ){ for ($i = 0; $i < $level; $i++) { $sep = $sep.'...'; } $sep = $sep.' '; } $kids = getPages('parent',$parent); foreach($kids as $k=>$kid){ if( $kid['id'] == $articleId ) continue; $sel = ""; if( isset($qr1[0]['parent']) && $kid['id'] == $qr1[0] ['parent'] ){$sel = "SELECTED";} echo "<option value='".$kid['id']."' $sel>{$sep}{$kid ['title']}</option>"; echo buildDropDown($kid['id'],($level+1),$articleId); } } ?>
You may notice that I make use of a form creation trick here. I use it a lot. You simply create an array in the common.php file that contains the form fields and what type of fields they are, then display it here. It works nicely, and keeps the forms pretty straightforward and not as messy as they can get.
Please enable JavaScript to view the comments powered by Disqus. blog comments powered by