while-do +array

This is a discussion on while-do +array within the PHP General forums, part of the PHP Programming Forums category; Hiya, I have the following code ... which only seems to result in one item ... which is incorrect ... can anyone spot ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-31-2007
Steven Macintyre
 
Posts: n/a
Default while-do +array

Hiya,

I have the following code ... which only seems to result in one item ...
which is incorrect ... can anyone spot my problem?

if ($armbase != "") {
$options = explode(",", $armbase);
$text .= '<table ><tr>';
$get_endRow = 0;
$get_columns = 8;
$get_hloopRow1 = 0;

do {
if($get_endRow == 0 && $get_hloopRow1++ != 0) {
$text .= '<tr>';
$text .= '<td valign="top" width="60">';
$text .= "<img
src='".e_BASE."images/options/armbase/".$value."'>";
$text .= '</td>';
$get_endRow++;
}
if($get_endRow >= $get_columns) {
$text .= '</tr>';
$get_endRow = 0;
}
} while(list($key,$value) = each($options));

if($get_endRow != 0) {
while ($get_endRow < $get_columns) {
$text .= '<td>&nbsp;</td>';
$get_endRow++;
}
$text .= '</tr>';
}

$text .= '</table>';

}

The purpose of the code is to create 8 colums and populate based on the
total results returned from the while-do check

Thanks in advance

Steven
Reply With Quote
  #2 (permalink)  
Old 10-31-2007
Robert Cummings
 
Posts: n/a
Default Re: [PHP] while-do +array

On Wed, 2007-10-31 at 13:02 +0200, Steven Macintyre wrote:
> Hiya,
>
> I have the following code ... which only seems to result in one item ...
> which is incorrect ... can anyone spot my problem?


Your subject line says "while-do" + array, following code contains a
"do-while" loop. We can't debug the script very well without a sample
input since we don't know how many times the loop "should" run. To debug
yourself, try using print_r() to see exactly what the following line of
code produces:

$options = explode(",", $armbase);

It's also useful for you to know that the list() function in your while
clause has return value void, AKA null, and so it will always be a false
statement and your loop will terminate on the first check.

Cheers,
Rob.



> if ($armbase != "") {
> $options = explode(",", $armbase);
> $text .= '<table ><tr>';
> $get_endRow = 0;
> $get_columns = 8;
> $get_hloopRow1 = 0;
>
> do {
> if($get_endRow == 0 && $get_hloopRow1++ != 0) {
> $text .= '<tr>';
> $text .= '<td valign="top" width="60">';
> $text .= "<img
> src='".e_BASE."images/options/armbase/".$value."'>";
> $text .= '</td>';
> $get_endRow++;
> }
> if($get_endRow >= $get_columns) {
> $text .= '</tr>';
> $get_endRow = 0;
> }
> } while(list($key,$value) = each($options));
>
> if($get_endRow != 0) {
> while ($get_endRow < $get_columns) {
> $text .= '<td>&nbsp;</td>';
> $get_endRow++;
> }
> $text .= '</tr>';
> }
>
> $text .= '</table>';
>
> }
>
> The purpose of the code is to create 8 colums and populate based on the
> total results returned from the while-do check
>
> Thanks in advance
>
> Steven
>

--
.................................................. ..........
SwarmBuy.com - http://www.swarmbuy.com

Leveraging the buying power of the masses!
.................................................. ..........
Reply With Quote
  #3 (permalink)  
Old 10-31-2007
Steven Macintyre
 
Posts: n/a
Default RE: [PHP] while-do +array

> Your subject line says "while-do" + array, following code contains
> a
> "do-while" loop.


Apologies :)

> We can't debug the script very well without a
> sample
> input since we don't know how many times the loop "should" run. To
> debug
> yourself, try using print_r() to see exactly what the following
> line of
> code produces:
>
> $options = explode(",", $armbase);
>


Array ( [0] => 4200_side_frame.jpg [1] => 4220_integral_frame.jpg [2] =>
flamingo_pu_armrest.jpg [3] => flexible_pu_armrest.jpg [4] =>
futura_standard_sleigh_base.jpg [5] => futura_universal_sleigh_base.jpg )

That's the output ... so the array is definitely ok ...

s
Reply With Quote
  #4 (permalink)  
Old 10-31-2007
Robert Cummings
 
Posts: n/a
Default RE: [PHP] while-do +array

On Wed, 2007-10-31 at 16:20 +0200, Steven Macintyre wrote:
> > Your subject line says "while-do" + array, following code contains
> > a
> > "do-while" loop.

>
> Apologies :)
>
> > We can't debug the script very well without a
> > sample
> > input since we don't know how many times the loop "should" run. To
> > debug
> > yourself, try using print_r() to see exactly what the following
> > line of
> > code produces:
> >
> > $options = explode(",", $armbase);
> >

>
> Array ( [0] => 4200_side_frame.jpg [1] => 4220_integral_frame.jpg [2] =>
> flamingo_pu_armrest.jpg [3] => flexible_pu_armrest.jpg [4] =>
> futura_standard_sleigh_base.jpg [5] => futura_universal_sleigh_base.jpg )
>
> That's the output ... so the array is definitely ok ...


Yeah the last comment in my response detailsed your problem. Namely that
list() has no return value so the loop ends when it's return value is
checked since null juggles to false in boolean context.

Cheers,
Rob.
--
.................................................. ..........
SwarmBuy.com - http://www.swarmbuy.com

Leveraging the buying power of the masses!
.................................................. ..........
Reply With Quote
  #5 (permalink)  
Old 10-31-2007
Robin Vickery
 
Posts: n/a
Default Re: [PHP] while-do +array

On 31/10/2007, Steven Macintyre <steven@steven.macintyre.name> wrote:
> Hiya,
>
> I have the following code ... which only seems to result in one item ...
> which is incorrect ... can anyone spot my problem?
>
> if ($armbase != "") {
> $options = explode(",", $armbase);
> $text .= '<table ><tr>';
> $get_endRow = 0;
> $get_columns = 8;
> $get_hloopRow1 = 0;
>
> do {
> if($get_endRow == 0 && $get_hloopRow1++ != 0) {
> $text .= '<tr>';
> $text .= '<td valign="top" width="60">';
> $text .= "<img
> src='".e_BASE."images/options/armbase/".$value."'>";
> $text .= '</td>';
> $get_endRow++;
> }
> if($get_endRow >= $get_columns) {
> $text .= '</tr>';
> $get_endRow = 0;
> }
> } while(list($key,$value) = each($options));


The first time around the loop, both $key and $value are undefined, as
you're not setting them until the end condition.

But that's not a big deal as nothing gets done first time through the
loop except for $get_hloopRow1 getting incremented.

The next time through the loop, because $get_hloopRow1 is now not
zero, the first conditional gets executed. You add some html and the
first value from the array to $text to the string and incremenent
$get_endRow.

All subsequent times through the loop, nothing gets done because the
$get_endRow == 0 condition fails. $get_endRow never gets incrememented
again.

that help at all?

-robin
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 06:33 AM.


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