Thursday 25 April 2013

Fetch RSS feed data in PHP

Create a file named "rssfeed.php" and copy paste the following code.
Try "http://www.aweber.com/blog/feed/" link which is containing link title and images.
See the Demo here: http://creativeideate.net23.net/demos/rssfeed/rssindex.php

⇛ HTML Code: Input field to enter rss feed

<form action="rssfeed.php" method="POST">
    <input type="text" name="feedurl"/>                    
    <input type="submit" name="submit" value="GO"/>
</form>

⇛ PHP code and tabular listing of feed data

<?php
error_reporting(E_ERROR);
if (isset($_POST['feedurl'])):
    $content = file_get_contents($_POST['feedurl']);
    try {
        $rss = new SimpleXmlElement($content);
    } catch (Exception $e) {
        return false;
    }
    $rssdata = array();
    $i = 0;

    foreach ($rss->channel->item as $item) :
        $title = (string) $item->title;
        $link = (string) $item->link;
        $content = $item->children('content', true)->encoded;
        preg_match('/<img.+src=[\'"](?P<src>.+)[\'"].*>/i', $content, $image);
        $image = substr($image['src'], 0, strpos($image['src'], '"'));
        $rssdata[$i]['title'] = $title;
        $rssdata[$i]['link'] = $link;
        $rssdata[$i]['image'] = $image;
        $i++;
    endforeach;
    ?>

    <!-- TABULAR LISTING -->

    <table border="1">
        <th>Image</th>
        <th>Title</th>        

        <?php foreach ($rssdata as $feed): ?>
            <tr>
                <td><img src="<?php echo $feed['image']; ?>" height="100" width="200"/></td>
                <td><a href="<?php echo $feed['link']; ?>"><?php echo $feed['title']; ?></a></td>                
            </tr>
        <?php endforeach; ?>
    </table>

    <?php
endif;
?>

Tuesday 23 April 2013

Disable submit button to prevent double form submission

Sometimes dealing with forms with Submit button can cause duplicate processing issues which results duplicate database records, multiple email id's etc. To prevent this javascript provides an easy way to disable multiple submission of form once the form has been submitted. Its also helpful for reservation/booking management systems such as hotel management system to prevent multiple or unintentional bookings.


Here is the simple javascript code will do the job


<form action="javascript:void(0);" name="myform">
   <input type="text" name="formContents" value="Example">
   <input type="Submit" value="Submit" name="submitbtn" onClick="myform.submitbtn.disabled=true;">
</form>

Linking: form_name . submit_button_name . disabled=true
i.e. myform . submitbtn . disabled=true;


Demo

Create PHP Application remotely on Netbeans

(Note: Click over an image to full screen)

⇛ Step 1: Go to File menu and click on New Project or press CTRL+SHIFT+N

⇛ Step 2: Select "PHP Application from Remote Server" on New Project window and click on "Next >" button.

PHP application remotely on Netbeans

⇛ Step 3: In next step enter the name and location of your PHP Application and click "Next >" button to move towards the next step.

PHP application remotely on Netbeans

⇛ Step 4: The next step is for configuration of remote server. Here you have to specify the way project's files will be deployed. Enter your project URL and click on "Manage" button for FTP settings.

PHP application remotely on Netbeans

⇛ Step 5: Click on "Add..." button on Manage Remote Connection window to create new connection. Enter the connection name and select connection type as FTP.

PHP application remotely on Netbeans

⇛ Step 6: Now enter your host name, username, password, initial directory (keep it as / i.e. root), timeouts, keep-alive intervals and mark "Passive Mode" option as checked. Click on "Test Connection" button to ensure that connection happened successfully. If it succeeds then click on "OK" button.

Note: It'll give an error if connection gets failed.

PHP application remotely on Netbeans

⇛ Step 7: Come back to previous window will show you the remote connection name. Enter your upload directory ex. /var/www and if you do not want to specify any directory then keep it as "/" i.e. root which is a default directory.

PHP application remotely on Netbeans

⇛ Step 8: Click on "Next" button will show you confirmation window and download all the files from FTP server into the Netbeans. Click on "Finish" button after completion of download and the project will be appeared on netbeans Projects window. Right Click on project name and select "properties" option.

⇛ Step 9: On "Project Properties" window select "Run Configuration" category. Here you can check the run configuration of your remote site. Select "On Save" option from "Upload Files" drop down field. It will follow all local changes i.e. create, update, rename and delete just on your CTRL+S.

PHP application remotely on Netbeans

Thanks. Have a nice day !!

Monday 22 April 2013

What is variable variables in PHP

Variable Variables are just variables whose name can be set and accessed by programmatically.


How to use variable variables?

Example:


<?php
$myVar = "hello";
?>
            

Now can you imagine, in the above example "hello" can be use as the name of a variable by using two dollor signs. i.e.

<?php
$$myVar = "world!!";
?>
            

Variable Variables takes the value of a variable and treat that as the name of variable. In the example above it is treating "hello" as a variable.

<?php

echo $myVar . "<br>";
//Output: hello

echo $hello . "<br>";
//Output: world!!

echo "$myVar $hello";
//Output: hello world!!

?>
            

If you don't want to use "hello" as a variable name (which is I think not a better way) then you can use double dollor sign ($$). i.e.

<?php

echo $myVar . "<br>";
//Output: hello

echo $$myVar . "<br>";
//Output: world!!

echo "$myVar ${$myVar}" . "<br>";
//Output: hello word!!
?>
            

For more reference visit PHP Manual: http://php.net/manual/en/language.variables.variable.php

Thanks. Have a great day!!

Wednesday 17 April 2013

Get tweet count by hashtag

Twitter API

http://search.twitter.com/search.json?q=your_hashtag&page=1

PHP Code

PHP code snippet for getting the Tweet data into the JSON format from Twitter API by using the CURL method.

<?php
if (isset($_POST['search_hash'])) {
    global $total, $hashtag;
    $hashtag = '#' . $_POST['search_hash'];
    $total = 0;

    function getTweets($hashtag, $page) {
        global $total;
        $url = 'http://search.twitter.com/search.json?q=' . urlencode($hashtag) . '&';
        $url .= 'page=' . $page;
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $json = curl_exec($ch);
        curl_close($ch);

        $json_data = json_decode($json);
        $total += count($json_data->results);
        if ($json_data->next_page) {
            $temp = explode("&", $json_data->next_page);
            $p = explode("=", $temp[0]);
            getTweets($hashtag, $p[1]);
        }
        return $json_data;
    }

    $tweet = getTweets($hashtag, 1);
}
?>

HTML Form

<form  method="POST" action="tweets.php">
    <input type="text" name="search_hash">
    <input type="submit" value="Search" name="submit"/>
</form>

Tweet count and tabular listing of user name and tweets

<h4>Total Tweet Count : <?php echo $total; ?></h4>
<table>
       <th>User Name</th>
       <th>Text</th>

       <?php foreach ($tweet->results as $data) : ?>
       <tr>
       <td><?php echo $data->from_user_name; ?></td>
       <td><?php echo $data->text; ?></td>
       </tr>
       <?php endforeach; ?>
</table>