0

I'm trying to dynamically generate html select options using PHP based on whatever its stored in mysql database.

the column that stores the data called sizez.

the data in that column is stored like so:

small,large,xlarge,xxlarge

so basically the data is separated by a comma.

now in my php page I simply pull the data and display it on my page using a while loop for each product that is stored in the mysql database.

the issue that I am having is that I need to generate a select option dropdown list based on the sizez column for each item.

for that I am using the explode() function and it will generate the select option successfully too.

however, the issue is that it will only get the strings from the first sizez column and ignores the rest of the items But it will display the string from the first column for other items too and it repeats them!

this is my code:

    while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
             $id = $row["id"];
             $sizez = $row["sizez"];
             $sizez = preg_replace('/\.$/', '', $sizez); //Remove dot at end if exists
             $array = explode(',', $sizez); //split string into array seperated by ','
             foreach($array as $value) //loop over values
             {
                 //echo $value . PHP_EOL; //print value
                 $sizesOption .='<option>'.$value.'</option>';
              }

$all_list .="<select>
         '.$sizesOption.'
         </select>";

so I thought to put the foreach($array as $value) inside the $all_list .= but that approach is wrong.

could someone please advise on this issue?

any help would be appreciated.

EDIT:

The expected result should be like this:

item one              item two                 item three
small                 large                    small
large                 xxlarge                  xxlarge

However, with my code I get the result like this:

item one              item two                 item three
small                 small                    small
large                 large                    large
                      small                    small
                      large                    large
                                               small
                                               large

so basically, it will get the sizes column from the first item and it will repeat it inside select options for other items exactly like the example above.

5
  • So, if they are repeated, whats the expected result? Add some data regarding what you expect to get. Commented Mar 2, 2015 at 14:59
  • 1
    Don't store multiple values in one field like that ("small,large,xlarge,xxlarge") - google about database normal forms...... Commented Mar 2, 2015 at 15:01
  • There is something strange in the way you concat strings. try: $all_list .="<select>".$sizesOption."</select>"; Commented Mar 2, 2015 at 15:02
  • 5
    If you have separate <select> for each iteration, than you have to reset $sizesOptions first. Better use $sizesOptions = array(); ... $sizesOptions[] = "<option>{$value}</option>"; ... $all_list .= '<select>'.implode("\r\n", $sizesOptions)... Commented Mar 2, 2015 at 15:02
  • @MilanG, adding a new table may or not may be the best idea, depends on the problem and on the dimension of the project Commented Mar 2, 2015 at 15:03

5 Answers 5

2

Since you are generating separate <select> for each iteration, you have to reset $sizeOptions. I suggest using arrays instead of just concatenating strings:

$allList = array();

while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
    $sizesOption = array();
    $sizez = preg_replace('/\.$/', '', $row["sizez"]);

    $array = explode(',', $sizez);

    foreach ($array as $value) {
        $sizesOption[] = "<option>{$value}</option>";
    }

    $all_list[] = '<select>'.implode("\r\n", $sizesOption).'</select>';
}

echo implode("\r\n", $allList);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you mate. a part of your answer helped me to solve the issue and I posted my own answer as well.
0

From what you have posted it looks like you're regenerating $all_list in every iteration of your while loop.

So if you echo $all_list outside of the while loop it will only have the last iteration available - all the other ones having been overwritten during the process of the while loop.

Comments

0

Maybe I am wrong. but as simple as:

$all_list ="<select>".$sizesOption."</select>";

3 Comments

that is exactly what I am doing now mate.
except one .= dot. don't you?
correct Alex. I think I figured it out. I will post an answer in a minute. I'm just a silly man..
0

Close the opening braces for while loop before the '$all_list .="' statement.You are iterating the statement inside while loop.

`

while($row = mysqli_fetch_array($query, MYSQLI_ASSOC))
{
             $id = $row["id"];
             $sizez = $row["sizez"];
             $sizez = preg_replace('/\.$/', '', $sizez); //Remove dot at end if exists
             $array = explode(',', $sizez); //split string into array seperated by ','
             foreach($array as $value) //loop over values
             {
                 //echo $value . PHP_EOL; //print value
                 $sizesOption .='<option>'.$value.'</option>';
              }
}
$all_list .="<select>
         '.$sizesOption.'
         </select>";

`

May be this will work

Comments

0

I figured it out. Thanks to Justinas's answer. I realized that I had to put my php variable inside the while loop.

so all I had to do was to put $sizesOption =array(); inside my while loop and everything works fine now.

P.S. i haven't made any other changes to my code above.

3 Comments

I my opinion this is wrong, because in your case (working with strings) you have to use $sizesOption = ''; and not an empty array.
@skroczek, I agree with you but for some strange reason it works with either array() or ="";. I'm going to change it to =""; in case.
@TERO PHP is strange language, you can reassign variables even if it's type is different.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.