Populate successive drop downs according to preceding drop down content

My SQL table has 8 columns 1. ID, 2. brand, 3. model, 4. type, 5. OEM, 6. description, 7. mono_yield, 8. colour_yield

I have 3 drop downs and I wish to populate the second one according to the first and third one according to the first and second.

1: Printer Brand
2. Printer Model
3. Printer Type

Here’s the code:

/*

  • Populate form filter
    */
    $stmt = $db->query(‘SELECT * FROM printers GROUP BY type ORDER BY type ASC’);

if($stmt === false) {
echo ‘<p>Unable to populate required data to build page.</p>’;
exit;
}

while($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
$tpl[‘filter’][‘type’][‘#values’] = array(
‘label’ => $row[‘type’]
,‘value’ => $row[‘type’]
,‘selected’ => isset($_GET[‘filter’],$_GET[‘filter’][‘type’]) && $_GET[‘filter’][‘type’] == $row[‘type’]
);
}

/*

I have no idea how to get all three working together.

Any help greately apprciated.

It would be preferable to use Ajax if the data are not static of those drop down. Just search the web for “dependent drop down using php” or something like “dropdown using ajax”… You will find a lot examples…

thanks, on further scratching of the head i now don’t need the other 2 drop downs to be dependent, alas i have another problem:

what i am trying to do now is simply show the results of three dropdown queries that connect to mysql database.

I have combined the template variables into one array but only the last dropdown box is --Select Printer Model-- is showing up. And that is showing “Select Printer Brand” instead of “Select Printer Model” so I have an issue there.

The page is here: http://www.justcheaptoner.com/dropdown/index.php

You can see how it works but I need to have the select options for the first two.

I have separated the files as follows:

index.php

  • includes the following 5 files:

  • header.php

  • brand.php

  • type.php

  • model.php

  • data_grid.php

In header.php here are the template variables:

/*

  • Template variables
    */

$tpl = array(
‘filter’=>array(
#action’ => $_SERVER[‘SCRIPT_NAME’]
,‘#method’ => ‘get’
,‘brand’,‘type’,‘model’ => array(
#values’=>array(
array(‘value’=>‘’,‘label’=>‘–Select Printer Brand–’,‘–Select Printer Type–’,‘–Select Printer Model–’)

        )
    )
)
,'grid'=&gt;array(
    'printers'=&gt;array()
)

);

As always any help most appreciated.

Are you using any framework ??? If not then share your header.php which i assume display the dropdown of printers…

thanks mihirpate183, umm how do i share the header.php? u mean print it here?

Yeh paste code if there is not option to attach…Evn am new to sitepoint :slight_smile:

ok thanks, here’s the header:

<?php
/*

  • Testing configuration
    */
    define(‘ENVIR’,0);
    define(‘ENVIR_DEV’,0);
    define(‘ENVIR_LIVE’,1);

/*

  • Data base credentials (replace this with your db credentials)
    */
    define(‘DB_USER’,‘xxxxx’);
    define(‘DB_PWD’,‘xxxxx’);
    define(‘DB_HOST’,‘localhost’);
    define(‘DB_NAME’,‘xxxxx’);

/*

  • Connect to the database
    */
    try {
    $db = new PDO(‘mysql:host=’.DB_HOST.‘;dbname=’.DB_NAME,DB_USER,DB_PWD);
    } catch(PDOExeption $e) {

    if(ENVIR == ENVIR_DEV) {
    echo ‘<p>’,$e->getMessage(),‘</p>’;
    }

    echo ‘<p>Unable to connect to database<p>’;
    exit;
    }

/*

  • Template variables
    */

$tpl = array(
‘filter’=>array(
#action’ => $_SERVER[‘SCRIPT_NAME’]
,‘#method’ => ‘get’
,‘brand’,‘type’,‘model’ => array(
#values’=>array(
array(‘value’=>‘’,‘label’=>‘–Select Printer Brand–’,‘–Select Printer Type–’,‘–Select Printer Model–’)

        )
    )
)
,'grid'=&gt;array(
    'printers'=&gt;array()
)

);

Are you using any framework or its core php ??? I dont assure you that i will solve your problem but will try…How do you use this $tpl ?? You have defined in template but where do you use it…show me the code where $tpl is used or echoed…

ok this is the result grid:

