Welcome to my site, this is a place for me to post not only my portfolio but also short blog type posts as well about various things I learn along my path. The abridged version of the about page is that I am currently enrolled at Champlain College working towards my B.S. in Web Site Development and Management. I enjoy programming very much, I hate the feeling of running into a problem I cannot solve, but this makes solving the problems all the more fun. So, check out my site and let me know if you like it, don't like it, hopefully you find something is broken because then I will have something tricky to work on. So go off and enjoy the site.
Access nested attributes using SimpleXML
I have been working on writing a function to edit an XML document using just SimpleXML. This is a sample of the XML file I am working with today:
<?xml version="1.0" encoding="UTF-8"?>
<Employees>
<Employee id="a21">
<First_Name>Levi</First_Name>
<Last_Name>Jackson</Last_Name>
<Position>Web Developer</Position>
<Department>Programming</Department>
<Status>Full-Time</Status>
<Contact_Info>
<Phone type="work">ext.123</Phone>
<Phone type="cell">850-123-1029</Phone>
<Email>Aerosox@gmail.com</Email>
</Contact_Info>
<Access_Info level="user">
<Username>ljackson</Username>
<Password>pass123</Password>
</Access_Info>
<Start_Date>2009-01-12</Start_Date>
<Photo>./images/Unwin.jpg</Photo>
<SSN>123-12-3121</SSN>
<Salary>$21,000</Salary>
</Employee>
</Employees>
Accessing the children of Employees was the easy part. Create the SimpleXML object and the nodes become properties of the object.
$simpleObj = simplexml_load_file($xml);
foreach($simpleObj as $emp){
$emps = get_object_vars($emp);
$fn = $emps->First_Name; //returns "Levi"
}
The trouble I had was in accessing the attributes of the child nodes. I was thinking about it in terms of the attributes being arrays, when in fact they were a child simpleXML object! After some reading about the SimpleXML object I came this solution for accessing the user type for my "employee".
$level = $emps["Access_Info"]->attributes()->level;
What this does is select the child node of Employee called "Access_Info". From there the attributes() method is called which returns a new simpleXML object to work with. So what was once hard is now as easy as the first example. Once we have the SimpleXML object we can then access the values quite easily.
Conclusion
So what did I take away from this experience? Well for starters just because it looks complicated, doesn't make it so. The biggest issue I had was that I was being returned SimpleXML objects and I wasn't sure how to access the information. It became easier to work with once I could visually see that objects can nests inside one another. Just because the SimpleXML object was a child of another object didn't mean I couldn't use the same methods on either. Essentially what I learned was that when given a situation that I am unsure of, the best way to work through it is to assess my options.
Posted by Levi on April 4 2010
Passing by Reference in PHP
I was just reading up on some PHP while I work on one of my final projects and I came across the topic of passing by reference. Which if you are not familiar with it, this is the PHP.net reference page. I had no idea that it existed and it instantly reminded me of the past three weeks of C++ where we have been discussing and using pointers as a means of "returning" more than one value from a function.
A good explanation of a reference is:
"References are a way to have multiple variables referencing the same variable container using different names -- so whatever name you're using an operation on that variable will always have an effect on the others." - Johannes Schlüter
My experience with pointers and passing by reference has been in the classroom and so the only benefit I have seen is that you can modify variables outside of a function. Check this snippet out for instance:
function addOne(&$input){
$input++;
}
$var = 1;
addOne($var);
//returns 2.
Conclusion
I don't know that I would really pass by reference outside of the classroom at this stage as I have not encountered a situation where I needed to return or modify more than one value at a time.
Posted by Levi on March 30 2010
Tags: php
Google Ranking
Last week I was watching state of the index by Matt Cutts and he mentioned in the future Google may rank sites based on how quickly the page loads. So I decided to test my site.
The test
I ran my site through Web Page Test and got some astonishing results. I failed on almost every account! Not having a lot of time there were some suggestions I was able to take an implement easily. The first was to enable gzip. I had always though I would have to install some fancy script to do this so I found excuses not to do it. Now I know that is not the case. It can be enabled quite easily if you have access to your php.ini file or if you just want to include this nice little snippet at the top of your page!
ini_set('zlib.output_compression','On');
I was also told that I had too many css and js files included and it suggested combining them. Understandable, but how can one combine the core jQuery file and their main script file and have it still be maintainable? I compromised and combined my js script files that I wrote together.
The change
I am not sure if it is because of the simple changes I made or if it is because I have been writing a little more frequently, but my ranking has improved when searching on my name. I usually do a search for 'Levi Jackson' to see where I pop up and currently I am number 3 when I had been 7 or lower for the longest time. Regardless it has made me more interested in optimizing my web pages than I have ever been before.
The current stats
Conclusion
If page speed isn't factored into the ranking yet... it will be. It is better to fix it now when it doesn't play a role than to find your site dropping in ranking and need to rig something together to fix it then.
Posted by Levi on February 3 2010
Tags: google
