Skip to content
Advertisement

Display data from database using and tags

The main goal I try to get is, the correct display data from the database, let me try to explain with the code.

This is my form code for now:

JavaScript

and the result that I get from this code is a dropdown selection which looks like this

JavaScript

But I need that this dropdown looks like this:

JavaScript

If a region has more than one address, then these address show under these regions

Please don’t judge the code, I know this is not perfect, but everything is working, except the dropdown

Here are two images that explain how it’s now and how it should be

First image how it’s for now
Second image, how it’s should be looking

Thank you everyone for helping me!HTML

Advertisement

Answer

JavaScript

You need to use fetchAll() istead of fetch(). fetchAll() returns the values as an associative array.

Note that fetchAll() works only when PDO::ATTR_DEFAULT_FETCH_MODE is set to PDO::FETCH_ASSOC without which you need to supply PDO::FETCH_ASSOC as argument to fetchAll().

Also note that I have used the alternative syntax for the while loop. You are free to use any syntax but this alternate syntax makes the code much easier to read.

Quoting from PHP Manual

PHP offers an alternative syntax for some of its control structures; namely, if, while, for, foreach, and switch. In each case, the basic form of the alternate syntax is to change the opening brace to a colon (:) and the closing brace to endif;, endwhile;, endfor;, endforeach;, or endswitch;, respectively.

For more information visit PHP: Alternative syntax for control structures – Manual

Edit:

You can try this

JavaScript

This will first fetch the unique regions and then for each row returned it will fetch the name whose region matches the value of $outgroup_record['region']. Then proceed to output the result in a <option> tag. Modify the queries as per your need.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement