PHP - Put Select Values Into Array?
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 Similar TutorialsHi, In my mysql database i have a text input option, in the registration form and edit my details form i have a multiple select dropdown list, which user selects options to populate the text input box, which ultimately populates the text field in the mysql database. All works perfectly. The dropdownlist consists of 3 parts <optgroups> first is current selection (what is the usesr current selection)works fine, The second <optgroup> is existing words, what words we(the site) have given as options, and the third <optgroup> is the words that others have used. This is where im having a small problem. Because its a text field when i call the data from the database, it calls the entire text box as a single option in my select list.. I want to break the words in the text field (at the comma) and have them listed each one as an option in the select list. Example what i need: Words in text box:(my input allows the "comma") word1, word2, word3, word4, word5, word6, How i want them called/displayed: <option value=\"word1\">word1</option> <option value=\"word2\">word2</option> <option value=\"word3\">word3</option> <option value=\"word4\">word4</option> <option value=\"word5\">word5</option> <option value=\"word6\">word6</option> here's my code: $query = "SELECT allwords FROM #__functions_experience WHERE profile_id = '".(int)$profileId."' LIMIT 1"; $original_functionsexperience =doSelectSql($query,1); $query = "SELECT allwords FROM #__functions_experience WHERE profile_id = '".(int)$profileId."' LIMIT 1"; $functionsexperiencelist=doSelectSql($query); $funcexpList ="<select multiple=\"multiple\" onchange=\"setFunctionsexperience(this.options)\">"; foreach ($functionsexperiencelist as $functionsexperienceal) { $selected=""; if ($functionsexperienceals->allwords == $original_functionsexperience) $selected=' selected="selected"'; $allwords=$functionsexperienceal->allwords; $funcexpList .= "<optgroup label=\"Current selection\"> <option value=\"".$allwords."\" ".$selected." >".$allwords."</option> </optgroup> <optgroup label=\"Existing Words\"> <option value=\"existing1,\">existing1</option> <option value=\"existing2,\">existing2</option> <option value=\"existing3,\">existing3</option> <option value=\"existing4,\">existing4</option> <option value=\"existing5,\">existing5</option> <option value=\"existing6,\">existing6</option> </optgroup> <optgroup label=\"Others added\"> //heres problem <option value=\"".$allwordsgeneral."\">".$allwordsgeneral."</option> </optgroup>"; } $funcexpList.="</select>"; $output['FUNCEXPLIST']=$funcexpList; The result im getting for optgroup others added: word1, word2, word3, word4, word5, how can i get it like this: <option value=\"word1\">word1</option> <option value=\"word2\">word2</option> <option value=\"word3\">word3</option> <option value=\"word4\">word4</option> <option value=\"word5\">word5</option> <option value=\"word6\">word6</option> Hi all, I'm a first time poster here and I would really appreciate some guidance with my latest php challenge! I've spent the entire day googling and reading and to be honest I think I'm really over my head and need the assistance of someone experienced to advise the best way to go! I have a multi dimensional array that looks like (see below); the array is created by CodeIgniter's database library (the rows returned from a select query) but I think this is a generic PHP question as opposed to having anything to do with CI because it related to working with arrays. I'm wondering how I might go about searching the array below for the key problem_id and a value equal to a variable which I would provide. Then, when it finds an array with a the matching key and variable, it outputs the other values in that part of the array too. For example, using the sample data below. How would you recommend that I search the array for all the arrays that have the key problem_id and the value 3 and then have it output the value of the key problem_update_date and the value of the key problem_update_text. Then keep searching to find the next occurrence? Thanks in advance, as above, I've been searching really hard for the answer and believe i'm over my head! Output of print_r($updates); CI_DB_mysql_result Object ( [conn_id] => Resource id #30 [result_id] => Resource id #35 [result_array] => Array ( ) [result_object] => Array ( ) [current_row] => 0 [num_rows] => 5 [row_data] => ) Output of print_r($updates->result_array()); Array ( [0] => Array ( [problem_update_id] => 1 [problem_id] => 3 [problem_update_date] => 2010-10-01 [problem_update_text] => Some details about a paricular issue [problem_update_active] => 1 ) [1] => Array ( [problem_update_id] => 4 [problem_id] => 3 [problem_update_date] => 2010-10-01 [problem_update_text] => Another update about the problem with an ID of 3 [problem_update_active] => 1 ) [2] => Array ( [problem_update_id] => 5 [problem_id] => 4 [problem_update_date] => 2010-10-12 [problem_update_text] => An update about the problem with an ID of four [problem_update_active] => 1 ) [3] => Array ( [problem_update_id] => 6 [problem_id] => 4 [problem_update_date] => 2010-10-12 [problem_update_text] => An update about the problem with an ID of 6 [problem_update_active] => 1 ) [4] => Array ( [problem_update_id] => 7 [problem_id] => 3 [problem_update_date] => 2010-10-12 [problem_update_text] => Some new update about the problem with the ID of 3 [problem_update_active] => 1 ) ) Hi there, Basic question, but I can't seem to find a page in the PHP docs that answers (though I'm sure such a one exists, I just can't seem to think up the right search terms - anyway) My question is: is there a short-hand way to use a value from an associative array as the key in another associative array? So for example, given this array: $the_array = array(0 => array('name': 'the_name', 'value' : 'the_value'), 1 => etc....); then this kind of foreach loop doesn't work Code: [Select] $new_data = array(); foreach($the_array as $element){ $new_data[$element['name']] = $element['value']; } Obviously I can just save out the values, like $name = $element['name'] --- but I was just wondering if there was a more efficient way to do it? Cheers, PS. I tried encapsulating with {} - i.e. $new_data[{$element['name']}] --- but that also fails. I have mysql select query shown below retrieving data from a database:
$sql = " SELECT student, sum( test_score ) as score FROM test INNER JOIN level ON test.level_id = level.level_id GROUP BY student" $result = $mysqli->query($sql, MYSQLI_STORE_RESULT); if($result) { while ($row = $result->fetch_array(MYSQLI_ASSOC)) { var_dump($row); } }On inspection of the retrieved array with var_dump(), i have these result below: array (size=2) 'John' => string 'English' (length=2) 'Score' => string '20' (length=3) array (size=2) 'Mary' => string 'Math' (length=3) 'Score' => string '35' (length=3) array (size=2) 'Samuel' => string 'Physics' (length=3) 'Score' => string '5' (length=3)How do I get at and print out the highest score from this set of array values within this context. I tried this echo max($row['count']) but i understandably got this feedback: Warning: max(): When only one parameter is given, it must be an array Thanks. Hello all, I have yet again trouble finding a logical solution to my problem. I'm fetching an array which can hold 1 or more values. The problem is, I want these values to ouput in my json_encode function, but this also needs to happen dynamically depending on the amount of values. I can't explain it further, so here's the code so far: Code: (php) [Select] $files = mysql_fetch_array($get_files); $a = count($files); $i = 1; while ($files) { $variablename = 'fileName' . $i; $$variablename = $files['fileName']; $i++; } $output = array( OTHER VALUES , 'fileName1' => $fileName1, 'fileName2' => $fileName2, 'fileName3' => $fileName3, ............); // How do I add the fileNames dynamically depending on how many there are? This got me thinking, I also need to dynamically GET the values with jQuery. How would I do that, when the above eventually works? Thank you. Hi all i have 2 small questions and maybe someone could help me out. Say I have a database with a table 'users'. This table has fields: email, name and year. What I want to do is show at the front-end all users, by selecting them from the table, and arrange them by year in column 1 and show them as individuals in column 2. But here is the thing; if someone selects for instance year 1970 and also someone from 1970 individually, someone will be selected twice. (edit:As a side note the selecting is aimed and emailing the users, that's why selecting someone twice is not good First question is: does anyone know how to remove doubles (maybe even triples if there is a select all button, maybe turn this around and say select only 1 instance of a user). Second question: is this a wise way to let someone select. Right now all users are fetched, and if the table users grows this could be tricky because of the large number of users. If someone could point me in the right direction of thinking I would be very happy, been thinking about this and scalability for quite a while now Hi guys, I am trying to create a script that gets a multiple select form selections and assign them to variables with php via the POST method. This is my code: Code: [Select] <select name="search_commodity[]" multiple="multiple" size="3" style="position:absolute; top:80px; left:414px; " > <option value="all">All</option> <option value="none">None</option> <option value="some">Some</option> </select> Then after the form gets submited then it should go to this php page and the first value of the chosen options has to be assigned to the variable called commo. The problem is that is is not assigning anything. Code: [Select] <?php $commo = $_POST['search_commodity[0]']; ?> Can someone help please? I can't figure it out. Hi all, Currently my dob_day is not showing any value, even though there is a value in the database, it seems like it is unable to retrieve the value. No value was shown in the <select> box. Do you guys have any idea? Thanks Code: [Select] <?php $query = "SELECT name, nric, gender, picture, dob_day FROM tutor_profile WHERE tutor_id = '" . $_GET['tutor_id'] . "'"; $data = mysqli_query($dbc, $query) or die(mysqli_error($dbc)); // The user row was found so display the user data if (mysqli_num_rows($data) == 1) { $row = mysqli_fetch_array($data); if ($row != NULL) { $name = $row['name']; $nric = $row['nric']; $gender = $row['gender']; $old_picture = $row['picture']; $dob_day = $row['dob_day']; } else { echo '<p class="error">There was a problem accessing your profile.</p>'; } //HTML CODING <label for="dob" class="label">Date Of Birth</label> <select id="dob_day" name="dob_day" value="<?php if (!empty($dob_day)) echo $dob_day; ?>"> <option> <option>01</option> <option>02</option> <option>03</option> <option>04</option> <option>05</option> <option>06</option> <option>07</option> <option>08</option> <option>09</option> <option>10</option> <option>11</option> <option>12</option> <option>13</option> <option>14</option> <option>15</option> <option>16</option> <option>17</option> <option>18</option> <option>19</option> <option>20</option> <option>21</option> <option>22</option> <option>23</option> <option>24</option> <option>25</option> <option>26</option> <option>27</option> <option>28</option> <option>29</option> <option>30</option> <option>31</option> </select> ?> hi people can vote 3 times with 3 select menus but i want them not to be able to choose 3x the same select option the options in the select menus come out of the database this is how i let them vote Code: [Select] <?php include("./includes/egl_inc.php"); $secure = new secure(); $secure->secureGlobals(); session_start(); if (isset ($_GET['match'])) { $matchid = (int)$_GET['match']; $_SESSION['matchid'] = $matchid; $matchmaps = mysql_fetch_array(mysql_query("SELECT maps,game FROM ffa_matches WHERE id=$matchid")); $matchmapschecker = $matchmaps[maps]; $matchgamechecker = $matchmaps[game]; $maps=mysql_query("SELECT id,map FROM ffa_maplists where gameid = $matchgamechecker"); while(list($id,$map)=mysql_fetch_row($maps)) { $maplist.="<option value='$id'>$map</option>\n"; } $out[body].="<table width='98%' border='0' cellspacing='0' cellpadding='0' style='border:1px solid black'> <tr style='color:#cccccc; background-color:black;'> <td valign='middle' background='$config[bg2]' align='center' colspan='10'> <br/>Choose Maps<br/> </td> </tr></table><table width='98%' border='0' cellspacing='0' cellpadding='0' style='border:1px solid black'> <tr style='background-color:black;'> <td> <form action='ffainsertmapvote.php' method='post'> <select name='map1'>$maplist</select> <select name='map2'>$maplist</select> <select name='map3'>$maplist</select> <input type='hidden' name='match' value='$matchid' /> <input type='submit' value='Vote' /> </form> </td> </tr></table>"; } include("$config[html]"); ?> do i need jquery to fix this? i dont want the user to be able to go to a next screen without having max 2 the same options selected thanks in advance ! Hi guys, Im developing a stock system and so far new stock and can be added with the quantity, however im trying to update the stock by using a select box and selecting the type of stock and inputting a new qty. the form and everything is set up and i can select files from the database, however i dont know how to update files from a select box The code i got so far is 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>stock manager</title> </head> <body> <?php $stockqty =&$_POST['stock_qty1']; ?> <center> <table> <td> <table> <td> <form action='stockview.php' method='POST'> Please Enter a Stock Name and Stock Value <table> <tr> <td> Stock Name: </td> <td> <input name="stock_name" type="text" /><BR /> </td> </tr> <tr> <td> Stock Qty: </td> <td> <input name="stock_qty" type="text" /> </td> </tr> </table> <input name="submit" type="submit" value="Add New Stock Items" /> </form> </td> </table> </td> <td> <table> <td> <form action='stockmanager.php' method='POST' enctype="multipart/form-data"> Please Select from the list the item you wish to update <table> <tr> <td> Stock Name: </td> <td> <?php $connect = mysql_connect("localhost","root", "") or die ("Couldn't Connect!"); mysql_select_db("stock", $connect) or die("Couldn't find db"); // select database $query=("SELECT id, stockname FROM stocks"); $result = mysql_query ($query); echo "<select name=stock value=''>Edit Stock QTY</option>"; while($nt=mysql_fetch_array($result)) { //Array or records stored in $nt echo "<option value=$nt[id]>$nt[stockname]</option>"; /* Option values are added by looping through the array */ } $queryreg = mysql_query("SELECT * FROM stocks WHERE stockqty='$stockqty'"); $numrows = mysql_num_rows($queryreg); $update = mysql_query("UPDATE stocks SET stockqty='$stockqty' WHERE stockname = '$stockname'"); echo("Its updated"); ?> </td> </tr> <tr> <td> Stock Qty: </td> <td> <input name="stock_qty1" type="text" /> </td> </tr> </table> <input name="submit" type="submit" value="Update stock items" /> </form> </td> </table> </td> </table> </center> </body> </html> Any help would be greatly appreciated. Thanks Lance I need to some how pull comma separated images $data[23] from this $data array and then put them into there own array so I can insert them into separate rows in a database. This is the provided array: $data[0] => VIN $data[1] => StockNumber ...(all the other number here) $data[23] => Image_URLs (comma seperated) $data[24] => Equipment // I need something like this (obviously doesn't work?!@#) $delimiter = ","; $img = explode($delimiter, $data[23]); foreach($img as $pic){ $sqlPic = "insert into class_prodimages (pid image rank) values('".$LastId['id']."', '$pic', '".rand(1,20)."')"; } Any help here? I have the following array structu Code: [Select] [0] => Array ( [id] => Array ( [$t] => http://www.google.com/mate/ ) [updated] => Array ( [$t] => 2011-08-31T11:43:05.942Z ) [category] => Array ( [0] => Array ( [scheme] => http://schemas.google.com/g/ [term] => http://schemas.google.com/contact/ ) ) [title] => Array ( [type] => text [$t] => Name ) [link] => Array ( [0] => Array ( [rel] => http://schemas.google.com/contacts/2008/rel#edit-photo [type] => image/* [href] => https://www.google.com/mate/feeds/photos/media/ ) [1] => Array ( [rel] => self [type] => application/atom+xml [href] => https://www.google.com/mate/feeds/contacts ) [2] => Array ( [rel] => edit [type] => application/atom+xml [href] => https://www.google.com/mate/feeds/ ) ) [gd$email] => Array ( [0] => Array ( [rel] => http://schemas.google.com/g/2005#other [address] => email_address@gmail.com [primary] => true ) ) ) I am tried to display name and email address. I am using following method, can anyone tell me is there any better way? Thank u 4u help. NAME $name=( $emp_det[0]['title'][t]); $email =( $emp_det[0]['gdemail'][0][address]); I have an array created to control the down states of the my buttons in my navigation. Which is working well. This array will have quite a few strings in it before I'm done. I then want to conditionally load a CSS file for all the files in that array with the exception of two, is this possible? In plain in english I want to say If in array apart from these values then load my css file. My array (will have more values when complete) Code: [Select] $otherTitles = array('Product Selector', 'Avon Range', 'Knightsbridge Range', 'Kingston Range' ); My code to load a css file Code: [Select] <?php if (in_array($title, $otherTitles)) { ?> <link rel="stylesheet" type="text/css" href="homepage.css"/> <?php } ?> I want all titles in the otherTitles array to get this CSS file apart from two Product Selector and Avon Range. Thanks Richard I have set up a form page with a select box of colleges to select. I want the "options" in the select box to be values taken from a field called "name" in a table called "colleges" and they should be ordered alphabetically. I also want the default selected option to be "none." I have attached a picture to describe what i want. Please be detailed with the code. I am fairly new to php and mysql. Thank you. hi all please help... i would like to get datas between certain intervals. please have a look @ db_table.jpg, it's the db structure. the red highlighted part is the value i need. condition is select values when both device one & device two are off. db_table_report.jpg is the resulting report format. It contains start time & end time and the duration.. please help, i know it's a bit complicated, but it's urgent. Hello there again!
I have the following construct of array now (schematics):
array[0] array[id] = "1" array[title] = "title" array[1] array[id] = "3" array[title] = "another title"Now what i want to do, is to assign the parent-array keys to the IDs of it's contents. This is what it should look like: array[1] array[id] = "1" array[title] = "title" array[3] array[id] = "3" array[title] = "another title"How do I do that again? I know I did this some years ago, but I can not remember how. 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 ? How can I select a value in an array but not its key. 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 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. |