PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: print_r
print_r - phponwebsites.com
Showing posts with label print_r. Show all posts

13 Oct 2014

Parse / read data from XML file using PHP

                                 You can get value from XML using PHP in following two ways.

1. Get data from XML using simple_load_file() in PHP
2. Get data from XML using file_get_contents() and SimpleXMLElement() in PHP

Lets see below examples.
 If the XML file looks like below, then you can retrieve node values from XML file using following two ways.

<?xml version="1.0" encoding="utf-8"?>
 <userlist>
    <user>
       <name>Clark</name>
       <state>California</state>
       <country>USA</country>
   </user>
   <user>
      <name>Smith</name>
      <state>London</state>
      <country>United Kingdom</country>
   </user>
   <user>
      <name>Nathan</name>
      <state>Franche Comte</state>
      <country>France</country>
   </user>
   <user>
      <name>Nastya</name>
      <state>Moscow</state>
      <country>Russia</country>
   </user>
</userlist>

Get data from XML  using simple_load_file() in PHP


                                   Simple_load_file() in PHP is used to convert XML document into an object. The below example explain retrieve data from XML file.


<?php
$xml=simplexml_load_file('sample.xml');
 //print_R($xml);
 echo'<table><tr><th>Name</th><th>State</th><th>Country</th></tr>';
 foreach($xml as $user)
 {
  echo'<tr><td>'.$user->name.'</td><td>'.$user->state.'</td><td>'.$user->country.'</td></tr>';
 }
 echo'</table>';
?>

Get data from XML file using children() with simplexml_load_file() in PHP

          
                       The children() find the children of the specific node. Consider below example to parsing XML values using PHP.


<?php
 $xml=simplexml_load_file('sample.xml');
 //print_R($xml);
 echo'<table><tr><th>Name</th><th>State</th><th>Country</th></tr>';
 foreach($xml->children() as $user)
 {
  echo'<tr><td>'.$user->name.'</td><td>'.$user->state.'</td><td>'.$user->country.'</td></tr>';
 }
 echo'</table>';
?>

Get data from XML using file_get_contents and SimpleXMLElement in PHP


                                  File_get_contents() is used to read the contents of a file into string. SimpleXMLElement is used to represents elements of XML document. The below example explain retrieve data from XML file.

<?php
$url=file_get_contents('sample.xml');
 $xml=new SimpleXMLElement($url);
// print_R($xml);
 echo'<table><tr><th>Name</th><th>State</th><th>Country</th></tr>';
 foreach($xml as $user)
 {
  echo'<tr><td>'.$user->name.'</td><td>'.$user->state.'</td><td>'.$user->country.'</td></tr>';
 }
 echo'</table>';
?>


 When you give print_R() in above PHP file, you can see the output on screen like below.


  SimpleXMLElement Object (
  [user] => Array (
     [0] => SimpleXMLElement Object (
   [name] => Clark
[state] => California
[country] => USA )
[1] => SimpleXMLElement Object (
   [name] => Smith
[state] => London
[country] => United Kingdom )
[2] => SimpleXMLElement Object (
   [name] => Nathan
[state] => Franche Comte
[country] => France )
[3] => SimpleXMLElement Object (
   [name] => Nastya
[state] => Moscow
[country] => Russia )
 )
)



                You can get output as below

NameStateCountry
ClarkCaliforniaUSA
SmithLondonUnited Kingdom
NathanFranche ComteFrance
NastyaMoscowRussia
 
Now you can retrieve data from XML file. You can parsed data from XML using PHP.

8 Oct 2014

Parse / Retrieve data from json file using php

                       You know how to create JSON file using PHP. Then we are going know how to get data from json file.

                       That means,  The json file returns encoded values. You have to decode it to display at wherever you wants. You can get data string from JSON using php.

                       The JSON file returns value as an array format, object format or combined both. See my previous post How to create JSON file using PHP.
                       In previous post, It give output like this:

       [    
           {
              'tutorial':'PHP',
              'link':'http://www.phponwebsites.com/p/php.html'
           },
           {
              'tutorial':'Mysql',
              'link':'http://www.phponwebsites.com/p/mysql.html'
           },
           {
            'tutorial':'htaccess',
            'link':'http://www.phponwebsites.com/p/htaccess.html'
           }    
      ]

Get data from JSON file using curl in PHP


               Now, you retrieve data from JSON file using PHP. Consider the following PHP script for retrieve data from JSON file.

  <?php

    $ch = curl_init("http://www.phponwebsites.com/json.php"); // add your url which contains json file
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $content = curl_exec($ch);
    curl_close($ch);
    $json = json_decode($content, true);
    //print_R($json);
    $count=count($json);
    echo'<table><th>Tutorial</th><th>Link</th>';
    for($i=0;$i<$count;$i++)
    {
      echo'<tr><td>'.$json[$i]['tutorial'].'</td><td>'.$json[$i]['url'].'</td></tr>';
    }
    echo'</table>';

