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?

Recommended Answers

All 7 Replies

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?

$count1 is the count from mysql_num_rows

And what's its value?

The total count of rows is 2

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/>';
                        }
                        }

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!

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/>';
                        }
                        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.