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 Final: PHP Zen With Zend
10 12 2008Comments : Leave a Comment »
Tags: Framework, php, Zend
Categories : Programming, Web Programming
Web Programming Lab 10: A News System
23 11 2008This 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:
if($_GET['move']=='prev'){
$smarty->assign('news', getPaginatedNews($_GET['page_id']-1));
$smarty->assign('page_id', $_GET['page_id']-1);
if($_GET['page_id']-1>0){
$smarty->assign('show_prev', true);
}
$smarty->assign('show_next', true);
}
elseif($_GET['move']=='next'){
$smarty->assign('news', getPaginatedNews($_GET['page_id']+1));
$smarty->assign('page_id', $_GET['page_id']+1);
$smarty->assign('show_prev', true);
if(sizeof(getPaginatedNews($_GET['page_id']+2))!=0){
$smarty->assign('show_next', true);
}
}
else{
$smarty->assign('news', getPaginatedNews(0));
$smarty->assign('page_id', 0);
if(sizeof(getPaginatedNews($_GET['page_id']+1))!=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.
Comments : 1 Comment »
Tags: database, mysql, news, paginating, php, posts
Categories : Web Programming
Web Programming Lab 9: Databases
16 11 2008In 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.
Comments : 1 Comment »
Tags: database, mysql, php
Categories : Web Programming
Web Programming Lab 8: Login
1 11 2008The purpose of this weeks lab in my web programming class was to accomplish two things: to enable users to log in to view more content, and to alter the way my page counts visitors so that it will not recount the same user more than once on a visit to my site. I did a large scale revamp on my site this week. It started with altering the css so that I can have left column navigation. From there, I added register and login links to the page for users who are not logged in. If a user registers, it will take them to the login page. Once logged in, users will no longer see the options to login or register, but will gain the “users nav” panel which displays the sites reporting section, the list of current users, as well as the ability to edit their account, and only their account. I can control this and trach them because I have set the $_SESSION['username'] variable. So far for this page, I have developed several functions for user retrieval. I can get all of the users, retrieve them by the unseen user id, or retrieve them by user name. Also as part of the site, I altered the way my site does page counts. Now in my common.php file that is pre-pended to all other pages in my site, I am starting the session on my site, and checking to see if the user has a session cookie set in their browser. If they do, then the page count for the session stays the same. If they do not, I append the url to my log file, and then return the count of pages visited in my site. Since my page is a single entity, I am now only returning one count through the site, as it is the same user if the session is set. Moving all of this over toa database will be appreciated, and increase the efficiency of what I am doing on this site ten fold. To see everything in my site, go here, and create an account using the navigation in the upper right, and then login. The count can be viewed on all of the public pages.
Comments : 1 Comment »
Tags: login, php
Categories : Web Programming
Web Programming Lab 7: Users
27 10 2008This 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 bob@bob.com emails, but will fail with emails like user@mail.plymouth.edu. 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';
$email='';
}
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){
$users=getAllUsers();
$users[$id]['first_name']=$first_name;
$users[$id]['last_name']=$last_name;
$users[$id]['email']=$email;
$users[$id]['zip']=$zip;
$users[$id]['password']=md5($password);
$page = ("users.txt");
$fp = file_put_contents($page ,'');
foreach($users as $user){
$line=$user['id'].'~'.$user['first_name'].'~'.$user['last_name'].'~'.$user['email'].'~'.$user['zip'].'~'.$user['password'];
$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.
Comments : 1 Comment »
Tags: form validation, php, users
Categories : Web Programming
Web Programming: Access tracking with site logs
17 10 2008This weeks lab 6 expanded upon the earlier lab of parcing a log file into useful access data by applying it to our site. I modified my site so that it now appends the address of the page to a log file, which is then searched for the particular page being visited, and returns the count of visits the site has received to that page in the footer. Also, a reporting page has been created that collects all of this information into a nice format dynamically. To check it out, look here. Also, from last week in lab 5, here is the gallery page.
Comments : 2 Comments »
Categories : Web Programming
Web Programming Lab 5: Image Gallery
11 10 2008Moving into the fifth lab of my web programming course, we received a couple of more interesting projects to accomplish. The first of which was to break down a log file to count the hits base directories were receiving. To take a look at the output of the program was, look here. Also, we expanded our web pages that we have been working on to contain a gallery. I used the php GD class to generate thumbs from images that are passed to a simple function I created. When the image path is passed to the function, it begins by checking the directory to see if a thumb of the image has been created yet. If it has, it simply returns the path to the thumb. If the image does no have a thumb, it generates them to meet some criteria housed in the method. To take a look at the new gallery page, look here.
Comments : 1 Comment »
Categories : Web Programming
Moving into templates
6 10 2008In my web programming class, my site has once again changed. This time, my page has progressed onto more familiar ground, as I set up my page using php and Smarty templates. To take a look, go here.
Comments : 1 Comment »
Categories : Web Programming
Working with Yahoo css layouts
29 09 2008I will admit that, although I know some css, the fine tuning that can take place with layouts to handle cross browser compatibility, resizing, relative positioning, etc… can sometimes elude me. This weeks goal in web programming was to implement a yahoo css layout across our site, as well as add two more pages to our developing page. The latest rendition with fancy new layout and styling of the Spectacular Spider-man site can be found here. Also, there is an alternate css for a more printer friendly version of the page. Take a look at the print preview of the page to get a better look.
Comments : 1 Comment »
Categories : Web Programming
Practicing CSS
21 09 2008The latest assignment in my web programming class has been to add CSS to the superhero web page that we have been constructing. Most of my CSS that I used was based around centering elements in the page, as well as controlling behavior of my navigation links. To see the current rendition of the site, look here.
Comments : 1 Comment »
Categories : Web Programming