?>

      where,
                  json_decode() - takes JSON encoded values and converts it to PHP variable
If you want to know about cURL, then visit php.net

Get data from JSON file using file_get_contents in PHP



 <?php
    $content=file_get_contents("http://www.phponwebsites.com/json-api.php");  // add your url which contains json file
    $json = json_decode($content, true);
   // print_R($json);
    $count=count($json);
    echo'<table><th>Tutorial</th><th>Link</th>';
    for($i=0;$i<$count;$i++)
    {
      echo'<tr><td>'.$json[$i]['tutorial'].'</td><td>'.$json[$i]['url'].'</td></tr>';
    }
   echo'</table>';
  ?>

           
       where,
                   file_get_contents() - reads whole values in file into a string

              When you print_r() the results, you will get output like this:

Array (
         [0] => Array ( [PHP] => Differences The Ranch House
                               [link] => http://www.phponwebsites.com/p/php.html
                             )
         [1] => Array ( [Mysql] => Kids Bus Kiss
                               [link] => http://www.phponwebsites.com/p/mysql.html
                            )
         [2] => Array ( [htaccess] => Beehive Hideout
                                [link] => http://www.phponwebsites.com/p/htaccess.html
                           )
          )

Finally, you will get output like this:

TutorialLink
PHPhttp://www.phponwebsites.com/p/php.html
Mysqlhttp://www.phponwebsites.com/p/mysql.html
htaccesshttp://www.phponwebsites.com/p/htaccess.html


Related Post:
Create XML file using PHP and MySQL
Create Excel file using PHP and MySQL
Create Sitemap using PHP and MySQL
Create RSS Feed using PHP and MySQL
Get / Parse data from XML Sitemap using PHP
Read data from RSS Feed using PHP
Parse data from XML file using PHP

25 Aug 2014

Parse/Read data from XML Sitemap using PHP

                                You can Create XML Sitemap dynamically using PHP and MySQL. Then you've have a doubt. How can retrieve data from XML Sitemap using PHP? In this post, You are going to know get data from XML Sitemap using PHP.

                                 You can get value from XML Sitemap using PHP in following two ways.

1. Get data from XML Sitemap using simple_load_file() in PHP
2. Get data from XML Sitemap using file_get_contents() and SimpleXMLElement() in PHP

Lets see below examples.

Get data from XML Sitemap using simple_load_file() in PHP


                                   Simple_load_file() in PHP is used to convert XML document into an object. The below example explain retrieve data from XML Sitempa.


<?php
$xml=simplexml_load_file('http://www.phponwebsites.com/sitemap.xml');
//print_R($xml);
foreach($xml->url as $val)
 {
  echo $val->loc.' '. $val->lastmod.' '. $val->changefreq.' '. $val->priority.'<br>';
 }
?>


Get data from XML Sitemap using file_get_contents and SimpleXMLElement in PHP


                                  File_get_contents() is used to read the contents of a file into string. SimpleXMLElement is used to represents elements of XML document. The below example explain retrieve data from XML Sitemap.

<?php
$url=file_get_contents('http://www.phponwebsites.com/sitemap.xml');
$xml=new SimpleXMLElement($url);
//print_R($xml);
foreach($xml->url as $val)
 {
  echo $val->loc.' '. $val->lastmod.' '. $val->changefreq.' '. $val->priority.'<br>';
 }
?>


 When you give print_R() in above PHP file, you can see the output on screen like below.


 SimpleXMLElement Object (
   [url] => Array (
         [0] => SimpleXMLElement Object (
       [loc] => http://www.phponwebsites.com/p/php.html
           [lastmod] => 2014-07-26
       [changefreq] => daily
       [priority] => 1 )
 [1] => SimpleXMLElement Object (
       [loc] => http://www.phponwebsites.com/p/mysql.html
       [lastmod] => 2014-07-26
       [changefreq] => daily
       [priority] => 1 )
 [2] => SimpleXMLElement Object (
       [loc] => http://www.phponwebsites.com/p/htaccess.html
       [lastmod] => 2014-07-26
       [changefreq] => daily
       [priority] => 1 )
  )
)

Now you can retrieve data from XML Sitemap.

Related Post:
Create Excel file using PHP and MySQL
Create XML Sitemap dynamically using PHP and MySQL
Create RSS Feed Dynamically using PHP and MySQL
PHP : Parse data from RSS Feed using SimpleXML
Read data from JSON file using PHP
Parse data from XML file using PHP

