Enjoy unlimited access to all forum features for FREE! Optional upgrade available for extra perks.

PHP Problem

Status
Not open for further replies.
Joined
Feb 14, 2008
Posts
2,249
Reaction score
65
Hello,

My girlfriend has uni project to create a mash up site using API's.

She is looking to use an IMDB API which we have found and got it onto her site but we are having problems formating the string.

The string looks as follows:

http://www.deanclatworthy.com/imdb/?q=The+Green+Mile

We would like to be able to use printf $movie_title, printf $movie_year etc..

Does anyone know how we can format that output to set each variable?

Thanks in advance, Darren.
 
Hi Darren,

If the string is always the same (in terms of number of variables and their order within it) you could just

<?php
$movie = (the API Query);
//create an array by breaking at each comma'd section
$movie = explode(",". $movie);

//process the new array

foreach ($movie as &$value) {
//create a 2 part sub array for each value
$value = explode(":". $movie);
//strip the label by splitting at the : and assigning the value as the second half
$value=$value[1];
//remove the quotes
$value = str_replace('"',"",$value);
}
//Leaves you with an array of the form {The Green Mile,1999,http:\/\/www.imdb.com\/title\/tt0120689\/,USA,etc etc
//Set the variables

$movie_title=$movie[0];
$movie_year=$movie[1];
etc etc
?>

Then refer to the newly set variable as you wanted to.

I've just written this out cold, so excuse any syntax errors there might be, but this should be an easy way of parsing the IMDB output.

Regards

Ty
 
Haha, I was in the midst of writing the exact same thing ;)

Go with Ty_'s but I had made a start! I've not tried it.

PHP:
<?

$string = '{"title":"The Green Mile","year":"1999","imdburl":"http:\/\/www.imdb.com\/title\/tt0120689\/","country":"USA","languages":"English,French","genres":"Crime,Drama,Fantasy","rating":"http:","votes":"179354","usascreens":"2875","ukscreens":"340"}';

$items = explode(',',$string);

foreach($items as $item){
	$temp = explode(':',$item);

	$key = $temp[0];
	$var = $temp[1];
	
	$key = str_replace("{","",$key);
	$key = str_replace('"','',$key);
	
	$var = str_replace("{","",$var);
	$var = str_replace('"','',$var);
	$var = str_replace(',','',$var);
	
	$array[$key] = $var;
	
}


?>
 
Hello Everyone,

I am the girlfriend with the Php Problem. I have tried using the JSON, I think I need to elaborate on it as it isn't doing anything different at the moment.

Thanks.
 
Hello Everyone,

I am the girlfriend with the Php Problem. I have tried using the JSON, I think I need to elaborate on it as it isn't doing anything different at the moment.

Thanks.

UH OH! my one place of freedom now gone :(
 
Last edited:
Here's an example:

PHP:
$string = '{
    "title":"The Green Mile",
    "year":"1999",
    "imdburl":"http:\/\/www.imdb.com\/title\/tt0120689\/",
    "country":"USA",
    "languages":"English,French",
    "genres":"Crime,Drama,Fantasy",
    "rating":"http:",
    "votes":"179354",
    "usascreens":"2875",
    "ukscreens":"340"
}';

$obj = json_decode($string);
printf('<a href="%s" title="%s">%s (%d)</a>', $obj->imdburl, $obj->title, $obj->title, $obj->year);

// prints <a href="http://www.imdb.com/title/tt0120689/" title="The Green Mile">The Green Mile (1999)</a>
 
Hi,

Thanks for the code.

I have tried to implement it but it doesnt display what you said here is my code:


<form action='trial.php' method='GET'>

<input type="text" name="search_term"/>
<input type="submit" value="Find"/>

</form>

<?php
// the if statement if there is nothing in the search box
if(empty($_GET['search_term']))
{
print "This must not be left blank!";
exit(0);
}
$search = $_GET['search_term'];

$movie = ("http://www.deanclatworthy.com/imdb/?q=");
$movie = explode(",". $movie);

$string = '{
"title":"The Green Mile",
"year":"1999",
"imdburl":"http:\/\/www.imdb.com\/title\/tt0120689\/",
"country":"USA",
"languages":"English,French",
"genres":"Crime,Drama,Fantasy",
"rating":"http:",
"votes":"179354",
"usascreens":"2875",
"ukscreens":"340"
}';

$obj = json_decode($string);
printf('<a href="%s" title="%s">%s (%d)</a>', $obj->imdburl, $obj->title, $obj->title, $obj->year);

I have loads of other code that didn't work that is commented out.
 
Just to mention that there's also a helpful PHP coding forum at PHP Freaks.

Although I'm sure some of the members here are quite handy with PHP too ...

Rgds
 
Here is another version of the code which I have been using and this does the same output as the one underneath it.

<form action='trial.php' method='GET'>

<input type="text" name="search_term"/>
<input type="submit" value="Find"/>

</form>

<?php
// the if statement if there is nothing in the search box
if(empty($_GET['search_term']))
{
print "This must not be left blank!";
exit(0);
}
$search = $_GET['search_term'];

