Could someone assist me in helping me display more than 1/2 results?
Here is my code:
$url = "http://otter.topsy.com/search.json?q=debt%20management&window=a&perpage=10";
$jsonfile = file_get_contents($url);
$obj = json_decode($jsonfile);
foreach($obj as $result) {
echo $obj->response->list[0]->trackback_permalink;
echo "<br />";
echo $obj->response->list[0]->trackback_author_nick;
echo "<br />";
echo $obj->response->list[0]->content;
echo "<br /><br />";
}
?>
*Note: I have taken out my API key.
Using that code it shows two of the same results.
Anyone got a solution?
You iterate over $obj which is the top-level object containing two elements (request and response). Since you probably want to iterate over the response list, this is what you need:
foreach($obj->response->list as $result) {
echo $result->trackback_permalink;
echo "<br />";
echo $result->trackback_author_nick;
echo "<br />";
echo $result->content;
echo "<br /><br />";
}
Ah, just saw it:
remove the $obj++ ! You increment twice during each loop run. Once by the foreach() loop iterating itself, and once by manually iterating.
Related
I am newer to PHP and I am able to get the desired output but I am doing it one index position at a time. I am returning data from a .txt file and I need to insert this data into an HTML table I am creating using PHP. BUT FIRST I need to be able to get the same output without typing out every index position. I tried to use a forloop but it kept outputting only one line.
I manually outputted the lines from the file and it works. What loop in PHP would be best to achieve the same results and output these elements? IMPORTANT, as is I am able to sort and rsort (I want to be able to do this so if it can be implemented in the loop that would be awesome) any help is more than I have right now.
PHP
$books = array();
if ($fp)
{
while(true)
{
$lines_in_file = count(file($filename));
$line = fgets($fp);
if (feof($fp))
{
break;
}
$line_ctr++;
list($title, $author, $pubdate, $isbn) = explode('*', $line);
$new_line = explode('*', $line);
for($ii= 1; $ii <= $lines_in_file; $ii++){
$lines = fgets($fp); //Read each line
$member = trim($lines);
array_push($books, $member);
}
//This foreach only brings back the first like in the txt file, why?
$cntr = 0;
foreach($books as $element){
$cntr++;
$table .= "<tr>";
$table .= "<td>".$title."</td>";
$table .= "<td>".$author."</td>";
$table .= "<td>".$pubdate."</td>";
$table .= "<td>".$pubdate."</td>";
$table .= "<td>".$isbn."</td>";
$table .= "</tr>\n"; //added newline
echo $element;
}
//sort($books);
// rsort($books);
echo $books[0];
echo "<br>";
echo $books[1];
echo "<br>";
echo $books[2];
echo "<br>";
echo $books[3];
echo "<br>";
echo $books[4];
echo "<br>";
echo $books[5];
echo "<br>";
echo $books[6];
echo "<br>";
echo $books[7];
echo "<br>";
echo $books[8];
echo "<br>";
echo $books[9];
echo "<br>";
echo $books[10];
echo "<br>";
echo $books[11];
echo "<br>";
echo $books[12];
echo "<br>";
echo $books[13];
echo "<br>";
echo $books[14];
echo "<br>";
echo $books[15];
echo "<br>";
echo $books[16];
echo "<br>";
echo $books[17];
}//END WHILE LOOP
fclose($fp ); //Close file
}
Having to make a few guesses here but i believe the file is going to look like:
title*author*pubdate*isbn
title*author*pubdate*isbn
title*author*pubdate*isb
if this is wrong, let me know
as long as the fie is not to large read it in to an array:
$book_array=file('book_file.txt');
//print_r($book_array); //is it an array of the books, one book per array key
now to separate each line:
foreach($book_array as $line){
$line_sep=explode('*',$line);
// with no sorting option you could just echo inside the loop
//if you want to keep sorting options we have to keep the separated lines
$new_book_array[]=$line_sep;
}
unset($book_array);//free up memory if needed
//print_r($new_book_array);//is it a multi-d array, book then the 4 elements
to sort our new multidimensoanl array:
usort($new_book_array, function($a, $b) {
return strcmp($a['0'], $b['0']);;
}); //this sorts on key 0 in your case title:
//print_r($new_book_array); // has it sorted properly
for display:
loop again
echo '<table><tr><th>Title</th><th>Author</th><th>Pub Date</th><th>ISBN</th></tr>';
foreach($new_book_array as $book){
//print_r($book); //is it each indervidual book array with 4 elements
echo '<tr><td>'.$book[0].'</td><td>'.$book[1].'</td><td>'.$book[2].'</td><td>'.$book[3].'</td></tr>';
}
echo '</table>';
I am new to PHP, I have some data from API in json format.
In php i tried
$response = \Httpful\Request::get($uri)->send();
$json = json_decode($response, true);
foreach($json as $k=>$val):
echo '<b>Name: '.$k.'</b></br>';
endforeach;
echo $response;
echo "<br />";
print_r($json);
echo "<br />";
echo ($json["genres"][0]);
But I am unable to read the array content. Can someone please help me how to parse this PHP array in a loop. Is there any php library to simplify this?
You can try to use the foreach loop in PHP as follows, which worked for me.
foreach(json_data as $key => $val){
echo $key . ":" . $val;
}
I hope it helps.
firstly you wrongly used php's foreach, second you have to use $json['genres'] to access the inner array
foreach($json['genres'] as $k=>$val)
{
echo $k.':'.$val['id'].'_'.$val['name'];
}
How do I echo the 4 at the bottom?
$request_body = '{"id":8801236,"order_id":"1854071","accepted":true,"type":"Payment","text_on_statement":null,"branding_id":null,"variables":{},"currency":"USD","state":"pending","operations":
[{"id":1,"type":"authorize","amount":8996,"pending":false,"qp_status_code":"20000","qp_status_msg":"Approved","aq_status_code":"000","aq_status_msg":"Approved","data":
{},"callback_url":"http://www.mywebsite/callback.php","callback_success":true,"callback_response_code":"200","created_at":"2015-11-11T13:32:22+00:00"},
{"id":2,"type":"capture","amount":8863,"pending":true,"qp_status_code":null,"qp_status_msg":null,"aq_status_code":null,"aq_status_msg":null,"data":
{},"callback_url":null,"callback_success":null,"callback_response_code":null,"created_at":"2015-11-11T14:37:18+00:00"}],"metadata":
{"type":"card","brand":"visa","last4":"0008","exp_month":1,"exp_year":2019,"country":"US","is_3d_secure":false,"hash":"fdsfsdfsdf4ds65f4dsf65ds4"
,"number":null,"customer_ip":"8.1.1.21","customer_country":"US","fraud_suspected":false,"fraud_remarks":
[]},"link":null,"shipping_address":null,"invoice_address":null,"test_mode":true,"acquirer":"nets","facilitator":null,"created_at":"2015-11-11T13:32:13Z","balance":0}';
$request_array = json_decode($request_body, TRUE);
echo $request_array['qp_status_code']."<br />";
echo $request_array['qp_status_msg']."<br />";
echo $request_array['aq_status_code']."<br />";
echo $request_array['aq_status_msg']."<br />";
I have tried to do a print_r on the request_array, but honestly that only confuses me more. I simply can not see what array these variable lies within. I have tried to call them with both variables and operations but alas.
Your JSON feed has more than one "operations" which means you can call them like this (to get the first only):
echo $request_array['operations'][0]['qp_status_code']."<br />";
echo $request_array['operations'][0]['qp_status_msg']."<br />";
echo $request_array['operations'][0]['aq_status_code']."<br />";
echo $request_array['operations'][0]['aq_status_msg']."<br />";
Or if you need all of them you need to loop in it:
foreach ($request_array['operations'] as $operation) {
echo $operation['aq_status_msg']."<br />";
}
with:
<?php
$data = file_get_contents("http://query.yahooapis.com/v1/public/yql?env=http%3A%2F%2Fdatatables.org%2Falltables.env&format=json&q=select%20*%20from%20yahoo.finance.historicaldata%20where%20startDate=%272014-01-01%27%20and%20endDate=%272014-01-10%27%20and%20symbol=%27YHOO%27");
$myArray = json_decode($data, true);
/*
echo "<pre>";
var_dump( $myArray );
echo "</pre>";
*/
echo $myArray['query']['results']['quote'][0]['Close']," DayX";
?>
i can read out the closing-number for the first day.
-> how can i read out ALL entries for 'Close'? -> in this example it would be:
echo $myArray['query']['results']['quote'][0]['Close']," Day1";
echo $myArray['query']['results']['quote'][1]['Close']," Day2";
echo $myArray['query']['results']['quote'][2]['Close']," Day3";
...
echo $myArray['query']['results']['quote'][6]['Close']," Day7";
You'll need to use a foreach loop to do that.
foreach ($myArray['query']['results']['quote'] as $k => $v) {
echo $v['Close'] . " Day" . ($k+1) . "<br />";
}
Having <br /> at the end of each line will add a line break, assuming this is being echoed to the browser;
Both values are '2' but I am not getting a true. Why?
echo $getuser. "<br />";
echo $userurl. "<br />";
if ($getuser == $userurl) {
echo "true <br />";
}
Result
2
2
Instead of echoing them out, use var_dump() to see exactly what is stored in those variables:
var_dump($getuser); echo "<br />";
var_dump($userurl); echo "<br />";
You probably have some stray spaces or other characters which are not easy, or maybe not possible to visually detect.
use
var_dump($getuser);
var_dump($userurl);
Pay attention to the string length. Consider using trim() if needed
may be contain dummy space in that, use like this
echo $getuser. "<br />";
echo $userurl. "<br />";
if (trim($getuser) == trim($userurl)) {
echo "true <br />";
}
When I try
$getuser = 2;
$userurl = 2;
echo $getuser. "<br />";
echo $userurl. "<br />";
if ($getuser == $userurl) {
echo "true <br />";
}
Results:
2
2
true
Works for me. On XAMPP (Windows) and Linux (Apache) alike. So there's problem probably somewhere in configuration or something...