Posts Tagged ‘ php ’

Web Programming Final: PHP Zen With Zend

The last part of this web programming class is to present on a topic that we had no former knowledge of. The topic I researched for this presentation was the Zend framework for PHP programming. The power point for this presentation is posted on slide share, and embedded below.

Web Programming Lab 10: A News System

This weeks lab was a little more interesting. We needed to create a new page visible to everyone visiting the site with news articles displayed from registered users. This had an extra credit stipulation for paginating the articles. Also, there needed to be an admin interface for registered users to write and edit news articles. This part had an extra credit option for using TinyMCE. TinyMCE is a useful way for modifying things like text areas to have additional controls for formatting text, much like Word Press’ blog posting system. Each article needed a date, an author, a title, and the actual content. To start, I added the necessary table in my database to support news articles. The admin interface is composed of the post writing area above a list of editable previous posts. If the title of any of these is clicked, the page will pull the information into the writing form for editing. This is fairly easy, submitting new posts simply as form content to the databases, and updating the database for editing. To check it out, create an account on my site, and visit the News Admin section. The other part of the site was the actual news display page. To see it, look here. The main display for the page is a simple smarty block that gets displayed as content.
To handle the back end, the code assigning the page is more like this:

$smarty->assign('news', getPaginatedNews($_GET['page_id']-1));
$smarty->assign('page_id', $_GET['page_id']-1);
$smarty->assign('show_prev', true);
$smarty->assign('show_next', true);
$smarty->assign('news', getPaginatedNews($_GET['page_id']+1));
$smarty->assign('page_id', $_GET['page_id']+1);
$smarty->assign('show_prev', true);
$smarty->assign('show_next', true);
$smarty->assign('news', getPaginatedNews(0));
$smarty->assign('page_id', 0);
$smarty->assign('show_next', true);
$smarty->assign('image', 'images/villains.jpg');
$smarty->assign('title', 'News');

This handles the navigation as well as the pagination based on page ids that I have assigned. The paginating news retrieval looks like this:

function getPaginatedNews($page_id){
$news = array();
$index = $page_id*3;
$query = "SELECT * FROM ssm_articles ORDER BY date desc LIMIT ".$index.",3";
$result = mysql_query($query);
while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
$news[] = $row;
return $news;

This is a nice little function, and was probably the most interesting thing to come out of the assignment. It took me a couple seconds to realize that if I used a 0 based set of page ids, then I could pass them in, multiply by three, and use them as the base for what i wanted to display as I wanted to display three posts per page. This was an interesting lab that makes me feel like with some more refinement, I could write my own blogging software.

Web Programming Lab 9: Databases

In this weeks lab, we finnaly moved onto the section that I have been waiting for. We finally are moving our sites to teh proper storage system of a mySQL database instead of text files for storing our users, and tracking history. To set up the connection so that it could be called on any page, I placed the connection code in my common.php file which is prepended to all other files in the directory. To set up a mySQL connection, it should look something like this:
$_GLOBALS['DB'] = mysql_connect('servername','username','password') or die('Error connecting to mysql'.mysql_error());
mysql_select_db('databasename', $_GLOBALS['DB']) or die('Error connecting to database'.mysql_error());

For using the database, query structure can look something like this to get all users:
function getAllUsers(){
$query = 'SELECT * FROM users ORDER BY username asc';
$users = array();
$result = mysql_query($query);
while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
$users[] = $row;
return $users;

This example runs the query to retrieve all of the users in my users table, and then loops through, turning each returned entry into an associative array that is then appended to the users array that is returned. This is a much better storage measure than text files. Check out the site here.

Web Programming Lab 7: Users

This weeks lab was all about users. We were instructed to create a way for users to apply to our page, and then to display a list of users on another page. The catch: we were not allowed to use databases for this. Instead, we needed to use text files. Well, for the assignment that isn’t horrible, but to get the extra credit we needed to make users editable. I took mine one step further, and made it so other users shouldn’t be able to edit another user’s information. Also, the lab dealt a lot with error handling on the page. For the error handling, I used regular expressions. An example is my email regular expression. It should handle most emails, but will fail with emails like It looks something like this:
if (!preg_match('/[a-zA-Z0-9\._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/', $email))
$email_message='This is not a valid email address';

My zip codes are checked more simply by making sure they are five digits and numeric. The more complex work stemmed from the editing of users. This actually completely re-writes the users file when a user is updated. The function looks like this:

function updateUser($id, $first_name, $last_name, $email, $zip, $password){
$page = ("users.txt");
$fp = file_put_contents($page ,'');
foreach($users as $user){
$fp = file_put_contents($page , $line, FILE_APPEND);

To create an account on my site go here, and to check out the users of my site look here.