...

View Full Version : Resolved php function help please how to use a function with $i in it?



LJackson
03-25-2009, 06:05 PM
Hi all,

ok i have a site which displays a number of different feeds and a total of around 5 different feed settings to display the correct info,

the php pages look up the databse to see which setting needs to be applied to show all the feed data so to speak. and the top 3 feeds are show in detail at the top end of my entertainment3 page which is what the for loop is for :)

so in my entertainment3.php page i have

for ($i = 0; $i < 3; $i++) {
feedinfo();
}

feedinfo is a function in my feeds_function.php page

<?php function feedinfo (){;
if ($rssIndividual[$i] == ''){
print "Sorry No Information Currently Available";
}

elseif ($feedID[$i] == '1'){

$feedurl = $rssIndividual[$i];
$xml=simplexml_load_file($feedurl);
$count = 1;

foreach ($xml->merch->prod as $item){
$desc = html_entity_decode($item->desc);

if (in_array($item->pId,$test[$i])){
if($count > 0){

echo"
<div class='inditemheader'>
<a href='{$item->awLink}'>{$item->name}</a></div><br />

<div class='inditemimg'>
<a href='{$item->awLink}'>
<img src='{$item->mImage}'
alt='{$item->name}'
height='100'
border='0'
vspace='0'/></a>
</div>

<div class='inditemdesc'>$desc</div><br />
<div class='inditemprice'>Buy Now For {$item->price->search}</div>
<div class='inditeminfo'><a href='{$item->awLink}'>More Info</a></div>";
$count--;
}
}
}
}

elseif ($feedID[$i] == '2'){

$feedurl = $rssIndividual[$i];
$xml=simplexml_load_file($feedurl);
$count = 1;

foreach ($xml->product as $item){

if (in_array($item->TDProductId,$test[$i])){
if($count > 0){
echo"
<div class='inditemheader'>
<a href='{$item->deepLink}'>{$item->productname}</a></div><br />

<div class='inditemimg'>
<a href='{$item->deepLink}'>
<img src='{$item->imageURL}'
alt='{$item->productname}'
height='100'
border='0'
vspace='0'/></a>
</div>

<div class='inditemdesc'>{$item->description}</div><br />
<div class='inditemprice'>Buy Now For {$item->Price}</div>
<div class='inditeminfo'><a href='{$item->deepLink}'>More Info</a></div>"; $count--;
}
}
}
}

elseif ($feedID[$i] == '3'){

$feedurl = $rssIndividual[$i];
$xml=simplexml_load_file($feedurl);
$count = 1;

foreach ($xml->merchant->prod as $item){

if (in_array($item->pId,$test[$i])){
if($count > 0){
echo"
<div class='inditemheader'>
<a href='{$item->uri->awTrack}'>{$item->text->name}</a></div><br />

<div class='inditemimg'>
<a href='{$item->uri->awTrack}'>
<img src='{$item->uri->mImage}'
alt='{$item->text->name}'
height='100'
border='0'
vspace='0'/></a>
</div>

<div class='inditemdesc'>{$item->text->promo}</div><br />
<div class='inditemprice'>Buy Now For {$item->price->buynow}</div>
<div class='inditeminfo'><a href='{$item->uri->awTrack}'>More Info</a></div>"; $count--;
}
}
}
}

}

i have also included this page on my entertainment3 page

but i am getting some notices here (http://www.kernow-connect.com/entertainment3.php)

and im not sure how to solve them.

please advise me :)

many thanks
Luke

abduraooft
03-25-2009, 06:09 PM
I think it should be like

for ($i = 0; $i < 3; $i++) {
feedinfo($i);
}

function feedinfo ($i){
if ($rssIndividual[$i] == ''){
print "Sorry No Information Currently Available";
}
.............

Fou-Lu
03-25-2009, 06:10 PM
Scope problem.
Functions create their own stack in memory. So, any variable declared outside of a function (with the exception of superglobals) are unavailable within the actual function.
That's this guy here: $rssIndividual. Pass this value into the method as a parameter in order to make use of it, or you can globalize it which is not recommended.



function feedInfo(array $rssIndividual);

should be you're function signature.


I lol'd. Neither mine or abduraooft's post's are correct, but together they make the whole. I was missing the $i variable, and he was missing the $rssIndividual variable.

Another one. I guess since I was also missing the $i, the signature will change:


function feedInfo(array $rssIndividual, $i);
// OR
function feedInfo($rssIndividual);

Where the second function is likely what you want, but you pass in the value at the corresponding $rssIndivdual[$i] into the feedInfo method.

LJackson
03-25-2009, 06:21 PM
ok im confused :( lol

the function signature is that what i would use to call the function in my entertainment3 page or what i use to define the function in the feeds_function page ?

sorry for missunderstanding :)

cheers
Luke

LJackson
03-25-2009, 06:40 PM
ok i have added it to my feeds_function page like so

<?php function feedinfo(array $rssIndividual, $i){;
if ($rssIndividual[$i] == ''){
print "Sorry No Information Currently Available";
}

elseif ($feedID[$i] == '1'){

$feedurl = $rssIndividual[$i];
$xml=simplexml_load_file($feedurl);
$count = 1;

foreach ($xml->merch->prod as $item){
$desc = html_entity_decode($item->desc);

if (in_array($item->pId,$test[$i])){
if($count > 0){

echo"
<div class='inditemheader'>
<a href='{$item->awLink}'>{$item->name}</a></div><br />

<div class='inditemimg'>
<a href='{$item->awLink}'>
<img src='{$item->mImage}'
alt='{$item->name}'
height='100'
border='0'
vspace='0'/></a>
</div>

<div class='inditemdesc'>$desc</div><br />
<div class='inditemprice'>Buy Now For {$item->price->search}</div>
<div class='inditeminfo'><a href='{$item->awLink}'>More Info</a></div>";
$count--;
}
}
}
}

elseif ($feedID[$i] == '2'){

$feedurl = $rssIndividual[$i];
$xml=simplexml_load_file($feedurl);
$count = 1;

foreach ($xml->product as $item){

if (in_array($item->TDProductId,$test[$i])){
if($count > 0){
echo"
<div class='inditemheader'>
<a href='{$item->deepLink}'>{$item->productname}</a></div><br />

<div class='inditemimg'>
<a href='{$item->deepLink}'>
<img src='{$item->imageURL}'
alt='{$item->productname}'
height='100'
border='0'
vspace='0'/></a>
</div>

<div class='inditemdesc'>{$item->description}</div><br />
<div class='inditemprice'>Buy Now For {$item->Price}</div>
<div class='inditeminfo'><a href='{$item->deepLink}'>More Info</a></div>"; $count--;
}
}
}
}
}

but its throwing up an error,

Catchable fatal error: Argument 1 passed to feedinfo() must be an array, none given, called in /customers/kernow-connect.com/kernow-connect.com/httpd.www/entertainment3.php on line 248 and defined in /customers/kernow-connect.com/kernow-connect.com/httpd.www/feeds_function.php on line 1

cheers for you help so far guys much appreciated
Luke

Fou-Lu
03-25-2009, 07:32 PM
Post how you're calling the feedInfo function.
Oh, and take that ; off the line with the function signature on it. That does not belong there.

LJackson
03-25-2009, 07:44 PM
its ok now thanks mate :) i removed that ; and added another two varibles to the function which i required

the function

<?php function feedinfo(array $rssIndividual, $feedID, $test, $i){;


and i called it by

feedinfo($rssIndividual,$feedID, $test, $i);


which is working :) :)

thank you both for all your help, you learn something new every day :)

cheers
Luke



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum