Fetching XML from a remote site

PHP has provided with a wonderful function file_get_contents

We first start with getting the xml feed into a string, by using file_get_contents

//the $url is the link from where XML is received
$url = 'http://devshed.com/index2.php?option=mos_rss&no_html=1'; 
$con = file_get_contents($url); //the contents are in the variable $con

//now create the SAX xml parser $xp = xml_parser_create();

Now using the function xml_parse_into_struct, we parse the xml and create 2 arrays $vals and $index, The $index contains all the info about the node nos. While the most important array is the $vals array

xml_parse_into_struct($xp, $con, $vals, $index);
// we now free the xml parser. xml_parser_free($xp);

If we print_r the $vals array we see something like this

Array
(
	[0] => Array
		(
			[tag] => RSS
			[type] => open
			[level] => 1
			[attributes] => Array
			    (
                    [VERSION] => 2.0
                )

            [value] => 

        )

    [1] => Array
        (
            [tag] => CHANNEL
            [type] => open
            [level] => 2
            [value] => 
	
        )

    [2] => Array
        (
            [tag] => TITLE
            [type] => complete
            [level] => 3
            [value] => Dev Shed - The Open...
        )

		.....
		Something like this

Reading XML RSS Feed

These days there are many websites from which you you can publish their latest information on YOUR site, How ? The XML way.

Sites like Devshed.com, Devarticles.com, XML.com provide an XML format which you can use. But since the format is XML, it can’t be just shown directly, there has to be some sort of formatting done to it. XML-Stylesheet is one way and the other one, that I’ll be explaining to you is using PHP.

xmlCommonly you can see an image like this for feed This is the link from where you can get the XML Feed, or syndicate its content

Code for writing the comment to the DB

Now create a new page called ‘addcomment.php’

<?php
$conn = mysql_connect('localhost', 'username', 'password');
mysql_select_db('yourdb');

$comdate = $_POST['comdate'];
$articleid = $_POST['articleid'];
$comsubject = htmlspecialchars(addslashes($_POST['comsubject']));
$comuser = htmlspecialchars(addslashes($_POST['comuser']));
$comtext = htmlspecialchars(addslashes($_POST['comtext']));

//write the query
$sql = "insert into comments(comdate, articleid, comsubject, comuser, comtext)";
$sql .= " values ('" . $comdate . "', " . $articleid . ", '" . $comsubject . "', '" . $comuser . "', '" . $comtext . "');";

if (!@mysql_query($sql)) {
  echo ('We are unable to register your comment, sorry');
} else {
  echo ("Your comment was added successfully<br>");
  echo ("<A HREF="article.php">Back to Articles</A>");
}

mysql_close($conn);
?> 

User Interface for adding comments

Now that we have had some article in our dB, it is now time to show the article to the world. Plus we need to show the comment to them

Let us make a page called as ‘article.php’ and the article is fetched by getting the a url as ‘article.php?articleid=1’ This will fetch the article # 1 and is stored in the DB as articleid=1

If the articleid query is not given then the page has to display all the aritcle listing with the article subject as an link to ‘article.php?article=[the articleid]’

<?php
//if articleid query is not set, show all the article listing
if (!isset($_GET['articleid'])) {

$conn = mysql_connect('localhost', 'username', 'password');
mysql_select_db('yourdb');

$sql = "select * from articles;";
$result = mysql_query($sql);

echo('<table boder=0 cellpadding=3 cellspacing=1>');
while ($row = mysql_fetch_assoc($result)) {
  echo('<tr>);
  echo('<td>);
  echo('<a href="aricle.php?articleid='.$row['articleid'].'"><b>'.$row['articlesubject'].'</b></a>');
  echo('<tr>);
  echo('<td>);
  echo($row['articlesubject']);
}
echo('</table>')
mysql_close($conn);

} else {

$conn = mysql_connect('localhost', 'username', 'password');
mysql_select_db('yourdb');

$sql = "select * from articles where articleid=" . $_GET['articleid'] . ";";

if (!($result = @mysql_query($sql))) {
  echo('Sorry We are not able to process your request');
} else {
  //now display the data
  $row = mysql_fetch_assoc($result);
  echo ("<p><b>" . nl2br(htmlspecialchars($row['articlesubject'])) .
"</b>");
  echo ("<p>".nl2br(htmlspecialchars($row['articletext']))."<p>");
?>

  Add Comment
  <table>
  <form name='comment' action'addcomment.php' method=post >
  <input type=hidden name='comdate' value="<?php echo(date("Y-m-d H:i:s"))?>">
  <input type=hidden name='articleid' value="<?php echo($_GET['articleid'])?>">
  <tr><td>
  Subject<input type=text name='comsubject' />
  <tr><td>
  User<input type=text name='comuser' />
  <tr><td>
  <textarea name='comtext' rows=15 cols=50></textarea>
  <input type=submit value='Post Comment' />
  </form>
  </table>

<?php
  //now show all the comments for this article
  
  $sql = "select * from comments where articleid=" . $_GET['articleid'] . ";";   $result = mysql_query($sql);
  if (mysql_num_rows($result)<1) {
    echo('No comments posted for this article');
} else {
    while ($row = mysql_fetch_assoc($result)) {
      echo('<p>');
      echo( htmlspechialchars(stripslashes($row['comsubject'])) . ' by ' . htmlspechialchars(stripslashes($row['comuser'])) . ' on ' date("d M Y h:i a", strtotime($row['comdate'])) . '<br>');
      echo( nl2br(htmlspechialchars(stripslashes($row['comtext']))) );
    }
  }
  mysql_close($conn);
  }

}
?>

This now completes the code for viewing the article, the page also displays a form which as a subject textbox, a username textbox and a textarea for comment text.
When user clicks the ‘Post Comment’ button, the data is ‘post’ fed to the page ‘addcomment.php’

Creating tables

The first and most important charecteristic for such type of a system is that the article whatever it may be must be in a database with an articleID associated with it.

So first create an table for an article like this

# Create a table for articles over here
create table articles (
  articleid int(5) primary key auto_increment,
  articlesubject varchar(255),
  articletext text
);

Once done with creating the articles table you need to create a posting table where user can post comments on the article. For that create a table as comments as follows

# Create a table for comments
create table comments (
  comid int(5) primary key auto_increment,
  articleid int(5),
  comsubject varchar(255),
  comuser varchar(255),
  comdate datetime,
  comtext text,
);

Here the articleid is the foreign key, don’t worry about foreign or home. It is just to link the comment to what article. The date time records the date, time in “YYYY-MM-DD HH:MM:SS” format.
comtext is a text field and can store big text values.

Now once you have created the table for article, it is time to fill some values into them let us fill some easy values into it.

> insert into articles(articletext) values ('This is some easy article and is for some testing purpose only');
> insert into articles(articletext) values ('This is another easy article and is for some testing purpose only and not to be fiddled with'); 

PHP Tutorials

Welcome ! to this PHP Tutorials page. I’m not a great PHP code buster, but still I feel that the tricks that I have learnt, found, should be shared with others as well.

Basics

This entry is part 2 of 42 in the series Guitar

An acoustic guitar has 6 strings, hence the “6 string” 🙂
It starts from the E string (this is the string with the lowest pitch, or lowest frequency note), then the A string, followed by the D, G, B strings. And the last one is the e string. This one is the string with the highest pitch. This e sting produces the “E” note of the next octave begining from the base E string.

We’ll denote it as.., This is the plan view (top view) of the guitar, facing the strings.

e +---------------+
B +---------------+
G +---------------+
D +---------------+
A +---------------+
E +---------------+

Now hold the guitar as you generally hold it, the body of it on the lap, and your base of your right hand (considering your are right-handed) on the bridge. Bridge is the point from where the guitar strings start, it is NOT the end where you tune the string with the knobs.

Hold your thumb and index finger as you hold a pen, withing that grip hold the plectrum (pick). The blade of the pick should be at an angle to the strings, and not directly flat to the strings.

Now practice picking the strings, begining from the base E string to the high e string. All the picking should be down strokes. Go in an easy manner, till you get a feel of distances between the strings, etc…

Once you are comfortable with the simple picking start picking alternate strings. i.e. pick E, D, then A, G, then D, B, and G, e. and then back up again. This will help you in getting a hold of picking alternate, different strings.

I’ll explain you a notation that you will follow all over, It is not my notation, probably you must have seen it somewhere.
When I say you play C note, I’ll specify a fret. A fret is marking on which you play a note, to play a note you press your finger just before the nth fret (the fret that is mentioned to produce the note).
So…

e +---------------+
B +---------------+
G +---------------+
D +----0----------+
A +--3------------+
E +---------------+

..means play the 3rd fret on the A string to produce the C note, then the play open D string (i.e do not fret the string anywhere) to play the D note.

Guitar Tutorial

This entry is part 1 of 42 in the series Guitar

This is my guitar class diary, you may consider it to be as your tutorial, guide or whatever, my purpose to publish this one is to keep the contents of my guitar class on the web.
thats it.

I start from my first class where I knew only 1 thing about guitar and that is “6 string” guitar or something… 🙂 quite an information to begin with.

Lets start now…

ऋतुराज का Home Page