OOP & While

OOP & While

am 07.04.2008 17:37:28 von UKuser

Hi Folks

I'm super new to using OOP (for example having recently discovered
that public functions don't therefore need to be inherited! horray) -
I have come across this issue.

Within my class is a private function which returns a Zend_db fetchAll
SQL query as an array.

Another function then needs to make repeated calls to the above
function, and the code below is what I'm using to do this:

while ($row = $search->fetch()) { // fetch comes from Zend_db
$cid = $row['Company_ID'];
$test = $this->allowedImages($cid); // allowedImages returns an
array of results
print "$cid-{$test[1]}
";
}

I expect about 12 results and if I comment out the $test line it does
so, however without the comment markers it only returns 1 result. Am I
missing how to refer to a function multiple times but with different
results?

Thanks in advance.

A

Re: OOP & While

am 08.04.2008 16:18:32 von tmilewski

On Apr 7, 11:37 am, UKuser wrote:
> Hi Folks
>
> I'm super new to using OOP (for example having recently discovered
> that public functions don't therefore need to be inherited! horray) -
> I have come across this issue.
>
> Within my class is a private function which returns a Zend_db fetchAll
> SQL query as an array.
>
> Another function then needs to make repeated calls to the above
> function, and the code below is what I'm using to do this:
>
> while ($row = $search->fetch()) { // fetch comes from Zend_db
> $cid = $row['Company_ID'];
> $test = $this->allowedImages($cid); // allowedImages returns an
> array of results
> print "$cid-{$test[1]}
";
> }
>
> I expect about 12 results and if I comment out the $test line it does
> so, however without the comment markers it only returns 1 result. Am I
> missing how to refer to a function multiple times but with different
> results?
>
> Thanks in advance.
>
> A

You would have to do this:

while ($row = $search->fetch())
{
$test = $this->allowedImages($row['Company_ID']);

for($i = 0; $i < count($test); $i++)
{
echo $test[$i] . '
';
}
}

Re: OOP & While

am 08.04.2008 17:45:47 von UKuser

On Apr 8, 3:18 pm, Tom Milewski wrote:
> On Apr 7, 11:37 am, UKuser wrote:
>
>
>
> > Hi Folks
>
> > I'm super new to using OOP (for example having recently discovered
> > that public functions don't therefore need to be inherited! horray) -
> > I have come across this issue.
>
> > Within my class is a private function which returns a Zend_db fetchAll
> > SQL query as an array.
>
> > Another function then needs to make repeated calls to the above
> > function, and the code below is what I'm using to do this:
>
> > while ($row = $search->fetch()) { // fetch comes from Zend_db
> > $cid = $row['Company_ID'];
> > $test = $this->allowedImages($cid); // allowedImages returns an
> > array of results
> > print "$cid-{$test[1]}
";
> > }
>
> > I expect about 12 results and if I comment out the $test line it does
> > so, however without the comment markers it only returns 1 result. Am I
> > missing how to refer to a function multiple times but with different
> > results?
>
> > Thanks in advance.
>
> > A
>
> You would have to do this:
>
> while ($row = $search->fetch())
> {
> $test = $this->allowedImages($row['Company_ID']);
>
> for($i = 0; $i < count($test); $i++)
> {
> echo $test[$i] . '
';
> }
>
> }

Hi,

Thanks for that. Yep - fixed it. Interested to know why $test has
become an array considering row['Company_ID'] is a unique number - but
thanks again.

A

Re: OOP & While

am 08.04.2008 17:48:29 von UKuser

On Apr 8, 4:45 pm, UKuser wrote:
> On Apr 8, 3:18 pm, Tom Milewski wrote:
>
>
>
> > On Apr 7, 11:37 am, UKuser wrote:
>
> > > Hi Folks
>
> > > I'm super new to using OOP (for example having recently discovered
> > > that public functions don't therefore need to be inherited! horray) -
> > > I have come across this issue.
>
> > > Within my class is a private function which returns a Zend_db fetchAll
> > > SQL query as an array.
>
> > > Another function then needs to make repeated calls to the above
> > > function, and the code below is what I'm using to do this:
>
> > > while ($row = $search->fetch()) { // fetch comes from Zend_db
> > > $cid = $row['Company_ID'];
> > > $test = $this->allowedImages($cid); // allowedImages returns an
> > > array of results
> > > print "$cid-{$test[1]}
";
> > > }
>
> > > I expect about 12 results and if I comment out the $test line it does
> > > so, however without the comment markers it only returns 1 result. Am I
> > > missing how to refer to a function multiple times but with different
> > > results?
>
> > > Thanks in advance.
>
> > > A
>
> > You would have to do this:
>
> > while ($row = $search->fetch())
> > {
> > $test = $this->allowedImages($row['Company_ID']);
>
> > for($i = 0; $i < count($test); $i++)
> > {
> > echo $test[$i] . '
';
> > }
>
> > }
>
> Hi,
>
> Thanks for that. Yep - fixed it. Interested to know why $test has
> become an array considering row['Company_ID'] is a unique number - but
> thanks again.
>
> A

Being daft - of course - its adding to the previous array for each row
rather than created a new one - hence you needed the foreach. Thanks!