Bluehost.com Web Hosting $6.95

warning: rewind() supplied argument is not a valid stream resource in...

This is a discussion on warning: rewind() supplied argument is not a valid stream resource in... within the PHP Language forums, part of the PHP Programming Forums category; i am implementing Iterator in a class, I have pretty much copied the code from php.net on Object Iteration. ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-17-2006
gp
 
Posts: n/a
Default warning: rewind() supplied argument is not a valid stream resource in...

i am implementing Iterator in a class, I have pretty much copied the
code from php.net on Object Iteration. Adding all the normal methods
for the task...rewind, current, next, etc.

I was attempting to add a seek method but am stymied by the above
warnin, as well getting a seek on index 0 (zero) to work.

public function __construct($contents) {
if ( is_array($contents)) {
if ( is_array($contents[1])) {
$this->cktl = $contents[1];
}
}
}

public function rewind() {

$cktl = rewind($this->cktl);
return $cktl;
}

public function current() {
$cktl = current($this->cktl);
return $cktl;
}

public function key() {
$cktl = key($this->cktl);
return $cktl;
}

public function next() {
$cktl = next($this->cktl);
return $cktl;
}

public function valid() {
$cktl = $this->current() !== FALSE;
return $cktl;
}

public function myseek($intPos) {
$myPos = 0;
$this->rewind();
while ( $myPos < $intPos && $this->valid() ) {
$cktl = $this->next();
$myPos++;
}
if ( !$this->valid() ) {
die("Invalid seek position");
}

return $cktl;
}

Reply With Quote
  #2 (permalink)  
Old 10-17-2006
Tim Hunt
 
Posts: n/a
Default Re: warning: rewind() supplied argument is not a valid stream resource in...


gp wrote:
> i am implementing Iterator in a class, I have pretty much copied the
> code from php.net on Object Iteration. Adding all the normal methods
> for the task...rewind, current, next, etc.
>


>
> public function rewind() {
>
> $cktl = rewind($this->cktl);
> return $cktl;
> }



You need to use reset($this->cktl) not rewind($this->cktl), the rewind
function is different to the rewind method of an iterator as it does
not work on arrays and it resets the pointer position of a file handle.

Reply With Quote
  #3 (permalink)  
Old 10-17-2006
gp
 
Posts: n/a
Default Re: warning: rewind() supplied argument is not a valid stream resource in...


aaaaarrrrrg..... i think i rewrote that function incorrectly...after
going back thank you for pointing that out for me :-D
i'll give it a whack
gp
On Oct 17, 11:28 am, "Tim Hunt" <tim.n.h...@gmail.com> wrote:
> gp wrote:
> > i am implementing Iterator in a class, I have pretty much copied the
> > code from php.net on Object Iteration. Adding all the normal methods
> > for the task...rewind, current, next, etc.

>
> > public function rewind() {

>
> > $cktl = rewind($this->cktl);
> > return $cktl;
> > }You need to use reset($this->cktl) not rewind($this->cktl), the rewind

> function is different to the rewind method of an iterator as it does
> not work on arrays and it resets the pointer position of a file handle.


Reply With Quote
  #4 (permalink)  
Old 10-17-2006
gp
 
Posts: n/a
Default Re: warning: rewind() supplied argument is not a valid stream resource in...


Back to this issue? My seek function hates me and index 0....any
ideas?

> I was attempting to add a seek method but am stymied by the above
> warnin, as well getting a seek on index 0 (zero) to work.
> public function myseek($intPos) {
> $myPos = 0;
> $this->rewind();
> while ( $myPos < $intPos && $this->valid() ) {
> $cktl = $this->next();
> $myPos++;
> }
> if ( !$this->valid() ) {
> die("Invalid seek position");
> }
>
> return $cktl;
> }


Reply With Quote
  #5 (permalink)  
Old 10-17-2006
Jerry Stuckle
 
Posts: n/a
Default Re: warning: rewind() supplied argument is not a valid stream resourcein...

gp wrote:
> i am implementing Iterator in a class, I have pretty much copied the
> code from php.net on Object Iteration. Adding all the normal methods
> for the task...rewind, current, next, etc.
>
> I was attempting to add a seek method but am stymied by the above
> warnin, as well getting a seek on index 0 (zero) to work.
>


<code snipped>

rewind() is for files. I think you're looking for reset().

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Reply With Quote
  #6 (permalink)  
Old 10-17-2006
Jerry Stuckle
 
Posts: n/a
Default Re: warning: rewind() supplied argument is not a valid stream resourcein...

gp wrote:
> i am implementing Iterator in a class, I have pretty much copied the
> code from php.net on Object Iteration. Adding all the normal methods
> for the task...rewind, current, next, etc.
>
> I was attempting to add a seek method but am stymied by the above
> warnin, as well getting a seek on index 0 (zero) to work.

<snip>
>
> public function myseek($intPos) {
> $myPos = 0;
> $this->rewind();
> while ( $myPos < $intPos && $this->valid() ) {
> $cktl = $this->next();
> $myPos++;
> }
> if ( !$this->valid() ) {
> die("Invalid seek position");
> }
>
> return $cktl;
> }
>


if $intPos is 0, you never enter your while loop and therefore never
execute your assignment to $cktl. Also, it's unnecessary overhead to
set $cktl each time through the loop. All you really need to do is
advance the pointer.

Try this:

public function myseek($intPos) {
$myPos = 0;
$this->rewind();
while ( $myPos < $intPos && $this->valid() ) {
$this->next();
$myPos++;
}
if ( !$this->valid() ) {
die("Invalid seek position");
}

return $this->current();
}


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 12:21 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0