Jump to content


Photo

How to populate table with sql data's based on value selected from combo


  • Please log in to reply
2 replies to this topic

#1 saravanataee

saravanataee

    Member

  • Members
  • PipPip
  • 25 posts

Posted 19 January 2013 - 02:47 AM

Dear All,
I have a scenario where i want to display database field's with values after selecting the id from combo.

For eg:

I am having a stock table with stcid as primary key.

I have other fields in stock table such as itemname, qty,package,rate,disc,amount etc.

What i m looking to do is,

In a php form, i want to have a combo alone initially that has to be loaded with available stcid's from db.

Based on the id that is selected i want to load the fields dynamically with values from db for that particular id.

The point is, i want to show the table field with label and boxes only after i select the id. which means initially my php page will have only the combo box.

How can i do this.. Any examples or methods to achieve this??

Thanks!

#2 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,187 posts
  • LocationCheshire, UK

Posted 19 January 2013 - 07:36 AM

here's a sample

<?php
$mysqli = new mysqli('localhost', 'root', 'maxwell', 'clem');

/**********************
* Get combo item list
* *********************/
$sql = "SELECT id, itemname
	    FROM stock
	    ORDER BY itemname";
$res = $mysqli->query($sql);
$options = "<option value=''>- Select an item -</option>\n";
while (list($id, $desc) = $res->fetch_row()) {
    $select = (isset($_POST['id']) && $_POST['id']==$id) ? "selected='selected'":'';
    $options .= "<option $select value='$id'>$desc</option>\n";
}
$res->free();

/************************
* Get selected item data
*************************/
$itemdata = 'No item selected';
if (isset($_POST['id'])) {
    $id = intval($_POST['id']);
    $sql = "SELECT *
		    FROM stock
		    WHERE id = $id";
    $res = $mysqli->query($sql);
    $row = $res->fetch_assoc();
    if ($row) {
	    $itemdata = "<table cellspacing='4'>\n";
	    foreach ($row as $fname=>$val) {
		    $itemdata .= "<tr><td>$fname :</td><td>$val</td></tr>\n";
	    }
	    $itemdata .= "</table>\n";     
    }
}
?>

<form method="post">
    Stock item <br>
    <select name="id">
	    <?php echo $options; ?>
    </select><br>
    <input type="submit" name="btnSubmit" value="Submit">
</form>
<hr>
<?php echo $itemdata ?>

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#3 demon1

demon1

    Newbie

  • New Members
  • Pip
  • 1 posts

Posted 30 September 2013 - 02:39 PM

Thanks for the sample code, found it very usefull






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com