I have a simple function, which I'm trying to turn turn into a query. The idea is to grab the number of facebook likes (which I've done), and pass it into a simple equation (which is ready) to work out the percentage of likes against a target number. I know that the php controling the percentage will return 0.0 at the moment - that's fine for now :) I just can't work out how to get from the function to a variable...
Code as follows:
<?php
function bfan() {
$pageID = 'VisitTywyn';
$info = json_decode(file_get_contents('http://graph.facebook.com/' . $pageID));
echo $info->likes;
} ?>
<?php bfan(); ?>
<?php
echo number_format(($num_amount/$num_total)*100, 1);
?>
Thanks for your help!
What you want to do is return your result so it can be passed to a variable.
<?php
function bfan() {
$pageID = 'VisitTywyn';
$info = json_decode(file_get_contents('http://graph.facebook.com/' . $pageID));
return $info->likes;
} ?>
<?php $something = bfan(); ?>
Related
my php function is
$exp3 = $_GET["url"];
echo $exp3;
This function gets me this link for example
"http://www.streamuj.tv/video/687aa15fe046f21cc1e3"
What do I need is to transform this function to get just the code of the url. In this case its 687aa15fe046f21cc1e3.
Can you please help? Thank you
You can use explode and get the last
$myArray= explode('/',$exp3 );
$my_Last = end($myArray);
echo $my_last;
You can use this:
$code = array_pop(explode('/', $exp3));
echo $code;
This block of PHP code prints out some information from a file in the directory, but I want the information printed out by echo to be used inside the HTML below it. Any help how to do this? Am I even asking this question right? Thanks.
if(array_pop($words) == "fulltrajectory.xyz") {
$DIR = explode("/",htmlspecialchars($_GET["name"]));
$truncatedDIR = array_pop($DIR);
$truncatedDIR2 = ''.implode("/",$DIR);
$conffile = fopen("/var/www/scmods/fileviewer/".$truncatedDIR2."/conf.txt",'r');
$line = trim(fgets($conffile));
while(!feof($conffile)) {
$words = preg_split('/\s+/',$line);
if(strcmp($words[0],"FROZENATOMS") == 0) {
print_r($words);
$frozen = implode(",", array_slice(preg_split('/\s+/',$line), 1));
}
$line = trim(fgets($conffile));
}
echo $frozen . "<br>";
}
?>
The above code prints out some information using an echo. The information printed out in that echo I want in the HTML code below where it has $PRINTHERE. How do I get it to do that? Thanks.
$("#btns").html(Jmol.jmolButton(jmolApplet0, "select atomno=[$PRINTHERE]; halos on;", "frozen on")
You just need to make sure that your file is a php file..
Then you can use html tags with php scripts, no need to add it using JS.
It's as simple as this:
<div>
<?php echo $PRINTHERE; ?>
</div>
Do remember that PHP is server-side and JS is client-side. But if you really want to do that, you can pass a php variable like this:
<script>
var print = <?php echo $PRINTHERE; ?>;
$("#btns").html(Jmol.jmolButton(jmolApplet0, "select atomno="+print+"; halos on;", "frozen on"));
</script>
When i tried to pass a value $abc to the IncludedFile.php, it doesnt go through. Neither does the $session work. Is there a way to pass a value to the IncludedFile.php? My ultimate aim is to get the calculation processed at the IncludedFile.php and then echo it on the Main.php
Main.php
<?php $abc = 'abc';
include_once ('IncludedFile.php');
echo $answer; ?>
IncludedFile.php
<?php if($abc=='abc') {$answer = 5;} ?>
I'm uning a php script to get a random news article from a directory and to display it on my website, this works fine, however when I try to recreate it to show something else at random, the new one doesn't work - Here is my code:
<?php
function random_unit($unit_dir = 'sections/units')
{
$units = glob($unit_dir . '/*.php');
$unit = array_rand($units);
return $units[$unit];
}
$unit_1 = random_unit("sections/units");
?>
<?php $unit_1 = file_get_contents( $unit_1 ); ?>
<?php echo $unit_1; ?> // Echo's The Contents Of Selected Random File
Can anyone see where I might have gone wrong?
A friend of mine wrote this script, displaying the 20 most recent instagram images, and I was wondering, how can I change the amount of images it grabs to maybe, 6?
<?PHP
$token = 'token';
$username = 'username';
$userInfo = json_decode(file_get_contents('https://api.instagram.com/v1/users/search?q='.$username.'&access_token='.$token));
if($userInfo->meta->code==200){
$photoData = json_decode(file_get_contents('https://api.instagram.com/v1/users/'.$userInfo->data[0]->id.'/media/recent/?access_token='.$token));
if($photoData->meta->code==200){ ?>
<?PHP foreach($photoData->data as $img){
echo '<img src="'.$img->images->thumbnail->url.'">';
} ?>
<?PHP } // If
} // If
?>
Now, the script is functional now because I've been working on it all day, but I'm not sure how to change how many it sends out.
Also, would any of you know how to style this? I already have the CSS done for it, but whenever I try it, it doesn't work correctly.
And, would you know how to get the description of the photo using the API?
Thank you in advance :-)
You need to use Instagram's count= url parameter when requesting data from their endpoints.
For example: https://api.instagram.com/v1/users/search?count=6
Or in your code:
<?PHP
$token = 'token';
$username = 'username';
$userInfo = json_decode(file_get_contents('https://api.instagram.com/v1/users/search?count=6&q='.$username.'&access_token='.$token));
if($userInfo->meta->code==200){
$photoData = json_decode(file_get_contents('https://api.instagram.com/v1/users/'.$userInfo->data[0]->id.'/media/recent/?count=6&access_token='.$token));
if($photoData->meta->code==200){ ?>
<?PHP foreach($photoData->data as $img){
echo '<img src="'.$img->images->thumbnail->url.'">';
} ?>
<?PHP } // If
} // If
?>
Pseudo example for styling. You'll need to figure out the css styles for that, but shouldn't be to difficult.
<div class='myBorder'>
<img url=$img->link />
<div class='myCaption'>$img->caption->text</div>
</div>
To get the description
if (isset($img->caption)) {
if (get_magic_quotes_gpc()) {
$title = stripslashes($img->caption->text);
} else {
$title = $img->caption->text;
}
}