THAT Agency Design Studio Blog

Let’s just begin by stating that XML is not my forte. Ok, with that out the way lets get on with it. So recently my challenge was to grab a file from a 3rd party server that was CSV. No harm no foul right? well not when the 3rd party site had to be connected from a secure FTP. So after rummaging around the web I found (phpseclib0.2.1a) which worked great to connect and download the CSV file I needed. Then I took that CSV (because let’s face it – nobody really likes CSV) and converted it into XML.

Ok?! so what right?

Well the data that is coming in a hodge-podge of information which I didn’t really need. I only needed a certain part of the XML file. So what was i suppose to do?

After a bunch of trial and error, internet help and so on and so forth, here is the solution I came up with:

First let’s just assume that your XML file looks something like this:

<data>
	<row>
    	<id>00001</id>
      	<some_name>Some Name 1</some_name>
      	<category>1</category>
	</row>
    <row>
    	<id>00002</id>
      	<some_name>Some Name 2</some_name>
      	<category>2</category>
	</row>
    <row>
    	<id>00003</id>
      	<some_name>Some Name 3</some_name>
      	<category>1</category>
	</row>
    <row>
    	<id>00004</id>
      	<some_name>Some Name 4</some_name>
      	<category>2</category>
	</row>
<data>

Now in this example lets assume that do_you_need_me = 0 (meaning you DON’T need it) and obviously do_you_need_me = 1 (meaning you DO need it) .

There is alot of information out there some that might work, some that might not work. This is how i was able to deal with this and have it work.

This code will irrerate through the XML file and select EVERYTHING that is NOT a category of 1 (category = 1) and delete those from the XML file and of course saves the XML file again.

Here is the complete function that I used to make this happen.

$file_xml = "/path/to/your/xml/file.xml";
$data = simplexml_load_file($file_xml);
$data_count	= count($data->row);
for($i = 0; $i < $data_count; $i++)
{
	if($data->row[$i]->category == "1")
    {
        unset($data->row[$i]);
    }
    else
    {
		// I just like keeping the else in here. just in case.		
    }
}
file_put_contents($file_xml, $data->saveXML());

Quick Synopsis

I have left the comments that I tested with in there so you can test if you want, just make sure you comment out unset($data->row[$i]);

So $file_xml is self explanitory.
we then loop through the XML file and if the current rows do_you_need_me != 1 then we go ahead and delete the record. Be mindful on the size of your XML file. If its too large a file, I would maybe suggest taking “file_put_contents($file_xml, $data->saveXML());” and moving it under the “unset($data->row[$i]);” to save the file after every run.

If you are running this through a cron, first check if your server allows you to override the time with “set_time_limit(0);” if they dont, then you might want to move that “file_put_contents($file_xml, $data->saveXML());” as explained above and run the cron a couple times in a row just to be certain that all the information you need has been parsed.

If you have anything to add or comment about please do so. If there is an easier way to handle this, I would love to know.

Share this article

There are characteristics that every web developer has.  One of those things is the way a project is started. While some developers might use open source Content Management Systems / Blogging software like: WordPress, Joomla, Movable Type or any of the other systems out there, some developers chose to have a foundation to their projects.

Here at THAT Agency, we write alot of custom code for our clients, but I personally still like building off of my own foundation. Here is my breakdown.

Note that we usually build in PHP.

  • css/
  • inc/
  • js/
  • img/
  • template.php

Seems pretty simple right? well lets look inside the folders themselves.

css

This folder includes 2 files (which you can merge into 1, but i like the two files).  We have been really utilizing the 960 Grid System for our development standards, so the first css file (reset-960.css) is pretty much what you expect. It contains the condensed versions of reset as well as the 960 grid. I opted not to use the text.css file that is included in the 960 grid download but wrote my own (this you can change to your own).

The next file site.css contains some regular standard css elements:

