Today’s tutorial is going to show you how you can use jQuery Mobile with PHP, MySQL and a bit of AJAX to build a small mobile web application.
At the end of this tutorial you will know how to add new RSS feed name and url into the database, how to delete the record from the database, how to fetch and parse a RSS feed and show its results inside jQuery Mobile list view.
If you are not familiar with jQuery Mobile, I suggest you read my jQuery Mobile basic tutorial and then come back.
So, let’s start. Many things in our RSS reader application will take place inside our index.php file. The front page of the application will have links for creating new RSS feed and Read existing feeds.
First of all we need a database table to store our feeds in. Create one using this SQL code:
[code lang=”sql”]
CREATE TABLE IF NOT EXISTS `demo` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`url` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM
[/code]
Now, I will create a db.php which will have a database connection and some basic functions for database handling. Create folder named inc and put your db.php file in it with this code pasted:
[code lang=”php”]
/*** mysql hostname ***/
$hostname = ‘localhost’;
/*** mysql username ***/
$username = ‘root’;
/*** mysql password ***/
$password = ”;
$dbname = ‘demo’;
try
{
mysql_connect($hostname, $username, $password);
mysql_select_db($dbname);
}
catch (Exception $e)
{
// normally we would log this error
echo $e->getMessage();
}
function db_select_list($query)
{
$q = mysql_query($query);
if (!$q) return null;
$ret = array();
while ($row = mysql_fetch_array($q, MYSQL_ASSOC)) {
array_push($ret, $row);
}
mysql_free_result($q);
return $ret;
}
function db_select_single($query)
{
$q = mysql_query($query);
if (!$q) return null;
$res = mysql_fetch_array($q, MYSQL_ASSOC);
mysql_free_result($q);
return $res;
}
[/code]
This is a pretty basic code for connecting and fetching records from our database.
To keep things organized, I will put my header section inside external PHP file. Inside inc folder create file named header.php, then paste this in the header file:
[code lang=”php”]
[/code]
This is basic HTML 5 header with included jQuery and jQuery Mobile.
Now create a file in your root directory and name it index.php and paste this in it:
[code lang=”php”]
[/code]
Save everything and try it in your browser. Pretty cool.
As you can see our links to other pages look like anchor links. I am going to have multiple pages inside my index.php file and that is the way to link to them.
Now paste this whole code inside index.php overwriting the previous code:
[code lang=”php”]