Re: Infinite Loop Issue

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Nov 6, 2008 at 5:56 PM, Chris <dmagick@xxxxxxxxx> wrote:
> Kyle Terry wrote:
>>
>> I believe I'm doing everything right here. It just seems like it doesn't
>> end. The browser just keeps trying to load the page forever...
>>
>> function displayAll(){
>>    global $db;
>>    $sql = "SELECT release_id, description, date(release_date) date,
>> issues,
>> priority FROM release_data";
>
> how many rows does this return?
>
>
>>    while($all->fetch()){
>>
>>        $i = 0;
>>        $iss_link = explode(', ', $issues);
>>        foreach($iss_link as $a){
>>          $row2[$i] = "<a href=\"http://mantisus/view.php?id=$a\";
>> target=\"_blank\">".$a.'</a>';
>>          $i++;
>>        }
>
> <snip>
>
> You have a loop inside a loop. What makes it worse is that the loop is
> growing with every iteration.
>
> If there are 100 rows, then at the end, there are 100 entries in the $issues
> array.
>
> To get to that point, you are doing a loop of size "X" where "X" = the row
> number being processed. ie row "2" is doing a foreach over 2 entries. row 15
> is doing a foreach over 15 entries. row 90 is doing a foreach over 90
> entries.

That's not QUITE right. The value of $issues will always be set to the
column value for the next row when $all->fetch() is called, therefore
overwriting the value it had after the previous iteration. The
variable $row2 should be set to an empty array before the foreach loop
just in case different rows have different numbers of issues.

Iteration 1:
before foreach
$issues == 'a, b, c'
$i == 0
$iss_link == Array
(
    [0] => 'a',
    [1] => 'b',
    [2] => 'c'
)
$row2 == empty

after foreach
$i == 2
$row2 == Array
(
    [0] => '<a href="http://mantisus/view.php?id=a"; target="_blank">a</a>',
    [1] => '<a href="http://mantisus/view.php?id=b"; target="_blank">b</a>',
    [2] => '<a href="http://mantisus/view.php?id=c"; target="_blank">c</a>',
)

after implode
$issues == '<a href="http://mantisus/view.php?id=a";
target="_blank">a</a>, <a href="http://mantisus/view.php?id=b";
target="_blank">b</a>, <a href="http://mantisus/view.php?id=c";
target="_blank">c</a>'

Iteration 2:
before foreach:
$issues == '1, 2'
$i == 0
$iss_link == Array
(
    [0] => '1',
    [1] => '2',
)
/* STILL CONTAINS THE ARRAY FROM THE PREVIOUS ITERATION */
$row2 == Array
(
    [0] => '<a href="http://mantisus/view.php?id=a"; target="_blank">a</a>',
    [1] => '<a href="http://mantisus/view.php?id=b"; target="_blank">b</a>',
    [2] => '<a href="http://mantisus/view.php?id=c"; target="_blank">c</a>',
)


after foreach:
$i == 1
$row2 == Array
(
    [0] => '<a href="http://mantisus/view.php?id=1"; target="_blank">1</a>',
    [1] => '<a href="http://mantisus/view.php?id=2"; target="_blank">2</a>',
    [2] => '<a href="http://mantisus/view.php?id=c"; target="_blank">c</a>',
)


after implode:
$issues == '<a href="http://mantisus/view.php?id=1";
target="_blank">1</a>, <a href="http://mantisus/view.php?id=2";
target="_blank">2</a>, <a href="http://mantisus/view.php?id=c";
target="_blank">c</a>'


There is the potential for wrong behavior, but it the multiplicative
loops you are talking about shouldn't happen.

Andrew

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux