Posts tagged ‘PHP’

Dynamically Assigning XSLT And XML Files Via PHP And Apache mod_rewrite

Recently asked on Yahoo! Answers:

How to work with XML files in PHP?
I’ve a couple directories filled with thousands of xml files.

Also all these xml files should be reachable with their filename (but have .php at the end instead of .xml) in the url.

For example in the article directory the xml file named “news.xml”should be reachable like this:
http://www.website.com/article/news.php
2nd example in the articles directory the xml file named “helpwithjeans.xml” should be reachable like this:
http://www.website.com/article/helpwithj
3rd example in the products directory the xml file named “bluray.xml” should be reachable like this:
http://www.website.com/product/bluray.ph

How to read what url is typed and process it by going to the right xml file? After that the xml still needs to be converted to one of several specific html formats.

Something like using the parameter “l” see in above examples (2nd and 3rd) to know which layout is desired and which xml elements to grab.

How can this be handled? And if there is multiple options, what do you suggest is a good way to handle this.

Thanks for pointing me in the right direction!

I was thinking XSLT might be the way to convert the xml to html but I don’t know how to use the “l” parameter from the url (if I’m heading int hat direction) in the XSLT and that would really be needed.

I’m really open to any input. Everything may help. Thanks a lot.

The most efficient way to accomplish this task is via a combination of Apache’s mod_rewrite module and PHP’s XSLTProcessor class.

By adding the two together, we can reduce the number of files needed to convert any number of these XML files to one PHP file.

Continue reading ‘Dynamically Assigning XSLT And XML Files Via PHP And Apache mod_rewrite’ »

Cleaning Up Some PHP And Incorporating A JavaScript-Based Image Preview

Recently asked on Yahoo! Answers:

Change image when new option selected in PHP?
This probably will also require Javascript. I am using this code:
What it does is draws from a MySQL database where an “avatars” table is set. The “avatars” table includes fields “id” (INT), “title” (VARCHAR) and “url” (VARCHAR).
Basically, the script draws the rows from the table. It puts the titles in an HTML select form as options.

{code block snipped}

When an option is selected, I would like the image that is selected to be shown, without a new page having to be loaded.

This is very easy to do with JavaScript and DOM manipulation, but based on the user’s code, it requires a bit more work that previous blog entries I’ve made on images and JavaScript.

Let’s start with looking at the original code block.

Continue reading ‘Cleaning Up Some PHP And Incorporating A JavaScript-Based Image Preview’ »

The Trouble With PHP’s Weak Data Types: An Example Examined And Explained

Asked recently on Yahoo! Answers:

Multiple nested MySQL functions in PHP?

I was going through code today, trying to make some things more compact. The application worked without problems, so I knew that mysql error statements were superfluous.

This is the type of statement I was changing, I figured that I’d remove the seemingly unnecessary $result2 variable

$result2 = mysql_query($carts) or die(mysql_error());<br />
while ($row2 = mysql_fetch_array($result2)) {<br />
	echo "<option>$row2[0]</option>";<br />
}

so this is what I changed it into (I basically replaced where it said $result2, to what $result2 had contained, and removed the error check)

while ($row2 = mysql_fetch_array(mysql_query($carts))) {<br />
	echo "<option>$row2[0]</option>";<br />
}

but this code returned infinite loops, much to my surprise. Why is it doing this? is there a way around it?

Recently, I wrote about how PHP’s weak data types often can lead to problems for new programmers who don’t understand the difference between null, empty and zero-length variables. Here’s another opportunity to expose why strong data typing is essential for best programming practices, and to show how PHP’s weak data types — normally, a source of comfort for beginning programmers — can be the source of extensive frustration.

Continue reading ‘The Trouble With PHP’s Weak Data Types: An Example Examined And Explained’ »

Better Managing Your PHP Application Via Modularization And Abstraction

Most Web sites are designed within a template or two. That is, the layout, typography and basic design of every page is fundamentally the same for most, if not all, pages.

Also, most Web pages tend to need the same resources. If you have a database-driven site, many pages need to use the same connection; other objects, such as user-made classes, functions and the like, often need to be used by many pages.

Templates are nothing new. Adobe has even built a dedicated Web authoring application, Contribute, that lets non-designers update content in templates made in Dreamweaver. Most open-source Web applications, such as Zen Cart, WordPress and phpBB, all allow you to “skin” their applications.

Thanks to the power of PHP, we can accomplish these goals on our own custom Web designs with abstraction and modularization. I’ll explain how I use both, in a typical PHP Web application, to streamline coding and provide, as much as possible, the ability to edit something once and have that change appear globally.

Continue reading ‘Better Managing Your PHP Application Via Modularization And Abstraction’ »

More Sociable Hacks: Adding MySpace And Yahoo! Buzz, Changing The Look

I’ve done some more hacking of the Sociable social bookmarking plug-in for WordPress and I’d thought I’d share them with others.

I’ve added buttons for MySpace and Yahoo! Buzz. I’ve also changed the layout to put all the icons inside a fieldset tag and eliminate the rollover effects.

I’ve also cleaned up some more code, most notably making a change to the way the script creates excerpts when the user has not done so.

Let’s look at the changes.

Continue reading ‘More Sociable Hacks: Adding MySpace And Yahoo! Buzz, Changing The Look’ »