Posts Tagged ‘PHP’

Still looking for fast web development techniques

I’ve been developing web sites for almost 10 years now. I’m still in search of a way to develop faster. I find myself doing things over and over again.

I use PHP as the programming language to develop the web apps with MySQL database. I started to look for different frameworks – but most use MVC and I don’t have time to learn it.

Some have suggested to do validation using MySQL. This helps a bit so I don’t have to write more PHP code to validate whether or not the record exists, foreign key constraints, etc. Plus it really is good practice to have these in place in the database layer.

Next I looked in to template engines to help with the layout of the site. It does help quite a bit but I’m still left with a lot of PHP code. I use Dreamweaver to write code and I used to use its automated code writing but I found it too messy and inefficient. Now I only use it because the FTP client is built in, has a file checkout system and in CS5 it can read included files so I have easy access to them. The error check is nice too. It catches many syntax errors right away. It can read used variables and be a part of the autocomplete feature.

I tried using existing systems like CMS or blogs (Joomla and WordPress). I figured I can create add-ons on top of an existing system. Joomla uses the MVC framework. WordPress on the other hand is easier to figure out how to build on top of it. Plus their documentation is really easy to follow. One of the limitations I found is the lack of access levels. Another is the pre-existing environment may not fit with a project since it’s so customized – better to be built from the ground up.

I’ve been peeking into the .NET framework off and on. I don’t primarily use Windows at all. I’m mostly on a Mac or using some sort of Linux flavor. I do have Windows7 running virtually on my MBP and iMac so I can play with it. I have played with Visual Studio and it felt easy to use plus it seemed to make developing quick. The drawbacks I’ve found are Windows hosting is more expensive than Linux hosting, IDEs are expensive and run mostly on Windows OS, and I don’t like creating online applications that can potentially only work with Internet Explorer (does not support web standards and only available on Windows OS).

Rails looks promising with the DRY (don’t repeat yourself) approach. I’ve tried to look into it and even bought some books and watched some screencasts. It’s a great language but I can’t seem to understand how to authenticate/authorize different users. They have many different plugins that I can use but each one uses their own way of doing it.

I guess I’ll keep looking. Either I’ll find time to learn MVC framework, get better at Rails, or someone will develop a new platform to speed up web development.

  • Share/Bookmark

Creating a wrapper page within WordPress 2.9

I’ve been searching for a way to create a wrapper page within WordPress without having to install a plugin. I browsed the WordPress files and found wp-blog-header.php in the root install directory. It’s really simple.

<?php
require_once(‘wp-blog-header.php’);
get_header();

// this is the content layout based on the current theme you are using

get_sidebar();
get_footer();

?>

I’m not sure when this was implemented. All of the articles I found online mentions plugins that had to be installed or creating a new page within the WordPress admin GUI then include a file, etc. I just need to create a custom PHP file that will display the header, sidebar, and footer.

Depending on the theme you are using will determine additional code between each section (header, sidebar, footer). So check out the page.php or index.php of the theme you are using to see how it is structured.

  • Share/Bookmark

Using PEAR Pager with PHP PDO

This post will be addressing 2 things.

  1. How to use PEAR’s Pager class with PHP PDO
  2. Addressing PDO bug #44639 (http://bugs.php.net/bug.php?id=44639)

Two queries will be used for this task. The first query, you will count the rows of the recordset. The second query is the same as the first but it will have the LIMIT – this is what will be used to display the data on each page.

Let’s say we have the following table.

CREATE TABLE IF NOT EXISTS`blog`
(
`blog_id` int(11) not null auto_increment,
`blog_text` varchar(4000) not null default ”,
PRIMARY KEY `pk_blog_id` (`blog_id`)
) ENGINE=INNODB;

Now we’ll query the database and get the row count using PDO.

<?php

require_once(‘file-with-pdo-db-connection.php’);

$sql = “SELECT * FROM `blog`”;
$stmt = $dbh->prepare($sql);

if ($stmt->execute())
{
// begin pager
require_once(‘Pager/Pager.php’);
$params = array
(
‘totalItems’ => $stmt->rowCount,
‘perPage’ => 10,
‘delta’ => 5,
‘mode’ => ‘Sliding’
);

$pager =& Pager::factory($params);

$links = $pager->getLinks();
echo $links['all'];

// offset setup
list($from, $to) = $pager->getOffsetByPageId();
$from = $from – 1;
$perPage = $params['perPage'];

// 2nd query based on 1st with LIMIT – this will be displaying data per page
$stmt2 = $db->prepare($sql.’ LIMIT :from, :perPage’);

// address bug 44639 – forces the variables to have the property of integer instead of string so no quotes will surround it
$stmt2->bindValue(‘:from’, $from, PDO::PARAM_INT);
$stmt2->bindValue(‘:perPage’, $perPage, PDO::PARAM_INT);
$stmt2->execute();

while ($row_blog = $stmt2->fetch())
{
echo ‘<div>’.$row_blog['blog_text'].’</div>’;
}

}