/* important */
html{background-color:#F9F9F9;}
body{font-family:Arial, Helvetica, sans-serif;font-size:14px;line-height:1.3em;}
*{margin:0;padding:0;}
/* headers */
h1{font-family:Georgia, "Times New Roman", Times, serif;font-size:24px;font-weight:normal;}
h2{font-family:Georgia, "Times New Roman", Times, serif;font-size:22px;font-weight:normal;}
h3{font-family:Georgia, "Times New Roman", Times, serif;font-size:20px;font-weight:normal;}
h4{font-family:Georgia, "Times New Roman", Times, serif;font-size:18px;font-weight:normal;}
h5{font-family:Arial, Helvetica, sans-serif;font-size:16px;font-weight:bold;}
h6{font-family:Arial, Helvetica, sans-serif;font-size:14px;font-weight:bold;}
h1,h2,h3,h4,h5,h6{margin:10px 0;padding:0;display:block;clear:both;}
/* Text and links */
p{font-family:Arial, Helvetica, sans-serif;line-height:1.5em;margin:0 0 10px 0;padding:0;}
blockquote{font-style:italic;display:block;margin:20px 0;padding:10px;background-color:#fff;border-top:1px solid #ccc;border-bottom:1px solid #ccc;}
a,a:link,a:visited{color:#21759B;}
a:hover{color:orange;}
ul{margin:0 0 0 10px;}
ul li{margin:0 0 0 10px;padding:0;}
/* clear pieces */
.clear5{height:5px;}
.clear10{height:10px;}
.clear15{height:15px;}
.clear20{height:20px;}
.clear25{height:25px;}
.clear50{height:50px;}
.clear100{height:100px;}
/* Text Classes */
.textRight{text-align:right;}
.textLeft{text-align:left;}
.textCenter{text-align:center;}

inc

The inc(ludes) folder for starters contains 3 files:

header.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DEVELOPMENT TEMPLATE</title>
<link rel="stylesheet" media="all" type="text/css" href="/css/reset-960.css" />
<link rel="stylesheet" media="all" type="text/css" href="/css/site.css" />
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="/js/site.js"></script>
</head>

analytics.php
This file was created to house your analytics code for your website. I find it annoying to have to copy and past something as simple as that on every page of your website.

footer.php

</body>
</html>

Like the idea? Check it out for yourself and download the zip file. It contains 2 folders. 1 for HTML and the other PHP

Download the template-foundation zip

Share this article

Ok, I know that there are plenty of classes, functions, pre-built scripts out there that will help you create an RSS feed to use with your website, but I have built my own and I will share it with all of you. This RSS page, will do a couple of things.

1. Grab the Title of your Article, Grab some Description, Grab the Link to your article and so on and so forth.
2. If by chance, any part of the article is in spanish, it will convert anything with an accent to regular letters so that the feed can actually validate.

You can check out the code here:

* Please note that in order for this to work, the server needs to know that it needs to look at this code that processes as XML…so place the RSS files into ONE folder…call it feeds or rss or something of that nature and edit the .htaccess file and place this next line of code in it:

AddType application/x-httpd-php .xml

————————-
All you have to do is change the parts needed, and rename it to whatever you want.
————————

<? header(‘Content-type: text/xml’); ?>

<? echo “<?xml version=\”1.0\” encoding=\”iso-8859-1\”?”.”>”;
?>

<?

$today = date(“D, j M Y G:i:s T”);

/*print (‘<?xml version=”1.0″?>’);

*/

$rssdate = date(“r”);

print (”

<rss version=\”2.0\” xmlns:dc=\”http://purl.org/dc/elements/1.1/\”>

<channel>

<title>TITLE OF THE FEED </title>

<link>LINK TO ACTUAL WEBSITE </link>

<description>DESCRIPTION OF THE FEED </description>

<language>en-us</language>

<pubDate>$today</pubDate>

<lastBuildDate>$today</lastBuildDate>

<docs>http://blogs.law.harvard.edu/tech/rss</docs>

<generator>YOU CAN PUT YOUR COMPANY NAME </generator>

<managingEditor>EMAIL@YOUREMAIL.COM</managingEditor>

<webMaster>EMAIL@YOUREMAIL.COM</webMaster>

“);

// prepare HTML text for use as UTF-8 character data in XML

function cleanText($intext) {

return utf8_encode(

htmlspecialchars(

stripslashes($intext)));

}

//Will Get snippet of Return Row depending how many words you want.

function ReturnSnippet ($string, $howmany)

{

$currString = explode(” “, $string);

for ($wordCounter=0; $wordCounter<$howmany; $wordCounter++) { echo $currString[$wordCounter].” “;
}

};

# Required to Connect to DB to get the information

# Opens DB Connection OR PLACE THE ACTUAL DB CONNECTION HERE.

require_once (“DBConn.php”);

$db = opendatabase();

# this function will gather all the information from the Authors table and
create a drop down of writers.

function BuildRSSFeed()

{

$sql = mysql_query(“SELECT id, categoryiD, title, body, datepublished
FROM tablename WHERE categoryID = ‘the id of the category you want to see’
ORDER BY id DESC”);

# THIS FOLLOWING FUNCTION WILL ACTUALLY CLEAN UP THE ACCENTS

function cleanUpAccents($str)

{

$patterns = array (“&aacute;”,”&eacute;”,”&iacute;”,”&oacute;”,”&uacute;”,”&Aacute;”,”&Eacute;”,”&Iacute;”,”&Oacute;”,”&Uacute;”);

$replace = array (“a”,”e”,”i”,”o”,”u”,”A”,”E”,”I”,”O”,”U”);

$stringReplaced = str_replace($patterns, $replace,$str);

return str_replace($patterns, $replace,$str);

}

while ($row = mysql_fetch_array($sql))

{

//Tue, 03 Jun 2003 09:39:21 GMT

$today = date(“D, j M Y G:i:s T”);

//$text = cleanText($row[body]);

print (”

<item>

<title>”);

$articleTitle = $row[title];

$title_accent = cleanUpAccents($articleTitle);

$title_final = $title_accent;

echo ($title_final);

print(“</title>

<link>http://www.yourdomain.com/whatever.php?id=$row[id]</link>

<description>”);

$text = ReturnSnippet ($row[body], “10″);

$text_accent = cleanUpAccents($text);

$finaltext = htmlentities(strip_tags($text_accent));

echo ($finaltext);

print (” </description>

<pubDate>$row[datepublished]</pubDate>

<author>EMAIL@EMAIL.com</author>

<guid>http://www.yourdomain.com/whatever.php?id=$row[id]</guid> #
this is a Globaly Unique IDentifier – meaning just put in the link of the article
thats NOT going to change.

</item>\n”);

}

};

/*

<?=htmlentities(strip_tags() );
include(‘xml.php’);

$xml = XML_serialize();

*/

echo BuildRSSFeed();

print (”

</channel>

</rss>

“);

?>

Share this article

PHP was at one time known as “Personal Home Page” but evolved into something so much more powerful than just that. It’s officially now known as “PHP: Hypertext Preprocessor” and what that means is that unlike Javascript which executes within the browser, PHP executes before it even hit’s the users screen. A PHP script is not sent directly to the client by the server; instead it is parsed by the PHP binary or module, which is server-side installed.

No more worrying about whether or not your user has Javascript enabled – it matters not with PHP. The output from PHP looks and smells like pure html and to the untrained and trained eye, it is.

So, why should you be using PHP?
There are endless reasons why but I’ll narrow it down to just a few. As an open source product, PHP is well supported by a talented and vigilant production team. PHP can and will run on all major operating systems without flaw. Because PHP allows you to separate HTML code from scripted elements, you will notice a significant decrease in development time on many projects. In many instances, you will be able to separate the coding stage of a project from the design and build stages. Not only can this make life easier for you as a programmer, but it also can remove obstacles that stand in the way of effective and flexible design.

Share this article