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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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> </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 |
|
|||
|
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> </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! .................................................. .......... |
|
|||
|
> 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 |
|
|||
|
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! .................................................. .......... |
|
|||
|
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 |