$dbh = null;

?>

If you don’t use the bindValue, you will get an error in your query. Your second query would look something like this… SELECT * FROM `blog` LIMIT ’0′, ’10′ and the error would occur right after LIMIT. You cannot have quotes surrounding the numbers. It took a few hours of Googling but found the solution.

If you want to read more about PEAR Pager, visit http://pear.php.net/package/Pager/docs.

Here’s a tutorial I found that helped me out a lot – http://www.codediesel.com/php/simple-pagination-in-php/.

  • Share/Bookmark

Updating PHP 5 with Godaddy and a Dedicated server

Jeff called me up asking for help with his dedicated server on Godaddy. He needed to update his PHP 5.1.6 to the latest stable version. His server is running Centos 5.2 (Final). He tried to call Godaddy for assistance and of course they were no help. I’m no expert myself but have played around with different systems. I found out that you can use the yum command to manage the installed packages. The problem is that the current repositories yum looks at does not contain new versions of PHP. This is why you get an error that says something like – cannot find PHP. What you need to do is add a repository that does have a newer version of PHP. We’re going to add Jason Litka‘s repository.

  • Log in to the Simple Control Panel
  • Log in using SSH
  • Type “su – ” to log in with root privileges
  • Type “nano -w /etc/yum.repos.d/utterramblings.repo”. If you’re not comfortable with nano, use whatever editor you’re more comfortable with.
  • In the utterramblings.repo file type the following

    [utterramblings]
    name=Jason’s Utter Ramblings Repo
    baseurl=http://www.jasonlitka.com/media/EL$releasever/$basearch/
    enabled=1
    gpgcheck=1
    gpgkey=http://www.jasonlitka.com/media/RPM-GPG-KEY-jlitka

  • Save the file and close it
  • Now you can use “yum update php” and it should update it to the latest that the repositories have
  • Restart Apache

This tutorial is based on the one found at http://www.jasonlitka.com/yum-repository/. It took awhile for me to find the solution. I hope this helps out people who had the same problem as I did.

  • Share/Bookmark

Installing PEAR on Godaddy shared accounts

I recently found out how to install PEAR on a Godaddy shared hosting account.

  1. Visit http://pear.php.net/go-pear. Save the text displayed onto your desktop and name it go-pear.php. Upload this to your server. If you’re hosting multiple domains on the account, it’s best that you should upload this file in your root folder. Run this file on your browser and follow the on-screen instructions.
  2. After completing the installation, it is time to edit your php.ini file. If you are running PHP4, there should be a file in the root directory called php.ini. If you are running PHP5, there should be a file in the root directory called php5.ini. If not, create one and add the following: include_path = “.:/usr/local/php5/lib/php:/home/content/s/a/m/sample/html/PEAR”. Keep in mind that /s/a/m/sample is just an example. Doing this will ensure that every page, you create, will look in the PEAR directory so that you it will use the installed packages.
  3. This installation will include Pear_Frontend_Web which is the web-based admin interface. It may have created an index.php file in the directory where PEAR is installed (root). If not you can get a copy from PEAR/docs/PEAR_Frontend_Web/docs/index.php.txt. I would suggest creating a folder called pear_admin in the root directory and storing this file there. If you didn’t install PEAR in the root directory, you may need to edit this file accordingly.
  4. You will need to create a .htaccess and .htpasswd file in the same directory as the index.php file. An example of how the .htaccess file should look as follows:
    AuthUserFile /home/content/s/a/m/sample/html/pear_admin/.htpasswd
    AuthType Basic
    AuthName “Web-based PEAR Frontend”
    Require valid-user

    An example of how the .htpasswd file should look as follows:


    admin:cGyUX9QugYMgE

    This will create “admin” as the user name and “password” as the password. You can generate your own by going to this link – http://www.htaccesstools.com/htpasswd-generator/

    Be aware that files beginning with a dot are invisible. You may have to edit your settings on the FTP app you’re using so that you can see them.

Once the files are created and saved, you can now go to http://your-domain.com/pear_admin/index.php. It will ask for the user name and password. Once you are logged in, you can now manage Pear via web browser. That’s it! Now you can run PEAR on a shared account from Godaddy. One less complaint :)

  • Share/Bookmark
Categories