Archive for August, 2009

Implementing Vanity URLs in PHP w/ Zend Framework

One of the reasons why people like vanity url’s is because they are easy to remember. For example take the following url:

http://www.somesocialsite.com/?user_id=123456789&page=somepage

If this was the url to my page in some social network site, there’s no way I could remember it nor would I be able to easily share the url with others unless I sent them the link. Now, if I could instead create a vanity url that looked like the following:

http://www.somesocialsite.com/joeyrivera

it would be much easier to remember and to share with others. Not only that but now I have a much more search engine friendly url with keywords that I would like to be found under – but ignore the search engine benefits for now.

Why

I’m currently working on an application that can benefit from vanity urls for the reasons mentioned above so I decided to spend some time thinking of ways to implement this. The first way that came to my mind was using mod_rewrite. Mod rewrite lets you manipulate urls. For example, you can write rules in your .htaccess file so when a user goes to http://www.somesocialsite.com/joeyrivera it really calls http://www.somesocialsite.com/search.php?user=joeyrivera or in zend the request would be more like http://www.somesocialsite.com/search/user/name/joeyrivera Read the rest of this entry »

Tags: , , , , , , ,

Extract collections of similar objects from an array in php

A co-worker was working on some stuff dealing with creating collections of objects within an array so I decided to play around with the idea. Basically, you start with a big array filled of objects and the objective is to create smaller arrays of similar objects. In this case we want collections of person objects who names are the same. Here is what I came up with:

<?php

class person
{
        public $id = null;
        public $name = ;

        public function __construct($id, $name)
        {
                $this->id = $id;
                $this->name = $name;
        }
}

$persons = array();
$persons[] = new person(1, ‘bob’);
$persons[] = new person(2, ‘bob’);
$persons[] = new person(3, ‘moses’);
$persons[] = new person(4, ‘joey’);
$persons[] = new person(5, ‘bob’);
$persons[] = new person(6, ‘joey’);
$persons[] = new person(7, ‘bob’);
$persons[] = new person(8, ‘moses’);
$persons[] = new person(9, ‘joey’);
$persons[] = new person(10, ‘joey’);

$collections = array();

// loop until there are no person left in array
while(count($persons) > 0)
{
        // new collection and insert first person from array
        $collection = array();
        $collection[] = $persons[0];

        // now remove person from array since already in collection
        array_shift($persons);

        // loop through each person in array
        for($x = 0; $x < count($persons); $x++)
        {
                // check if there is a match
                if($persons[$x]->name == $collection[0]->name)
                {
                        // add person to this collection
                        $collection[] = $persons[$x];

                        // pop if last item
                        if($x+1-count($persons) == 0)
                        {
                                array_splice($persons, $x);
                        }
                        else
                        {
                                array_splice($persons, $x, $x+1-count($persons));
                        }
                        // move back since array position gets deleted, else will skip over next index
                        $x–;
                }
        }
        $collections[] = $collection;
}

debug($collections);
function debug($o)
{
        echo ‘<pre>’;
        print_r($o);
        echo ‘</pre>’;
}

And the result is:

Array
(
    [0] => Array
        (
            [0] => person Object
                (
                    [id] => 1
                    [name] => bob
                )

            [1] => person Object
                (
                    [id] => 2
                    [name] => bob
                )

            [2] => person Object
                (
                    [id] => 5
                    [name] => bob
                )

            [3] => person Object
                (
                    [id] => 7
                    [name] => bob
                )

        )

    [1] => Array
        (
            [0] => person Object
                (
                    [id] => 3
                    [name] => moses
                )

            [1] => person Object
                (
                    [id] => 8
                    [name] => moses
                )

        )

    [2] => Array
        (
            [0] => person Object
                (
                    [id] => 4
                    [name] => joey
                )

            [1] => person Object
                (
                    [id] => 6
                    [name] => joey
                )

            [2] => person Object
                (
                    [id] => 9
                    [name] => joey
                )

            [3] => person Object
                (
                    [id] => 10
                    [name] => joey
                )

        )

)

You start with 1 big array and end up with, in this case, 3 arrays of person whos names are ‘bob’, then ‘moses’, and finally ‘joey’.

Tags: , ,

SQLSTATE[HY000]: General error – using php/pdo mysql stored procedures (sp)

I spent many hours last night trying to figure out why I was getting a fatal error: ’SQLSTATE[HY000]: General error 0′ in my code and finally figured it out but first my environment. I’m using Zend Server CE running php 5.3.0, zend framework 1.9.0 and mysql 5.1.32. I should have tested this bug without the zend framework to make sure it’s not specific to zf (I don’t think it is) but I’m feeling lazy so I’ll let someone else try it out.

My code works as follows. I have a php class that calls a stored procedure which will take in an id, return a record set (if found) and will also return 2 out variables. While it was returning a record set everything was working perfectly fine. When I tried passing an invalid id, nothing was being returned and my code would keep giving me the ‘SQLSTATE[HY000]: General error 0′ (very helpful error indeed…).

The issue ended up being the way I had my stored procedure coded. I would first check to see if the id passed was valid, if so I would select the data else I would set my out vars to some value and do nothing else. For some reason, because I wasn’t returning a select, my code would blow up. In the mysql query browser, my stored procedure worked fine and my second select to get the out vars was working correctly. But php didn’t like it one bit. I tried forcing a select in my stored procedure in the invalid id section and then everything worked fine again. This sounds a bit confusing so here is the way I can replicate this. Read the rest of this entry »

Tags: , , , , , , , , ,

Creating RSVP in PHP/MySQL w/ Zend Framework

This post is to share the php/zend framework code I used to create an rsvp for my wedding site. I’m not going into all the details since that would take too long ;p but all the code is available if you want to use it. This was created using the zend framework version 1.7.

So I’m getting married in two month and for our wedding my fiancee and I decided to create a website for our guests. The site includes information such as location, time, links to registries, maps, and a section to rsvp. The site was made by my fiancee in html and css. When she was done, I ported it over to zend framework and started creating the rsvp section which I’ll describe next. You can view the finished wedding site here:

RSVP Page

http://www.joeyrivera.com/wedding

Read the rest of this entry »

Tags: , , , , ,