Welcome Guest, Not a member yet? Register   Sign In
For Loop Array Problem...
#1

[eluser]evolutionxbox[/eluser]
Controller:
Code:
function podcast($number = 0)
{
        $podcast = $this->curl->simple_get($feed_url);
        if($podcast) {
            $podcastXML = new SimpleXMLElement($podcast);
        }
        for ( $i = $number; $i < ($number+10); $i++ ) {
            $podcastChannel[] = $podcastXML->channel->item[$i];
        }
}

Number is defined by the url (example.com/podcast/<number>).

All I keep getting is an array that is filled but element 0 is empty.
Code:
Array
(
    [0] =>
    [1] => SimpleXMLElement Object
        (
            [title] => Blah
            [link] => ...
        )
)
1

Why is this?
#2

[eluser]evolutionxbox[/eluser]
I have found that instead of having a variable ($number) to set the start and end of my loop, having static numbers it actually works.

Why is this, I wonder?

Code:
$podcast = $this->curl->simple_get($feed_url);
if($podcast) {
    $podcastXML = new SimpleXMLElement($podcast);
}
for ( $i = 0; $i < 5; $i++ ) {
    $podcastChannel[] = $podcastXML->channel->item[$i];
}
#3

[eluser]Starovich[/eluser]
Have you attempted to cast it to an int?

Try it like this:
Code:
$podcast = $this->curl->simple_get($feed_url);
        if($podcast) {
            $podcastXML = new SimpleXMLElement($podcast);
        }
        for ( $i = $number; $i < (intval($number)+1); $i++ ) {
            $podcastChannel[] = $podcastXML->channel->item[$i];
        }
#4

[eluser]evolutionxbox[/eluser]
I haven't I shall try it.
#5

[eluser]danmontgomery[/eluser]
What's the point of writing a loop that only iterates once?

Code:
$podcast = $this->curl->simple_get($feed_url);
if($podcast) {
    $podcastXML = new SimpleXMLElement($podcast);
    $podcastChannel = array_shift($podcastXML->channel->item);
}

More to the point, the first element of the array is empty because $podcastXML->channel->item[$number] is empty/doesn't exist, or $number isn't defined.
#6

[eluser]evolutionxbox[/eluser]
I actually meant to write $number + 10 not 1... sorry.

$number is defined by the url (controller/function/parameter) in this case $number is my parameter.
#7

[eluser]danmontgomery[/eluser]
I suspect that it's not. I would double check the value before the loop to ensure that it's being set.
#8

[eluser]evolutionxbox[/eluser]
I have found that the problem lies in reading the simpleXML object, when I have this code:
Code:
print_r($podcastXML->channel->item[$number]);

I get nothing. Whereas if I have this:
Code:
print_r($podcastXML->channel->item[0]);

I get what I want. Why is this? ($number is defined by the function parameter).
#9

[eluser]evolutionxbox[/eluser]
I have found a solution! =)

Using the inval function suggested by Starovich (thank you btw), I used this code:
Code:
function podcast($number = 0)
{
    $podcast = $this->curl->simple_get($feed_url);
    if($podcast){
        $podcastXML = new SimpleXMLElement($podcast);
    }
    
    for ( $i = $number; $i < ($number+10); $i++ ) {
        $podcastChannel[$i] = $podcastXML->channel->item[intval($i)];
    }
}
... and got it working.

Thank you to everyone who helped out. Much appreciated.




Theme © iAndrew 2016 - Forum software by © MyBB