Jump to content

PHP and Dropdown boxes with MySQL


Steve_NI

Recommended Posts

Hi

 

I'm looking for some help.

I am looking to query a mysql database and get distinct elements from a particular column and have the result set populate a dropdown box.

After that I am looking for the ability of a user to click on one of those drop down options and from that it will produce all rows in the table that have that element in it.

 

I'm new to php but the site I am developing is using PHP.

 

First thing I need to know is that if this is even possible to do and if so would anyone have any tutorials that would guide me?

 

thanks!

Link to comment
Share on other sites

Ok I have managed to code it so that I can loop through the database and it has given me my drop down box populated with unique references from the database.

Here is the code:

$server = 'localhost';
$user='root';
$pass='';
$db = 'finance_checker';

$mysqli = mysqli_connect($server, $user, $pass, $db);

$query = $mysqli->query("SELECT distinct `catagory` FROM `transactions`");

while($array[]= $query->fetch_object());

array_pop($array);

?>

        
        <h3>Payments Made by Catagory</h3>
        <select name="the_name">
            <?php foreach ($array as $option): ?>
            <option value="<?php echo $option->Transaction; ?>"><?php echo $option -> catagory;?></option>
            <?php endforeach; ?>
        </select>
        
        <?php
        $query-> close();
        ?>

This now gives me a dropdown that looks something like:

Food

Clothes

Entertainment

Petrol

 

Now what I would like is for my user to be able to click on one of these dropdown options lets say food and this then will link to a search which will for example "Select `payee`, `amount`, `date` From `transactions` Where `catagory` = Food" and then produce a new table of information of all food transactions ie:

Payee   Amount   Date

Tesco     20.45      26/10/2013

Boots      12.32      25/10/2013

 

How would I go about linking the dropdown options back to the database?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.