SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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....

  3. #3
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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'=>array(
    'printers'=>array()
    )
    );

    As always any help most appreciated.

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you using any framework ??? If not then share your header.php which i assume display the dropdown of printers...

  5. #5
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks mihirpate183, umm how do i share the header.php? u mean print it here?

  6. #6
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeh paste code if there is not option to attach....Evn am new to sitepoint

  7. #7
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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'=>array(
    'printers'=>array()
    )
    );

  8. #8
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.....

  9. #9
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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>

    <thead>
    <tr>
    <th bgcolor="#99FFFF">Brand</th>
    <th bgcolor="#99FFFF">Type</th>
    <th bgcolor="#99FFFF">Model</th>
    <th bgcolor="#99FFFF">OEM</th>
    <th bgcolor="#99FFFF">Description</th>
    <th bgcolor="#99FFFF">Mono Yield</th>
    <th bgcolor="#99FFFF">Colour Yield</th>
    </tr>
    </thead>
    <tbody>
    <?php
    if(!empty($tpl['grid']['printers'])) {
    foreach($tpl['grid']['printers'] as &$printers) {
    printf(
    '<tr>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    </tr>'
    ,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 '<tr><td colspan="2">No names available</td></tr>';
    }
    ?>
    </tbody>
    </table>

  10. #10
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry, not using any framework no

  11. #11
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    Well you're using a templating scheme.

    This, i believe, is your problem:

    Code:
    $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.
    Code:
    $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:
    Code:
    $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.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  12. #12
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks starlion, the values are coming directly from the sql database


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •