I noticed w/ version 5.1.2, the behavior of
list() has changed (this occurred at some point between version 5.0.4
and 5.1.2). When re-using a variable name in list() that list() is
being assigned to, instead of the values being assigned all at once, the
reused variable gets overwritten before all the values are read.
Here's an example:
** disclaimer: obviously this is sloppy code, but I want to point out
the behavior change (in case anyone else comes across similar code) **
<?
$data = array();
$data[] = array("value1", "value2", "value3", "value4");
$data[] = array("value1", "value2", "value3", "value4");
$data[] = array("value1", "value2", "value3", "value4");
$data[] = array("value1", "value2", "value3", "value4");
foreach($data as $record)
{
list($var1, $var2, $var3, $record) = $record;
echo "var 1: $var1, var 2: $var2, var 3: $var3, record: $record\\n";
}
?>
OUTPUT on version 5.0.4:
var 1: value1, var 2: value2, var 3: value3, record: value4
var 1: value1, var 2: value2, var 3: value3, record: value4
var 1: value1, var 2: value2, var 3: value3, record: value4
var 1: value1, var 2: value2, var 3: value3, record: value4
OUTPUT on version 5.1.2:
var 1: v, var 2: a, var 3: l, record: value4
var 1: v, var 2: a, var 3: l, record: value4
var 1: v, var 2: a, var 3: l, record: value4
var 1: v, var 2: a, var 3: l, record: value4Monday, 23 April 2012
php for list array
Friday, 13 April 2012
Ajax Tutorial: Dynamic Loading of ComboBox using jQuery and Ajax in PHP
Table Structure
CREATE TABLE IF NOT EXISTS `ajax_categories` ( `id` int(11) NOT NULL AUTO_INCREMENT, `category` varchar(50) NOT NULL, `pid` int(11) NOT NULL, PRIMARY KEY (`id`) )
<?php if($_REQUEST) { $id = $_REQUEST['parent_id']; $query = "select * from ajax_categories where pid = ".$id; $results = mysql_query( $query);?> <select name="sub_category" id="sub_category_id"> <option value="" selected="selected"></option> <?php while ($rows = mysql_fetch_assoc(@$results)) {?> <option value="<?php echo $rows['category'];?> ID=<?php echo $rows['id'];?>"><?php echo $rows['category'];?></option> <?php }?> </select> <?php }?>
<div class="both"> <h4>Select Category</h4> <select name="search_category" id="search_category_id"> <option value="" selected="selected"></option> <?php $query = "select * from ajax_categories where pid = 0"; $results = mysql_query($query); while ($rows = mysql_fetch_assoc(@$results)) {?> <option value="<?php echo $rows['id'];?>"><?php echo $rows['category'];?></option> <?php }?> </select> </div> <div class="both"> <h4 id="show_heading">Select Sub Category</h4> <div id="show_sub_categories" align="center"> <img src="loader.gif" style="margin-top:8px; float:left" id="loader" alt="" /> </div> </div>
Dropdown Box from an Array
<?php | // Array contents |
| $array1 = array('Sydney','Melbourne','Brisbane','Tasmania','Adelaide','Perth','Darwin','ACT'); |
|
| // Values from array 1 |
| echo'<select name="cities">'; |
| // For each value of the array assign variable name "city" |
| foreach($array1 as $city){ |
| echo'<option value="'.$city.'">'.$city.'</option>'; |
| } |
| echo'</select>'; |
| ?> |
Create dynamic combobox with dynamic value
Combobox is one element in a form that is often used in applications –
the application registration, generally combobox is used to select one
option where there are many options available. Value in the combobox can
actually be written directly in the html, but, what if the data you
want to appear very much or depend on certain conditions?
Suppose, we want to show the choice of cities – cities that exist within the Indonesian state, the city is also a city which appears to be in Indonesia.
For example, a user was coming from England, the town which is also a city which appears to be in the UK.
We need dynamic combobox
Then, here we go.
Step 1 : Prepare the Database
Suppose, we want to show the choice of cities – cities that exist within the Indonesian state, the city is also a city which appears to be in Indonesia.
For example, a user was coming from England, the town which is also a city which appears to be in the UK.
We need dynamic combobox
Then, here we go.
Step 1 : Prepare the Database
- Create database named db_tutorial
- Prepare a table named tb_kota, with structure like picture below,
- Insert some sample data like below,
- Done!
- Create folder named tutorphp in your document root
- Save all file in this tutorial in this folder.
- Type the following script,
<?php $host = "localhost"; $user = "root";//adjust according to your mysql setting $pass = ""; //adjust according to your mysql setting, i use no password here $dbName = "db_tutorial"; mysql_connect($host, $user, $pass); mysql_select_db($dbName) or die ("Connect Failed !! : ".mysql_error()); ?> - Save with the name connect.php
- Type the following script,
<form name='form' method='post' action='prosescombo.php'> <h3> Choose Your City</h3> <select name="kota"> <option value=0 selected>- city -</option> <?php include 'connect.php'; $q = mysql_query("select * from tb_kota where negara = 'Indonesia' "); //choose the city from indonesia only while ($row1 = mysql_fetch_array($q)){ echo "<option value=$row1[kota]>$row1[kota]</option>"; } ?> </select> <input type="submit" name="submit" value="Submit"> </form> - Save with the name formcombo.php
- Type the following script,
<?php $kota = $_POST['kota']; if ($kota == '0') { echo "anda belum memilih"; } else echo "anda memilih ".$kota; ?> - Save with the name prosescombo.php
combo box using arrays
<form name="order_form" action="http://localhost/myforum/index.php" method="GET">
<input type="hidden" value="1" name="forum" />
<select name="order_by" style="margin-left:5px;">
<option name="order_by_option" value="Array"/>Order By: Ascending</option>
<option name="order_by_option" value="Array"/>Order By: Descending</option>
</select>
<input type="submit" value="Order" />
</form>
Basically all i want to do is have Order By: Ascending and the relevent option value to be asc
or Order: By Descending and its option value to be desc. I thought I could use an array like:
$order_name = array('Ascending', 'Descending');$order_value = array('asc' => 'Ascending', 'desc' => 'Descending');
and then have a foreach loop to echo the options for how ever many there are in the array like so:
foreach ($order_name as $i)
<input type="hidden" value="1" name="forum" />
<select name="order_by" style="margin-left:5px;">
<option name="order_by_option" value="Array"/>Order By: Ascending</option>
<option name="order_by_option" value="Array"/>Order By: Descending</option>
</select>
<input type="submit" value="Order" />
</form>
Basically all i want to do is have Order By: Ascending and the relevent option value to be asc
or Order: By Descending and its option value to be desc. I thought I could use an array like:
$order_name = array('Ascending', 'Descending');$order_value = array('asc' => 'Ascending', 'desc' => 'Descending');
and then have a foreach loop to echo the options for how ever many there are in the array like so:
foreach ($order_name as $i)
{
echo "<option name=\"order_by_option\" value=\"{$order_value}\"/>";
echo "Order By: {$i}";
echo "</option>";
}
Php array to flash as 2 fill combo box problem
I seem to keep getting a problem to fill a combo box in flash CS3 (actionscript 2.0)
I made a php file wich reads data from a mysql table and then puts it in an array (this works fine. I seperated it to data and label(name) and afterwards exported it to read it in flash as data and label. But when I read it in flash nothing happens, I keep getting 'undefined'...
Here's my code:
Combolist.php:
<?
//connect to database
mysql_connect("localhost","username","passwoord") or die ("didn't connect to mysql" . mysql_error());
mysql_select_db("combolistinfo") or die ("no database" . mysql_error());
//make query
$query = "SELECT list FROM combo";
$result = mysql_query( $query ) or die ("didn't query" . mysql_error());
$num = mysql_num_rows( $result );
$ComboName = array();
$ComboData = array();
$j = 0;
while ($row = mysql_fetch_row($result)){
$ComboName[$j] =$row[0];
$ComboData[$j] =$row[0];
$data[] = $ComboName[$j].":".$ComboData[$j];
$j++;
}
print implode("\n",$data);
mysql_close($DBConn);
?>
so this above works properly!!
flash fill combolist.fla:
tmp=new Array();
tmp2=new Array();
//make new loadvar
myDataCombo = new LoadVars();
//load php file
myDataCombo.load("Combolist.php");
//get data from php file
myDataCombo.onData = function(raw) {
//split data to fill array
tmp = raw.split("\n");
for (var i = 0; i<tmp.length; i++) {
tmp2 = tmp[i].split(":");
_root.cmblist.addItem(tmp2[0],tmp2[1]);
}
};
//execute function
traceCombo();
//function to read data and label that is selected
function traceCombo(com) {
trace("Selected Data: "+com.getSelectedItem().data);
trace("Selected Label: "+com.getSelectedItem().label);
trace("--------------------------------------");
}
I made a php file wich reads data from a mysql table and then puts it in an array (this works fine. I seperated it to data and label(name) and afterwards exported it to read it in flash as data and label. But when I read it in flash nothing happens, I keep getting 'undefined'...
Here's my code:
Combolist.php:
<?
//connect to database
mysql_connect("localhost","username","passwoord") or die ("didn't connect to mysql" . mysql_error());
mysql_select_db("combolistinfo") or die ("no database" . mysql_error());
//make query
$query = "SELECT list FROM combo";
$result = mysql_query( $query ) or die ("didn't query" . mysql_error());
$num = mysql_num_rows( $result );
$ComboName = array();
$ComboData = array();
$j = 0;
while ($row = mysql_fetch_row($result)){
$ComboName[$j] =$row[0];
$ComboData[$j] =$row[0];
$data[] = $ComboName[$j].":".$ComboData[$j];
$j++;
}
print implode("\n",$data);
mysql_close($DBConn);
?>
so this above works properly!!
flash fill combolist.fla:
tmp=new Array();
tmp2=new Array();
//make new loadvar
myDataCombo = new LoadVars();
//load php file
myDataCombo.load("Combolist.php");
//get data from php file
myDataCombo.onData = function(raw) {
//split data to fill array
tmp = raw.split("\n");
for (var i = 0; i<tmp.length; i++) {
tmp2 = tmp[i].split(":");
_root.cmblist.addItem(tmp2[0],tmp2[1]);
}
};
//execute function
traceCombo();
//function to read data and label that is selected
function traceCombo(com) {
trace("Selected Data: "+com.getSelectedItem().data);
trace("Selected Label: "+com.getSelectedItem().label);
trace("--------------------------------------");
}
Subscribe to:
Comments (Atom)