Jump to content

help with error " Warning: Invalid argument supplied for foreach() "


Recommended Posts

can anyone tell me why am i getting this error

 

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\rest\hope2.php on line 19

 

here the code :

 

$xml = file_get_contents($request);

$Result = xmlparser($xml);

function theContent(){

global $Result;

echo '<table cellspacing="2" cellpadding="2"><tr>';

$rowcount=0;

here line 19=> foreach($Result['ItemSearchResponse']['Items'][0]['Item'] as $item) {

if (isset($item['MediumImage']['URL'])){

$image='<img src="'.$item["MediumImage"]["URL"].'" />';

}

else{ $image='<img src="'.NOIMAGE.'" />';}

$title= '<a href="'.$item["DetailPageURL"].'" target="_blank">'.

$item["ItemAttributes"]["Title"].'</a>';

if (is_int($rowcount/5)) echo '</tr><tr>';

echo '<td><div style="text-align: center;>'.$title.'</div><div style="text-align: center;>'.$image.'<div></td>';

$rowcount++;

}

echo '</tr></table>';

return;

}

 

i will be waiting for all your reply thx once again .

foreach( as $item)

 

this should be an array but when you do this $Result['ItemSearchResponse']['Items'][0]['Item'] you are referring to specific index so its not like an array

 

hmm do this

 

foreach($Result as $item)

{

//$item['ItemSearchResponse']['Items'][0]['Item'];

}

 

// or if that not what you want then

print_r($Result);

then copy here the result and specify what you want

 

 

I think the function you are looking for is

xml_parse

 

Not

xmlparser

 

Unless of course that is your own function. If it isn't, the xml_parse() requires two parameters.

www.php.net/xml_parse

------------------------------

 

As teng said, the first step is checking to see if your array even holds any values.

print_r($Result);

thx for all your reply ,

when i put this

:" print_r($Result);"

 

i get this on top of the error

:" Your request is missing required parameters. Required parameters include SearchIndex "

 

so where do i need include that ? thx once again

ok here's the whole php script

 

<?php

require_once('tools.inc.php');

define('SEARCH','Lou Donaldson');

define ('SUBID', '1A7XKHR5BYD0WPJVQEG2');

define('ASSOCIATE_ID','ws');

define('VERSION','2005-02-23');

define('NOIMAGE','http://g-images.amazon.com/images/G/01/x-site/icons/no-img-lg.gif');

define('DEFAULT_PAGE','1');

$page=(isset($_GET['page']))?$_GET['page']:DEFAULT_PAGE;

$request='http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AssociateTag='.ASSOCITES_ID.'&SubscriptionId='.

SUBID.'&Operation=ItemSearch&Index=Music&Artist=Lou%20Donaldson&ItemPage='.

$page.'&ResponseGroup=Small,Images&Version='.VERSION;

$xml = file_get_contents($request);

$Result = xmlparser($xml);

function theContent(){

global $Result;

echo '<table cellspacing="2" cellpadding="2"><tr>';

$rowcount=0;

=>print_r($Result);

=>foreach($Result['ItemSearchResponse']['Items'][0]['Item'] as $items) {

if (isset($item['MediumImage']['URL'])){

$image='<img src="'.$item["MediumImage"]["URL"].'" />';

else{ $image='<img src="'.NOIMAGE.'" />';}

$title= '<a href="'.$item["DetailPageURL"].'" target="_blank">'.

$item["ItemAttributes"]["Title"].'</a>';

if (is_int($rowcount/5)) echo '</tr><tr>';

echo '<td><div style="text-align: center;>'.$title.'</div><div style="text-align: center;>'.$image.'<div></td>';

$rowcount++;

}

echo '</tr></table>';

return;

}

function thePagination(){

global $Result;

global $page;

echo '<div style="text-align: left;"><b>More result.....</div><div>';

for ($i=1; $i<= $Result['ItemSearchResponse']['Items'][0]['TotalPages']; $i++)

{

$p =($i == $page) ? $page : '<a href="lou.php?page='.$i.'">'.$i.'</a>';

echo $p.' ';

}

echo '</b></div>';

return;

}

?>

<html>

<head>

<title>

Lou Donaldson CDs </title>

</head>

<body>

<table width="100%" border="1" cellpadding="2" cellspacing="2">

<tr>

<td width="100%" height="250"<?php theContent();?></td>

</tr>

<tr>

<td colspan="2">

<table width="100%"> <?php thePagination();?>

</td>

</tr>

</table>

<td>

</td>

</table>

</body>

</html>

 

the output should be a page with a set of cds  ,

 

Use this code, and tell us what you see on the screen:

 

<?php
require_once('tools.inc.php');
define('SEARCH','Lou Donaldson');
define('SUBID', '1A7XKHR5BYD0WPJVQEG2');
define('ASSOCIATE_ID','ws');
define('VERSION','2005-02-23');
define('NOIMAGE','http://g-images.amazon.com/images/G/01/x-site/icons/no-img-lg.gif');
define('DEFAULT_PAGE','1');
$page=(isset($_GET['page']))?$_GET['page']:DEFAULT_PAGE;
$request='http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AssociateTag='.ASSOCITES_ID.'&SubscriptionId='.
SUBID.'&Operation=ItemSearch&Index=Music&Artist=Lou%20Donaldson&ItemPage='.
$page.'&ResponseGroup=Small,Images&Version='.VERSION;
$xml = file_get_contents($request);
$Result = xmlparser($xml);

print_r($Result);

?>

 

thx pocobueno1388,

when i run the script i got this in my browser :

 

Array ( [itemSearchResponse] => Array (

[xmlns] => http://webservices.amazon.com/AWSECommerceService/2005-02-23 [OperationRequest] => Array ( [HTTPHeaders] => Array ( [Header] => Array ( [0] => Array ( [Name] => UserAgent [Header] => ) ) ) [RequestId] => 1NXHRWCNRZTYMDFAPH6M [Arguments] => Array ( [Argument] => Array ( [0] => Array ( [Name] => AssociateTag [Value] => ASSOCITES_ID [Argument] => ) [1] => Array ( [Name] => Artist [Value] => Lou Donaldson [Argument] => ) [2] => Array ( [Name] => ItemPage [Value] => 1 [Argument] => ) [3] => Array ( [Name] => Service [Value] => AWSECommerceService [Argument] => ) [4] => Array ( [Name] => SubscriptionId [Value] => 1A7XKHR5BYD0WPJVQEG2 [Argument] => ) [5] => Array ( [Name] => ResponseGroup [Value] => Small,Images [Argument] => ) [6] => Array ( [Name] => Operation [Value] => ItemSearch [Argument] => ) [7] => Array ( [Name] => Index [Value] => Music [Argument] => ) [8] => Array ( [Name] => Version [Value] => 2005-02-23 [Argument] => ) ) ) [RequestProcessingTime] => 0.0269980430603027 ) [items] => Array ( [0] => Array ( [Request] => Array ( [isValid] => False [itemSearchRequest] => Array ( [Artist] => Array ( [0] => Lou Donaldson ) [itemPage] => 1 [ResponseGroup] => Array ( [0] => Small [1] => Images ) ) [Errors] => Array ( [Error] => Array ( [0] => Array (
 => AWS.MissingParameters [Message] => Your request is missing required parameters. Required parameters include SearchIndex. ) ) ) ) ) ) ) ) [/b]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.