Welcome Guest, Not a member yet? Register   Sign In
loop problem
#1

[eluser]Bigil Michael[/eluser]
i want to execute multiple while loop

like this

Code:
<?php $selected_ member = "select id,name from members"
$result_search = $db->query($selected_ member);
while(list($id,$name) = $db->fetch_array($result_search))
{
     $search_query = "select id,name from members"
     $result_s = $db->query($search_query);
     while(list($id,$name) = $db->fetch_array($result_s))
    {
      echo $name;
    }
}

my table contains 5 entries
so it must print 25 rows

it prints 5 rows only once
that means the loop execute only once.
i dont know what is the problem here ??????
can anyone help me????
#2

[eluser]Seb[/eluser]
I think you should store the resulting arrays into variables, and then loop over those variables instead of looping directly on the queries.
#3

[eluser]vitoco[/eluser]
This may work
Code:
<?
$selected_member    = "select id,name from members" ;
$result_search        = $db->query($selected_ member);
//
$query_search    = $this->db->query( $selected_member );
//
foreach( $query_search->result_array() as $row_1 )
{
    $id        = $row_1['id'] ;
    $name    = $row_1['name'] ;
    //
    $search_query    = "select id,name from members" ;
    $query_s        = $db->query($search_query) ;
    //
    foreach( $query_s->result_array() as $row_2 )
    {
            echo $row_2['name'];
    }
            
    $query_s->free_result();
    
}
        
$query_search->free_result();
#4

[eluser]CroNiX[/eluser]
because both of your loops use $id, $name, so the inner loop is overwriting the outer loops variables. Just rename the inner loop variables to something else.
#5

[eluser]vitoco[/eluser]
@CroNiX it's right , that's because i prefer foreach to while.
#6

[eluser]CroNiX[/eluser]
I was referring to the original post.

in both loops he is doing list($id, $name). 2nd loop's $id, $name overwrites first loops $id, $name. 2nd loop should be $id2, $name2 or something different, although I don't see the logic in what he is doing in the first place. He is retrieving the same results over and over because there is no where statement.
#7

[eluser]vitoco[/eluser]
i know yo do...
#8

[eluser]jmadsen[/eluser]
@Bigil MM - can you explain what you are doing this for? your code looks very strange to me, unless you are trying to create a grid where every value matches up against every other value?

If that's the case, there is a trick to create a Cartesian Product that will be much simpler and efficient. If it's not, you may be doing something incorrectly




Theme © iAndrew 2016 - Forum software by © MyBB