//Url for imdb
$url = file_get_contents("http://www.deanclatworthy.com/imdb/?q=".urlencode($search));
$data =($url);

print $data;

2nd Version:



<form action='trial.php' method='GET'>

<input type="text" name="search_term"/>
<input type="submit" value="Find"/>

</form>

<?php
// the if statement if there is nothing in the search box
if(empty($_GET['search_term']))
{
print "This must not be left blank!";
exit(0);
}
$movie = $_GET['search_term'];

$movie = ("http://www.deanclatworthy.com/imdb/?q=");
$movie = explode(",". $movie);

$string = '{
"title":"",
"year":"",
"imdburl":"",
"country":"",
"languages":"",
"genres":"",
"rating":"",
"votes":"",
"usascreens":"",
"ukscreens":""
}';

$obj = json_decode($string);
printf('<a href="%s" title="%s">%s (%d)</a>', $obj->imdburl, $obj->title, $obj->title, $obj->year);

Any help would be much appreciated.
 
Works

I have just fiddled around with the code and at the moment it creates a link when the movie is searched and when you press the link it redirects you to the imdb web page!

I still need to add another api but at the moment it works. Thank you.
 
Query

Hi,

I just need to know exactly what this does:

$obj = json_decode($url);
printf('<a href="%s" title="%s">%s (%d)</a>', $obj->imdburl, $obj->title, $obj->title, $obj->year);

I know that it decodes the json into a php format. It does work which is great but if I want to add more details about the movie e.g. country I add that field as $obj->country after $obj->year and nothing appears?
 
You will need to add another place holder in the first argument to the printf query. %s is the place holder for a string.
 
Tables

Hi,

Does anyone know how to display the json output into a table?

I have tried some methods but it does not work.

Thanks alot everyone that has helped with the coding.
 
Hi,

Does anyone know how to display the json output into a table?

I have tried some methods but it does not work.

Thanks alot everyone that has helped with the coding.

The following two links may help:

This is very basic and not very useful:
Creating and Parsing JSON data with PHP

If you can implement this class could be useful:
Table Constructor (html table) - PHP Classes

This class can be used to display an HTML table from array, JSON or XML.

It can traverse data from an array and generate an HTML table that displays that data.

It can also read data from a JSON or XML file and convert it into an array so it can also be displayed in an HTML table.

1. Simply to use
new Table ($data); // work with array
new Table ('data.json', 'json-file');
new Table ($json_string, 'json'); // work with json-string
new Table ('data.xml', 'xml-file'); // work with xml-file
new Table ($xml_string, 'xml'); // work with xml-string
2. Universal: array, xml, json
3. Quick
 
Hi,

Thanks a lot. Here is the code that I am working with:

$obj = json_decode($url);
printf('<a href="%s" title="%s">%s (%d) %s %s</a>', $obj->imdburl, $obj->title, $obj->title, $obj->year, $obj->country, $obj->genres);

I presume that I will need the

new Table ('data.json', 'json-file');
 
More problems!

Hi,

I have been trying to do this for ages now....here is the code I have got.

$obj = json_decode($url);

printf('%s (%d) %s %s %s', $obj->title, $obj->year, $obj->country, $obj->genres, $obj->rating);
printf('<a href="%s"> %s'</a>, $obj->imdburl);

I want to display the first printf without linking to the url, I want this information to be displayed in a table with html or php. With the 2nd printf I want a link called 'More Info' to take the user to the imdb page on the film that is searched.

Originally I had:

printf('<a href="%s" title="%s">%s (%d)</a>', $obj->imdburl, $obj->title, $obj->title, $obj->year);

This made the whole content hperlinked and when pressed it took the user to the imdb site with info about the film searched.

I probably haven't described it clearly but if anyone knows how to do this it would help a lot.

Thanks.
 
This wont be exactly right, but should put you on track! No more help from me after this, you'll learn the most by finding your own way!

PHP:
<?php

$obj = json_decode(file_get_contents($url));

?>
<table>
    <tr>
        <th>Title</th>
        <th>Year</th>
        <th>Country</th>
        <th>Genres</th>
        <th>Rating</th>
        <th>&nbsp;</th>
    </tr>
    <tr>
        <td><?php echo htmlentities($obj->title);?></td>
        <td><?php echo htmlentities($obj->year);?></td>
        <td><?php echo htmlentities($obj->country);?></td>
        <td><?php echo htmlentities($obj->genres);?></td>
        <td><?php echo htmlentities($obj->rating);?></td>
        <td><a href="<?php echo htmlentities($obj->imdburl);?>">More info</a></td> 
    </tr>
</table>
 
Status
Not open for further replies.

The Rule #1

Do not insult any other member. Be polite and do business. Thank you!

Featured Services

Sedo - it.com Premiums

IT.com

Premium Members

AucDom
UKBackorder
Be a Squirrel
Acorn Domains Merch
MariaBuy Marketplace

New Threads

Domain Forum Friends

Other domain-related communities we can recommend.

Our Mods' Businesses

Perfect
Service
Laskos
*the exceptional businesses of our esteemed moderators
Top Bottom