20 Aug 2014

Get/Read value from RSS Feed using PHP

                                You can generate RSS Feed using PHP and MySQL. Then you've have a doubt. How can retrieve data from RSS Feed using PHP? In this post, You are going to know get data from RSS Feed using PHP.

                                 You can get value from RSS Feed using PHP in following two ways.

1. Get data from RSS Feed using simple_load_file() in PHP
2. Get data from RSS Feed using file_get_contents() and SimpleXMLElement() in PHP

Lets see below examples.

Get data from RSS Feed using simple_load_file() in PHP


                                   Simple_load_file() in PHP is used to convert XML document into an object. The below example explain retrieve data from RSS Feed.


<?php
$xml=simplexml_load_file('http://www.phponwebsites.com/rss.xml');
//print_R($xml);
foreach($xml->channel->item as $val)
 {
  echo $val->title.' '. $val->link.'<br>';
 }
?>


Get data from RSS Feed using file_get_contents and SimpleXMLElement in PHP


                                  File_get_contents() is used to read the contents of a file into string. SimpleXMLElement is used to represents elements of XML document. The below example explain retrieve data from RSS Feed.

<?php
$url=file_get_contents('http://www.phponwebsites.com/rss.xml');
$xml=new SimpleXMLElement($url);
//print_R($xml);
foreach($xml->channel->item as $val)
 {
  echo $val->title.' '. $val->link.'<br>';
 }
?>


 When you give print_R() in above PHP file, you can see the output on screen like below.


  SimpleXMLElement Object (
   [@attributes] => Array ( [version] => 2.0 )
   [channel] => SimpleXMLElement Object (
         [title] => Phponwebsites
[link] => http://www.phponwebsites.com
[description] => Phponwebsites about php related topics like .htaccess, robots.txt, mysql, jquery, ajax, js and also php for php developers
         [language] => en-us
[item] => Array (
          [0] => SimpleXMLElement Object (
            [title] => PHP
  [link] => http://www.phponwebsites.com/p/php.html
               [description] => PHP doubts clarified at Phponwebsites
                )
         [1] => SimpleXMLElement Object (
            [title] => MySQL
           [link] => http://www.phponwebsites.com/p/mysql.html
               [description] => MySQL doubts clarified at Phponwebsites
               )
           [2] => SimpleXMLElement Object (
           [title] => htaccess
          [link] => http://www.phponwebsites.com/p/htaccess.html
              [description] => htaccess doubts clarified at Phponwebsites
               )
          )
    )
)

Now you can retrieve data from RSS Feed.

Related Post:
Create Sitemap dynamically using PHP and MySQL
Create RSS Feed dynamically using PHP and MySQL
PHP : Create json file
Create XML file using PHP and MySQL
PHP : Parse data from XML Sitemap using simplexml_load_file and file_get_elements
Read data from JSON file using PHP
Parse data from XML file using PHP

23 Jun 2014

How to store array values into session in php

                You can store single value to session in PHP easily. Visit store values to session in php.


Store array values to session in PHP:


                          You can also store multiple values into session using PHP. You can store one or more than one values in array that can be stored into session. Consider the following example. It describes the array values directly stored into session in PHP. The following PHP script is used to store values to session in PHP.

<?php
 session_start();
 $ar=array('php','mysql','jquery');
 $_SESSION['arr']=$ar;
 $session=$_SESSION['arr'];
 foreach($session as $val)
 {
   echo $val.'<br>';
 }
 ?>

 where,
            session_start() - start the session
            $ar=array() - array variable

                  Now you can get output like this:

                                    php
                                    mysql
                                    jquery

Store multiple (array) values into session in PHP on form submit:

                          Consider the following example:


 <html>
 <body>
 <?php
 session_start();
 error_reporting('E_ALL ^ E_NOTICE');
 if($_POST['submit'])
 {
  if(count($_SESSION['arr'])==0)
  {
   $ar=array();
   $val=$_POST['value'];
   array_push($ar,$val);
   $_SESSION['arr']=$ar;
   print_R($_SESSION['arr']);
  }
  else
  {
   $val=$_POST['value'];
   array_push($_SESSION['arr'],$val);
   print_R($_SESSION['arr']);
  }
 }
?>
 <form action="#" method="post">
  <input type="text" name="value">
  <input type="submit" name="submit" value="Submit">
 </form>
 </body>
 </html>


                    The above example describes array values are storing into session in PHP while form submitting.
            where,
                      if(count($_SESSION['arr'])==0) {} - check  whether the array contains values or not. Create array variable and store value into array, then value is stored into session if there is no values in session. Otherwise it directly add values into session.
                     array_push() - add values to array in PHP.

Related Post: