Powered By
Showing posts with label Php. Show all posts
Showing posts with label Php. Show all posts

Load Youtube toprated videos using simplexml_load_file PHP

View Comments
add to del.icio.us
saved by 0 users
  In this post I will show you how to fetch the you tube top rated videos just with built in PHP functions instead of using zend data api libraries.  Zend data api is the best and optimized way to do this task but rather than including and working with zend and zend data api you can just do this with php built-in functions by reading the you tube feeds for the top rated videos.

 So here we go.
General feed url of youtube is

http://gdata.youtube.com/feeds/api/standardfeeds/FEED_IDENTIFIER 
 
Youtube feeds URL for the top rated videos is
http://gdata.youtube.com/feeds/api/standardfeeds/top_rated

Here you can replace top_rated with




Fore more info regarding this

http://code.google.com/apis/youtube/2.0/developers_guide_php.html

So we have to read the feeds from the above url, then you can display it where ever you want.

We have the top rated videos as xml so we need to parse it using php function "simplexml_load_file() " .
To do this in php.ini we need to enable the url include option to On if it is not already turned on.

Get the feeds in a variable

                     $url="http://gdata.youtube.com/feeds/api/standardfeeds/top_rated";
                     $comments =simplexml_load_file($url); // Php built in function


So we have  top rated videos in $comments  , we need to parse it using foreach and display it in the following way.

 Here the feed is in yahoo feed format so you to mention the feed as yahoo and then you can parse the media group under entry tag.

      $media = $video->children('http://search.yahoo.com/mrss/');
So from this we can easily parse all the data from the youtube feeds.

Download the source for your reference.





You can also do all other functionality like searching a video or displaying particular video comments, recent videos and more using the above method. Its very easier than the zend data api. Try it out. Leave the comments if you have any problem
Read More

Previous & Next Functionality with PHP, AJAX, JQuery and MySQL

View Comments
add to del.icio.us
saved by 0 users
Here i have designed a very simple application to fetch data from mysql server with php, through Ajax. In this application i have used two image input component for previous and next functionality and a table to display the data. Here is the preview of the application.
On clicking the next button, next set of data with the specified limit is fetched from the database through Ajax and displayed in the table, vice versa for the previous button. The limit parameter can be changed at any time by changing the limit value.


You can download the application:
In db.php file, you can change the limit of data to fetch from the server. The design goes in the index.php file and the core of the application lies in the prevnext.js file.

On clicking the next button, the ajax function is called through this JQuery function

$("#next").click(function(){
var current = parseInt($("#currenthid").val()) + limit;
var data="current="+current;
$.ajax({
type:"POST",
url:"ajaxpost.php",
data:data,
cache:false,
success:function(html){
$("#showtable").html(html);
$("#currenthid").val(current);
checkVisible();
}
});
});
It sends the index of the next set of data to fetch, to the ajax function. On success the data in the view is updated and the index of current data set is updated. The checkVisible function is to ensure the next previous functionality. i.e, the Next button will be disables on reaching the last data set and the previous button will be disable on reaching the first data set.
  function checkVisible(){
var current = parseInt($("#currenthid").val());
var total = parseInt($("#totalhid").val());
$("#previous").css('display','block');
$("#next").css('display','block');
if(current <>= total) {
$("#next").css('display','none');
}
}
In the ajaxpost file the MySql database fetch is executed.

$limitquery = "select id,name,category,price,discount from previousnext limit ".(int)$_REQUEST['current'].",".$limit;
$result = mysql_query($limitquery, $dbHandle);
The data send from sql server is parsed in the php file and send to the client.
while($row = mysql_fetch_array($result)){
echo "
".$row['id']."
".$row['name']."
".$row['category']."
".$row['price']."
".$row['discount']."
";
}
In the next post i have planned to expand this application to have pagination. Donot forget to comment on my ideas.
Read More

Steps to Install and Connect to PHP in Flex using AMFPHP

View Comments
add to del.icio.us
saved by 0 users
If you are bored of using XML to transfer data from PHP to Flex, then here is a end for your boredom. Using AMFPHP, you can convert data object in PHP to dataobject in action script so that you can use that in your flex project.

Download Source:

Here are the steps to install and integrate AMFPHP with Flex.
  1. First of all download AMFPHP, depending upon the platform and os you are using. Then unzip the downloaded file,rename it to amfphp and place it in the root folder of your web server.
  2. To make sure amfphp is working correctly, go to http:/localhost/{amfphp}/gateway.php if you are working in local or use your server name instead of localhost.
  3. If you get the success message, then go ahead or look into the documentation for additional configurations.
  4. Then create a new flex project, with server type as PHP. Enter your valid web root and base url.
  5. Create a file called service-config.xml and save in the src folder. You can get the content of this xml file from the download source package.
  6. In the main mxml file, the data object from php is received through RemoteObject class in flex as

    < mx:remoteobject id="amf" source="AMFPHPConnect.Connect" destination="amfphp" > < mx:method name="testConnection" result="onConnect(event)" > </mx:method > </mx:remoteobject >
  7. Don't forget the give the same name for remote object destination and service-config.xml destination id.
  8. Give the correct path for the gateway.php file in the channels tag.

    <channels>
    <channel-definition id="my-amfphp" class="mx.messaging.channels.AMFChannel">
    <endpoint uri="http://{webroot}/amfphp/gateway.php" class="flex.messaging.endpoints.AMFEndpoint"/>
    </channel-definition>
    </channels>
  9. Create a simple php file and place it inside the 'services' folder inside amfphp installation. Under the a folder 'AMFPHPConnect' and name the php file 'Connect.php'.
  10. So that you can give the source for the RemoteObject class as 'AMFPHPConnect.Connect'.
  11. To call a method in the php file use <method> tag as
    <mx:method name="testConnection" result="onConnect(event)" />
  12. Before compiling the project, you must intimate the compiler that you are using amfphp, by specifying the compiler argument as shown.

  13. Then the output will be something like this:


Read More

Need php script to import csv/excel file into Mysql

View Comments
add to del.icio.us
saved by 0 users





Using a script to parse and import the file


If you need to take a csv or other delimited data file and import it INTO MySQL, here is a php script that can do it for you:








$connection = mysql_connect("localhost", "test", "test") or die ("Unable to connect to server");
 $db = mysql_select_db("test", $connection) or die ("Unable to select database");

 // first get a mysql connection as per the FAQ

 $fcontents = file ('./spreadsheet.xls');
 // expects the csv file to be in the same dir as this script


  for($i=0; $i<sizeof($fcontents); $i++) { 
        $line = trim($fcontents[$i]); 
  $arr = explode("\t", $line);
  //if your data is comma separated  instead of tab separated, 
  // change the '\t' above to ','

  $sql = "insert into TABLENAME values ('".implode("','", $arr) ."')";
  mysql_query($sql);
  echo $sql ."\n";
   if(mysql_error()) {
    echo mysql_error() ."\n";
   }
 }
?>








And you can get the source from here




Get It and enjoy....

Note: 
Upload a spreadsheet file into the same directory as this script. Then you edit this script to put in the correct table name instead of "TABLENAME".





Read More