After finally getting this to work, I thought I'd post it in case it maybe
I was having trouble working to populate a few sets of dropdown select tags.
I had researched a number of similar submissions and solutions, but I still couldn't find the answers I'd been looking for.
@Ronser had helped me to test through my queries, which lead me to learning more about how the arrays actually worked. I realised I needed to go back and to update the access column in TABLE 1 to access_id. (I should've indexed these originally).
Table 1: app_generalData
app_id,
title,
status_id,
category_id,
tags,
access_id
Table 2: app_access
access_id,
access_title
Desired result(s):
Objective 1: Show/echo the selected option (stored in app_access table)
Objective 2:
Build these queries with variables to allow for easy updating for adding new dropdowns.
Resulting HTML:
<select name="access"><option "">Global</option>\n<option " selected ">Corporate</option>\n<option "">Local Site</option>\n</select>
Code:
<?php
//connect to the database
require_once('connectvars.php');
// global variable for this populating this dropdown
$dropdown = "access";
$before_var = "app_";
$column1= $before_var.$dropdown;
$after_var = "_title";
$column2= $dropdown.$after_var;
$id_var= "_id";
$dropdown_table_id= $dropdown.$id_var;
$optionsList = array();
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
or die ('Error connecting to MySQL server.');
echo '<select name="' . $dropdown . '">';
// See if we're viewing a selected app or are we creating a new app
if (isset($_GET['app_id'])) {
// print_r($_GET); // GET is Successful
// 'if' [app_id] is appended in the url
// STEP 1: Get the stored value of the SELECTED from mysql
// Get "selected item" (id) from app_generalData (table) with the "selected app" (id)
$option = "SELECT ".$dropdown_table_id." FROM app_generalData WHERE app_id ='" . $_GET['app_id'] . "'";
// submit the select statement
// Get & store the value of "selected" <option>
$selected_option = mysqli_query($dbc, $option)
or die(mysql_error());
$row_1=mysqli_fetch_array($selected_option);
// STEP 2: Build the SELECT input
// Set the array of data to populate dropdown list <option>s
$options = "SELECT * FROM ".$column1." ORDER BY ".$dropdown_table_id."";
// NOTE: print_r($options)...SELECT access_title FROM app_access ORDER BY access_id
$selected_options = mysqli_query($dbc, $options)
or die(mysqli_error());
$kk = 0; //initialize kk
while($row_2 = mysqli_fetch_array($selected_options)) {
$selected ='';
if($row_1["$dropdown_table_id"]==$row_2["$dropdown_table_id"]) {
$selected=' selected ';
}
$optionsList[$kk++] ='<option "' . $selected . '">' . $row_2["$column2"] . '</option>';
}
// Echo the <option>s
$optionCount = count($optionsList);
for($i=0;$i<$optionCount;$i++) {
echo $optionsList[$i].'\n';
}
}
else {
// Action 'if' no [app_id] is appended in the url
};
// close the last <select> tag
echo '</select>';
// close the last database
mysqli_close($dbc);
?>
print_r($_GET);to verify the data flow.. any errors???print_r($_GET);directly afterif (isset($_GET['app_id'])) {. It producedArray ( [app_id] => 1 ). Looks right to me.print_r($_GET);,print_r($_POST);,print_r($_SESSION);is generally used at the top to know what are the elements passed/initialized.