Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 update data using php and javascript

views
     
nyem
post May 25 2009, 10:19 AM

Enthusiast
*****
Senior Member
751 posts

Joined: Jan 2007


QUOTE(Rowan @ May 24 2009, 01:32 PM)
CODE
$rs_brand = mysql_query("SELECT `brandId`,`brandName` FROM `brand`");
while ($row0 = mysql_fetch_array($rs_brand)) {
  $rs_model = mysql_query(sprintf("SELECT `modelId`,`modelName` FROM `model` WHERE `brandId` = '%u'",$row0[0]));
     while ($row1 = mysql_fetch_array($rs_model)) {
        ...
     }
  }
}

This is not a recommended way of doing database query. If you have 100 brands you are going to query the database 100 times.

QUOTE(yoyorock @ May 24 2009, 10:17 AM)
if (navigator.appName.indexOf("Microsoft") == 0) {
IE = true;
} else if (navigator.appName.indexOf("Netscape") == 0) {
FF = true;
}

I wonder why the need to detect Netscape. Anyone still using Netscape?

Here's a revamped code that should work on IE, Firefox, Opera, Chrome and Safari. I don't have Netscape to test this.
CODE

<select name="jenama" size="1" id="brand" onChange="auto_select(Brands,'jenama','model')">
</select> | <select name="model" size="1" style="width:150px"></select>

<script>
var Brands = {

<?php
mysql_connect("localhost","username","password");
mysql_select_db("test");

$sql = <<<EOT
  SELECT CONCAT( "'", a.brandName, "':['", GROUP_CONCAT( b.modelName SEPARATOR "','" ), "']")
  FROM brand a, model b
  WHERE a.brandId = b.brandId
  GROUP BY a.brandId
  ORDER BY a.brandName

EOT;

$rows = array();
$dbquery = mysql_query($sql);
while ($row = mysql_fetch_array($dbquery)) {
  array_push($rows, $row[0]);
}
echo( implode(",\n", $rows) );
?>

};

function init_select(arr, list1name)   {

  // remove all existing options from List1
  var List1 = document.getElementsByName( list1name )[0];
  while (List1.options.length > 0)
     List1.options[List1.options.length-1] = null;

  // populate options
   for (var i in arr)
     List1.options[ List1.options.length ] = new Option(i, i, false, false);

}

function auto_select(arr, list1name, list2name)   {

  // remove all existing options from List2
  var List2 = document.getElementsByName( list2name )[0];
  while (List2.options.length > 0)
     List2.options[List2.options.length-1] = null;

  // populate options
  var List1 = document.getElementsByName( list1name )[0];
  for (var i in arr)
     if (List1.options[ List1.selectedIndex ].text == i)
        for (var k=0;k<arr[i].length;k++)
           List2.options[ List2.options.length ] = new Option(arr[i][k], arr[i][k], false, false);
}

init_select(Brands,'jenama');
auto_select(Brands,'jenama','model');

</script>


This post has been edited by nyem: May 25 2009, 10:20 AM

 

Change to:
| Lo-Fi Version
0.0158sec    0.60    6 queries    GZIP Disabled
Time is now: 30th November 2025 - 10:26 PM