PHP - How Can I Populate A Usa States Select Box With An Array Call?
I am having a helluva time trying to populate my state option box with a state array.
I have the array set but now how does this get called into the foreach block where the select box is? I can populate it i guess with options manually but I want to get the array to display in the box instead. I tried everything with the minimal experience I have so I need a little help, I got the select box up and displaying where I want it but I cant populate it with an array call. See foreach block near the end of the code: Thanks a bunch! here is my include file code so far: <?php /* Program name: form_test_state.inc * Description: Defines a form that collects a user's * name and mailing address. */ $rows = array( "first_name" => "First Name", "last_name" => "Last Name", "phone" => "Phone Number", "city" => "City", "state" => "State", "address" => "Address", "zip_code" => "ZIP Code", "e_mail" => "E-Mail"); $states_list = array('AL'=>"Alabama",'AK'=>"Alaska",'AZ'=>"Arizona",'AR'=>"Arkansas",'CA'=>"California",'CO'=>"Colorado",'CT'=>"Connecticut",'DE'=>"Delaware",'DC'=>"District Of Columbia",'FL'=>"Florida",'GA'=>"Georgia",'HI'=>"Hawaii",'ID'=>"Idaho",'IL'=>"Illinois", 'IN'=>"Indiana", 'IA'=>"Iowa", 'KS'=>"Kansas",'KY'=>"Kentucky",'LA'=>"Louisiana",'ME'=>"Maine",'MD'=>"Maryland", 'MA'=>"Massachusetts",'MI'=>"Michigan",'MN'=>"Minnesota",'MS'=>"Mississippi",'MO'=>"Missouri",'MT'=>"Montana",'NE'=>"Nebraska",'NV'=>"Nevada",'NH'=>"New Hampshire",'NJ'=>"New Jersey",'NM'=>"New Mexico",'NY'=>"New York",'NC'=>"North Carolina",'ND'=>"North Dakota",'OH'=>"Ohio",'OK'=>"Oklahoma", 'OR'=>"Oregon",'PA'=>"Pennsylvania",'RI'=>"Rhode Island",'SC'=>"South Carolina",'SD'=>"South Dakota",'TN'=>"Tennessee",'TX'=>"Texas",'UT'=>"Utah",'VT'=>"Vermont",'VA'=>"Virginia",'WA'=>"Washington",'WV'=>"West Virginia",'WI'=>"Wisconsin",'WY'=>"Wyoming"); $submit = "Submit mailing information"; ?> <html> <head> <style type='text/css'> <!-- body { background: #42413C; color: #000; font-family: Tahoma, Geneva, sans-serif; font-size: 90%; } form { margin: 1em 2 2 2; padding: 1; width: 1100px; margin-left: 35%; } .field {padding-bottom: 1em; } label { font-weight: bold; float: left; width: 10%; margin-right: 1em; text-align: right; background-color: #9F6; padding-right: 5px; } select {margin-bottom: 1em;} #submit { margin-left: 15%; } h3 { width: 500px; margin-left: 35%; } --> </style> </head> <body> <h3>Please enter your mailing information below:</h3> <?php /* loop that displays the form */ echo "<form action='checkBlankOnly_latest.php' method='post'>"; foreach($rows as $field => $label) if($field == "state") { echo "<label for='$field'>$label</label> <select name='State'> <<<< Insert $states_list array here I am trying to figure out >>>> <option value='state'>Alabama </option> </select> "; } else { echo "<div class='field'><label for='$field'>$label</label> <input id='$field' name='$field' type='text' value='".@$$field."' size='25%' maxlength='65' /></div>\n"; } echo "<div id='submit'> <input type='submit' value='$submit'></div>"; ?> </form> </body> </html> Similar Tutorialshi, I was going through this tutorial: http://www.electrictoolbox.com/json-data-jquery-php-mysql/ here is my HTML Code: [Select] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> <script language="javascript" type="text/javascript"> function populateFruitVariety() { $.getJSON('search-by.php', {fruitName:$('#fruitName').val()}, function(data) { var select = $('#fruitVariety'); var options = select.attr('options'); $('option', select).remove(); $.each(data, function(index, array) { options[options.length] = new Option(array['variety']); }); }); } $(document).ready(function() { populateFruitVariety(); $('#fruitName').change(function() { populateFruitVariety(); }); }); </script> </head> <body> <form> Search by: <select name="name" id="fruitName"> <option>Please Select</option> <option id="Town" value="Town">Town</option> <option id="County" value="County">County</option> </select> Variety: <select name="variety" id="fruitVariety"> </select> </form> </body> </html> here is my PHP Code: [Select] $dbhost = "xxx"; $dbname = "xxx"; $dblogin = "xxx"; $dbpass = "xxx"; function dbConnect() { global $dbhost; global $dbname; global $dblogin; global $dbpass; $db = mysql_connect($dbhost, $dblogin, $dbpass) or die("could not connect to database: ".mysql_error()); mysql_select_db($dbname) or die("could not select database"); return $db; } function dbClose($db) { mysql_close($db); } // basis code ^^ $db = dbConnect(); $rows = array(); if(isset($_GET['Town'])) { $query = "SELECT DISTINCT rsCounties FROM pubs"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo json_encode($rows); } } if(isset($_GET['County'])) { $stmt = $pdo->prepare("SELECT rsCounty FROM pubs"); $stmt->execute(array($_GET['rsCounty'])); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); } echo json_encode($rows); Can someone please help? I have the JQuery working but not the PHP?! here is a link to what I have at the moment: http://www.mypubspace.com/dashnew/index.html thanks Hi there, i am relatively new to php, mysql, css etc but learning fast. My problem is such; i have a php file which is doing a SELECT mysql_query, WHILE results to strings, then ECHO the resulting rows to produce a list formatted using <table> and finally this <table> is inside a <form> which will POST the changes back to the specific database.tble.row. I wish to have a drop down menu within the <form><table> which will be populated from a separate database.table. I have accomplished the drop down menu outside the <?php ?> tags inside <form><table> which POSTS to a php file but my problem is to add the populating drop down menu inside <?php ?> an already ECHOing resulting rows from the sql query. i.e <?php blurb and stuff ?> <form><table><tr><td> <select name etc> <?php $result = mysql_query("SELECT * FROM tbl WHERE string = tble.rw ORDER BY column"); while($row=mysql_fetch_array($result)){ echo "<OPTION VALUE=".$row['column'].">".$row['column']."</OPTION>"; } ?> </select> WORKS!!!! but placing this inside <?php $x =mysql_query[select] while {strings = conditions; echo ("<form><table><tr><td> insert populated drop menu here </td> etc "); echo"";}?> doesnt work and just leaves the select drop menu blank Hoep you understand my problem. I do not think i can attached the population WHILE loop to a string and just insert the string to the form but maybe i am wrong. thanks in advance and if you go tthis far reading you must be on lots and lots of coffee zark Hey there I just started out with PHP so it might be a trivial problem. I want to populate an array with values by a recursive function but i don't get it the way i want it to have. Target array (written in python syntax): myArray = { key : [ [a1, a2, a3, a4], [b1, b2, b3 ,b4] ], key2 : [ [c1, c2, c3, c4], [d1, d2, d3, d4] ] } So far i created a recursive function to collect all data i need for the array but i don't get the right syntax to achieve my target array. What i have so far is following code (snippet): function get_data($main_dir, $items=[], $myArray=[], $depth=0) { global $items; if ($myArray == NULL && depth == 0) { $myArray = array(); } $depth++; $dirHandle = opendir($main_dir); while ($file = readdir($dirHandle)){ if(is_dir($main_dir.$file) && $file != "." && $file != "..") { $new_dir = $main_dir.$file."/"; $items = get_data($new_dir, $items, $myArray, $depth); $myArray = $items[1] $items = $items[0] } else { splitted_dir = preg_split("/\//", $main_dir); key = splitted_dir[2]; file_information = read_out_file($main_dir.$file); information_1 = file_information[0]; information_2 = file_information[1]; myArray = array_merge($myArray, array($key, array($key, $information_1, $information_2, $main_dir, $file)) } } } The array contains all informations i need but the structure of it is not useable. Current output: Array ( [0] => key1 [1] => Array ( [0] => key1 [1] => information_1 [2] => information_2 [3] => ./path/ [4] => filename ) [2] => key1 [3] => Array ( [0] => key1 [1] => information_1 [2] => information_2 [3] => ./path/ [4] => filename ) [4] => key1 [5] => Array ( [0] => key1 [1] => information_1 [2] => information_2 [3] => ./path/ [4] => filename ) [6] => key2 [7] => Array ( [0] => key2 [1] => information_1 [2] => information_2 [3] => ./path/ [4] => filename ) [8] => key2 [9] => Array ( [0] => key2 [1] => information_1 [2] => information_2 [3] => ./path/ [4] => filename ) ) 1 ## IDK why this "1" at the end gets printed out... But what i want to get is this: Array ( [key1] => Array ( Array ( [0] => key1 [1] => information_1 [2] => information_2 [3] => ./path/ [4] => filename ) ) Array ( [0] => key1 [1] => information_1 [2] => information_2 [3] => ./path/ [4] => filename ) ) ) [key2] => Array ( Array ( [0] => key2 [1] => information_1 [2] => information_2 [3] => ./path/ [4] => filename ) ) Array ( [0] => key2 [1] => information_1 [2] => information_2 [3] => ./path/ [4] => filename ) ) ) )
At the end i want to sort the output by alphabetic order of the keys which i should achieve with sort() i guess?
Greetings Edited August 10, 2019 by Sandy_85I have a table that allows users to add rows, depending on how much data they need to insert. upon submit I'd like to populate another table for review. Now I can generate the data, but I'm having trouble putting the array into a table. Here is the output from the initial page: Code: [Select] Array ( [quantity] => Array ( [0] => 1 [1] => 2 [2] => 3 ) [description] => Array ( [0] => 1 [1] => 2 [2] => 3 ) [unit_cost] => Array ( [0] => 1 [1] => 2 [2] => 3 ) [shipping] => 4 [tax] => 5 [review_po] => Review ) I need it to look like this: Quantity | Description | Unit Cost 1 | 1 | 1 2 | 2 | 2 3 | 3 | 3 Tax | 4 Ship | 5 I think it's a foreach statement, but I'm not having any luck.. If someone could point me in the right direction??? I am currently creating a form and I want to populate a drop down selection menu with data from two fields in a form. For example, I want it to pull the first and last name fields from a database to populate names in a drop down menu in a form. I want the form to submit to the email address of the person selected in the drop down. Is this possible to do? The email is already a field in the record of the person in the database. Can anyone give me some pointers or advice on how I should go about setting up the "Select" box drop down? I am not sure how to code it to do what I am wanting. Any links to relevant help would be appreciated too. Thanks in advance! Code: [Select] <? $i=1; $analysts[$i] = array(0=>'alpha'); $regions[$i] = array(0=>'beta'); $countries[$i] = array(0=>'gamma'); $provinces[$i] = array(0=>'delta'); $events[$i] = array(0=>'epsilon'); $selectable_document_elements = array ( 0=> analysts, 1=> regions, 2=> countries, 3=> provinces, 4=> events ); foreach($selectable_document_elements as $value) { echo $value; print_r($$value[$i]); echo "</br>"; } ?> I can't seem to call $$value[$i], however when I leave off the [$i] part Code: [Select] print_r($$value); I get the individual elements... Code: [Select] analystsArray ( [1] => Array ( [0] => alpha ) ) regionsArray ( [1] => Array ( [0] => beta ) ) countriesArray ( [1] => Array ( [0] => gamma ) ) provincesArray ( [1] => Array ( [0] => delta ) ) eventsArray ( [1] => Array ( [0] => epsilon) ) However, I would like to get the 1 part of the array, in this example. Is there a magic trick to calling variable arrays' individual elements? Hello, I use loads of different servers at work and they all run seperate services that I need to keep my stuff running. I want to build a simple webpage that will poll the services and display the current state by colour. EG have table that lists the services in green and have them turn red if the service stops unexpectedly. I can build the tables and stuff, I just don't know how to retrieve the state of a service. I've been messing about with some ideas but nothing is working. eg lets say I have two servers each with one service: service one | IP Address of server | RUNNING service two | IP Address of server | STOPPED Has anyone got any ideas? Thanks!! I'm needing some direction in how to start this... I have a page with the 50 U.S. states listed. I have a column in my mysql database that holds each state's name called: source There's a few thousands rows in my database, so the state names vary in the record count. How can I query the database and have a little count by each one of my 50 states that shows how many results exist in the db for that state? I know how to query for total rows and show that number... but confused on how I would do it for individual line items. Thanks for any suggestions. I've got 2 database tables (states, stores) and I want to output all the states with a list of specific locations under each state. My 'states' table has a 'state_id' and 'state' while my 'stores' table has all the store information along with the 'state_id' to work with the 'states' table. How would I ago about formulating a loop to display all locations organized by each state, etc.? Hi, I have the following code in one of my configuration files of the application I'm writing: // config.php $config = array( 'IndexPage' => "$config['MenuItems']['Main Menu']['Home']"; // What page should the application default to if no action is specified? 'MenuItems' => array( 'Main Menu' => array( 'Home' => array('module' => 'main', 'action' => 'index'); Essentially, $config['IndexPage'] can change if a user wants a different page when users access the system. I need to basically call the $config['IndexPage'] array as if it were an actual array, and stick it straight into my page. Is there anyway this can be done? Lets start out by saying I'm a nube to sql/php things so I am learning as I go. I try to read all that I can before I post, and only post when I cant figure it out on my own. That being said. What I want to do in simplest terms is be able to assign a variable to each item in an sql table. So say I have an mysql table that has ID, username, fontcolor. I want to be able to pull those out so say.... while($row = mysql_fetch_array($users)) { $username[$i] = $row[username]; $fontcolor[$i] = $row[fontcolor]; } Then on the page I can just call to $username[1] type thing. I have tried mixing this several different ways with for and while and I keep getting errors on the page. I realize the code isn't showing everything but its just there to show you the idea of what I'm trying to do. I just want it to generate a list(array) that will make it easier for me to call back just the items I need on parts of the page with out having to have extra coding everywhere. Thanks in advance. Jim Is it possible to achieve a similar thing to this... SELECT FROM db WHERE value!=$array My example code is this: $browser_hide = array("1" => "YandexBot 0.0 for unknown"); $query = "SELECT * FROM hitlist_hits WHERE month='$month' AND browser!='$browser_hide' ORDER by id DESC"; Thanks Guys. How can I select a value in an array but not its key. In witch conditions you need to transfer the data from a select in array and after that to show from array in content of the page ? Hi guyz, please help me I want to get an id from my database, I use a word to search it which is stored as keys of array. But all the keys can't be found in my database, even though it's there. Here some parts of my code Code: [Select] $arrKeys = array_keys($arrResult); foreach($arrKeys as $key) { //if($arrResult[$key]>1) { echo $key/*."=>". $arrResult[$key]*/."<br>"; //$q = mysql_query("select id_katadasar from tb_katadasar where katadasar='".$key."'"); $kon->query("select id_katadasar from tb_katadasar where katadasar='".$key."'"); var_dump($kon->query); //echo "select id_katadasar from tb_katadasar where katadasar='".$key."'"; //$jum = $kon->getJumlah(); //$row = mysql_fetch_array($q, MYSQL_ASSOC) or die(mysql_error() . ''. $q); //echo count($row); if($row = $kon->tampilkan()) //if($row = mysql_fetch_array($q, MYSQL_NUM)) { //$res = $kon->tampilkan(); echo $row[0]; } //else { //echo "tidak ada di db <br><br>"; } } } $kon->query is equal to mysql_query() $kon->tampilkan return $row=mysql_fetch_array(query) Thanks nothing gets put into the select list hi i have this function for select / function select($table, $rows = '*', $where = null, $group = null, $order = null, $limit = null) this is what i do to display the records. $db->select('loan'); $records = $db->getResult(); foreach($records as $row) { $user = $row['id']; //$name = $row['firstname']; //print_r($row); echo $user; ?> the codes is running.but when i want to use WHERE id = $_POST['id']; this is what im doing. $db->select('member')->where(array('id' => $_POST['client'])); an error as occured where in function where is not existing. please help how to use the proper way of using select * $table where id=$id; in array. thanks Hi I need to do a SELECT query like $query = "SELECT name FROM myusers WHERE myidnum = ".$_SESSION['myid'].""; and put the results into an array like $myusers = array("David","John","Lucy","Sarah"); But I cannot figure out how to do this Can anyone help? Thanks hello, have a question. im trying to do a select * where id exists in array $id8 here is my array Code: [Select] $result8 = mysql_query("SELECT * FROM customers WHERE whosclient = 'Lansoft'"); while($row8 = mysql_fetch_array($result8)) { $id8=$row8['id']; } here is my select code. it is only returning results for the highest number in the array Code: [Select] $result = mysql_query("SELECT * FROM timesheets WHERE status = 3 AND billable = 1 AND client = '$id8' ORDER BY date, starttime"); while($row = mysql_fetch_array($result)) { Thanks for any help! I have a Prepared Statement that runs a SELECT statement and returns 2 records, and I would like to store the Field Value for each Record into an Array. Here is the code I usually use for queries that return just a single value... // ****************** // Populate Form. * // ****************** // Build query. $q2 = "SELECT response FROM bio_answer WHERE member_id=?"; // Prepare statement. $stmt2 = mysqli_prepare($dbc, $q2); // Bind variable to query. mysqli_stmt_bind_param($stmt2, 'i', $memberID); // Execute query. mysqli_stmt_execute($stmt2); // Store results. mysqli_stmt_store_result($stmt2); // Check # of Records Returned. if (mysqli_stmt_num_rows($stmt2)>0){ // Details Found. // Bind result-set to variable. mysqli_stmt_bind_result($stmt2, $response); // Fetch record. mysqli_stmt_fetch($stmt2); // Close prepared statement. mysqli_stmt_close($stmt2); }else{ // Details Not Found. $_SESSION['resultsCode'] = 'DETAILS_NOT_FOUND_2133'; // Close prepared statement. mysqli_stmt_close($stmt2); // Set Error Source. $_SESSION['errorPage'] = $_SERVER['SCRIPT_NAME']; // Redirect to Display Outcome. header("Location: " . BASE_URL . "/members/results.php"); // End script. exit(); }//End of POPULATE FORM Can someone help me out with the syntax so that I get an end result like this... $answerArray[0] = 'I want to be my own boss!!' $answerArray[1] = 'Don't waste your time trying to do your own Taxes!' Thanks, Debbie |