<!-- data grid template –>
<style type=“text/css”>
body,td,th {
font-family: Arial, Helvetica, sans-serif;
font-size: 10pt;
}
</style>

<table width=“100%” border=“1”>
<caption>
<strong>Just Cheap Toner - for all your toner needs
<p>
</strong>
</caption>

&lt;thead&gt;
    &lt;tr&gt;
        &lt;th bgcolor="#99FFFF"&gt;Brand&lt;/th&gt;
        &lt;th bgcolor="#99FFFF"&gt;Type&lt;/th&gt;
        &lt;th bgcolor="#99FFFF"&gt;Model&lt;/th&gt;
        &lt;th bgcolor="#99FFFF"&gt;OEM&lt;/th&gt;
        &lt;th bgcolor="#99FFFF"&gt;Description&lt;/th&gt;
        &lt;th bgcolor="#99FFFF"&gt;Mono Yield&lt;/th&gt;
        &lt;th bgcolor="#99FFFF"&gt;Colour Yield&lt;/th&gt;
  &lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
    &lt;?php
        if(!empty($tpl['grid']['printers'])) {
            foreach($tpl['grid']['printers'] as &$printers) {
                printf(
                    '&lt;tr&gt;
                        &lt;td&gt;%s&lt;/td&gt;
                        &lt;td&gt;%s&lt;/td&gt;
						&lt;td&gt;%s&lt;/td&gt;
						&lt;td&gt;%s&lt;/td&gt;
						&lt;td&gt;%s&lt;/td&gt;
						&lt;td&gt;%s&lt;/td&gt;
						&lt;td&gt;%s&lt;/td&gt;
                     &lt;/tr&gt;'
                     ,htmlentities($printers['brand'])
					 ,htmlentities($printers['type'])
                     ,htmlentities($printers['model'])
					 ,htmlentities($printers['oem'])
					 ,htmlentities($printers['description'])
					 ,htmlentities($printers['mono_yield'])
					 ,htmlentities($printers['colour_yield'])
                );
            }
        } else {
            echo '&lt;tr&gt;&lt;td colspan="2"&gt;No names available&lt;/td&gt;&lt;/tr&gt;';
        }
    ?&gt;
&lt;/tbody&gt;

</table>

sorry, not using any framework no

Well you’re using a templating scheme.

This, i believe, is your problem:


$tpl = array(
'filter'=>array(
'#action' => $_SERVER['SCRIPT_NAME']
,'#method' => 'get'
,'brand','type','model' => array(
'#values'=>array(
array('value'=>'','label'=>'--Select Printer Brand--','--Select Printer Type--','--Select Printer Model--')

)
)
)
,'grid'=>array(
'printers'=>array()
)
);

Now… lets rearrange that a bit, make it slightly more readable, and see if we can see the problem.


$tpl = array(
	'filter'=>array(
		'#action' => $_SERVER['SCRIPT_NAME'],
		'#method' => 'get',
		'brand',
		'type',
		'model' => array(
			'#values'=>array(
				array(
				 'value'=>'',
				 'label'=>'--Select Printer Brand--',
				 '--Select Printer Type--',
				 '--Select Printer Model--'
				)
			)
		)
	),
'grid'=>array(
	'printers'=>array()
	)
);

Answer: You’ve only given data about the ‘model’ item, so it only displays that one.

I THINK what you were TRYING to do should look more like this:

$tpl = array(
	'filter'=>array(
		'#action' => $_SERVER['SCRIPT_NAME'],
		'#method' => 'get',
		'brand' => array(
			'#values'=>array(
				array(
				 'value'=>'',
				 'label'=>'--Select Printer Brand--'
		        ),
				array(
				 'value'=>'demobrand',
				 'label'=>'Demo Brand',
				)
			)
		),
		'type' => array(
			'#values'=>array(
				array(
				 'value'=>'',
				 'label'=>'--Select Printer Type--',
				),
				array(
				 'value'=>'demotype',
				 'label'=>'Demo Type',
				)				
			)
		
		),
		'model' => array(
			'#values'=>array(
				array(
				 'value'=>'',
				 'label'=>'--Select Printer Model--',
				),
				array(
				 'value'=>'demomodel',
				 'label'=>'Demo Model',
				)				
			)
		)
	),
'grid'=>array(
	'printers'=>array()
	)
);

This is of course based on not seeing your template engine’s code.

thanks starlion, the values are coming directly from the sql database