0

This is my code :

                        for($i = 0;$i<$count1;$i++){
                        echo '<select name="attribute['.$i.']">';
                        while($parent = mysql_fetch_array($query)){

                        //echo $parent['Name'];
                        echo '<option value="'.$parent['ID'].'">'.$parent['Name'].'</option>';


                        }//while $parent
                        echo '</select>';


                        }//for loop 

My problem is why the output like this?:

<select name="attribute[0]">
<option value="2">Colour</option>
<option value="3">Size</option></select>
<select name="attribute[1]"></select>

Why this line does not contain the both option value?:

<select name="attribute[1]"></select>

Where is the logical mistake I did?

2
Contributors
7
Replies
24
Views
4 Years
Discussion Span
Last Post by devianleong
0

If you're only getting 1 row back from $query? Where is $count coming from? It looks okay to be hinest unless I'm missing something too. What's the value of $count?

0

I've change the structure to array and it's working now. This is the code. I hope can help somebody else that face the problem.

if($count1 > 0){
                        $attr = array();
                        while($parent = mysql_fetch_array($query)){
                        $attr[] = array(
                        "ID"=> $parent['ID'],
                        "Name" => $parent['Name']);

                        }//while $parent
                        for($i = 0;$i < $count1;$i++){
                        echo '<select name="attribute['.$i.']">';    
                        foreach ($attr as $att){
                        echo '<option value="'.$att['ID'].'">'.$att['Name'].'</option>';
                        }
                        echo'</select><br/>';
                        }
                        }
0

Well done, and sorry I couldn't help. You might want to leave this open for the next day before you mark it as solved as someone might spot what's wrong with your original code. To be honest I think the problem probably existed outside of the snippet you posted as I can't see anything wrong. At least you have it working now!

0

This is my full source code. I don't know why it can't loop when while loop inside a for loop. After I change while loop data to array and foreach it to output, It's work! weird huh.

The only thing I change is after the code of if($count1>0)

$query = mysql_query("SELECT * FROM attribute WHERE ParentID = '".(int)$_SESSION['userid']."'") or die (mysql_error());
                        $count1 = mysql_num_rows($query);
                        if($count1 > 0){
                        $attr = array();
                        while($parent = mysql_fetch_array($query)){
                        $attr[] = array(
                        "ID"=> $parent['ID'],
                        "Name" => $parent['Name']);

                        }//while $parent
                        for($i = 0;$i < $count1;$i++){
                        echo '<select name="attribute['.$i.']">';    
                        foreach ($attr as $att){
                        echo '<option value="'.$att['ID'].'">'.$att['Name'].'</option>';
                        }
                        echo'</select><br/>';
                        }
                        }

Edited by devianleong

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.