Dear friends
i m a developer and making a MLM site i had a problem to show number of left and right nodes of a binary tree.
if more then 1000 data are there in the database then we can not bring all the count for each node.
so we had a solution for that we called an ajax script which would return the count for each when event of mouse-hover occurs.
script to find the count is here
find_counts.php
<?php
//echo "start TOT=".$total;
$memid=$app->getRequestVar("memid");
$total_other=0;
$total_right_ajax= get_tree($app->getRequestVar("memid"),'right');
//$app->assign("total_right_ajax", $total_right_ajax);
global $total_other;
$total_other=0;
//echo "after TOT=".$total;
$total_left_ajax= get_tree($app->getRequestVar("memid"),'left');
//$app->assign("total_left_ajax", $total_left_ajax);
function get_tree($memid,$leg){
$sql="select user_id from user
left join user_binary on user_binary .user_id=user.id
where user_binary.parent_id='".$memid."' and binary_position='".$leg."'";
// echo "<br>";
$res=mysql_query($sql);
global $total_other;
$total_other=$total_other+mysql_num_rows($res);
$row=mysql_fetch_array($res);
if($row['user_id']!=''){
get_tree ($row['user_id'],'left');
get_tree ($row['user_id'],'right');
}
return $total_other;
}
echo "LEFT: ".$total_left_ajax." RIGHT: ".$total_right_ajax;
?>
Now just write a ajax function to call find_counts.php and display it
if any query feel free to ask ..
Wednesday, May 7, 2014
How to create API with Json as output
Hi,Friends
How to create a API in PHP which gives a json output
First of all json is very light and secured way of getting data from an API. we will make an API to output a json
Here is a simple example
File: index.php
<?php
$url = 'http://localhost/jsonapi/json.php';//url for json
include('connect.php');//db connnection
$query = "SELECT * FROM category";
$result = mysql_query($query) or die("Query failed");
while ($row = mysql_fetch_array($result)) {
$record[]=array($row["id"],$row["categoryname"]);
}
//curl for secured transaction of data
$ch=curl_init($url);
$data_string = urlencode(json_encode($record));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, array("category"=>$data));
$result = curl_exec($ch);
curl_close($ch);
// echo $result;
?>
File: json.php
<?php
$data = $_POST['category'];
$output_data = ( urldecode( $datastring));
print_r($output_data);
?>
Function mainly used is json_encode
Now just run these file and you will get a json output for your API
just simple as that!!!!!!!!
How to create a API in PHP which gives a json output
First of all json is very light and secured way of getting data from an API. we will make an API to output a json
Here is a simple example
File: index.php
<?php
$url = 'http://localhost/jsonapi/json.php';//url for json
include('connect.php');//db connnection
$query = "SELECT * FROM category";
$result = mysql_query($query) or die("Query failed");
while ($row = mysql_fetch_array($result)) {
$record[]=array($row["id"],$row["categoryname"]);
}
//curl for secured transaction of data
$ch=curl_init($url);
$data_string = urlencode(json_encode($record));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, array("category"=>$data));
$result = curl_exec($ch);
curl_close($ch);
// echo $result;
?>
File: json.php
<?php
$data = $_POST['category'];
$output_data = ( urldecode( $datastring));
print_r($output_data);
?>
Function mainly used is json_encode
Now just run these file and you will get a json output for your API
just simple as that!!!!!!!!
Subscribe to:
Posts (Atom)