0

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); 

?>
4
  • check with print_r($_GET); to verify the data flow.. any errors??? Commented Oct 14, 2014 at 8:58
  • @Ronser I placed the print_r($_GET); directly after if (isset($_GET['app_id'])) {. It produced Array ( [app_id] => 1 ). Looks right to me. Commented Oct 14, 2014 at 12:40
  • 1
    print_r($_GET);,print_r($_POST);,print_r($_SESSION); is generally used at the top to know what are the elements passed/initialized. Commented Oct 14, 2014 at 13:02
  • see the change posted and check the result Commented Oct 14, 2014 at 13:10

3 Answers 3

1

Please try the following...

  1. Check for the 'app_id'.

  2. Print the sql query and run directly in your mysql.

If it returns no rows or error please verify the sql query.

Sign up to request clarification or add additional context in comments.

1 Comment

@Ronser and @Bincy-Kuriakose See above that <br /> 1. The app_id did print the correct data. 2. SQL Query tested directly in mysql - The 1st sql query produced SELECT access FROM app_generalData WHERE app_id ='1' * mysql produced corporate, which is the correct result from correct the access column - The 2nd sql query produced `SELECT access_title FROM app_access ORDER BY access_id * mysql query produced: the correct column of the desired table
1

try this...

     $options = "SELECT ".$column2." FROM ".$column1." ORDER BY ".$dropdown_table_id."";
      $kk     = 0;   //initialize kk
     while($row = mysqli_fetch_array($options)) {
          $selected ='';
          if($selected_option==$row["$column2"]) {
          $selected=' selected ';
      }
      $optionsList[$kk++] ='<option "' . $selected . '">' . $row["$column2"] . '</option>';          
     //try this change. or

     echo '<option "' . $selected . '">' . $row["$column2"] . '</option>'; //print it here itself
     }

    print_r($optionsList);

11 Comments

I've patched in this snippet. The print_r resulted in Array ( )
was it an empty array after trying the above code??
Yes. As you've helped me to realise I could test each step with print_r, I've gone back through &...I think I've found something: I ran a print_r($selected_option); and it produced mysqli_result Object ( [current_field] => 0 [field_count] => 1 [lengths] => [num_rows] => 1 [type] => 0 ) That didn't seem right, so I readdressed the mysqli_fetch_array to $row_1=mysqli_fetch_array(**$selected_option**); print_r($row_1);` gave me Array ( [0] => corporate [access] => corporate ) . Getting close... I've changed the if statement in your while to if($row_1==. Still nothing..
hope you are nearing... first print_r($row); to know the values.. you can also use echo "<pre>"; print_r($row); echo "</pre>"; inorder to align a large array... then have you tried the above solution??
then there is something wrong with your query check that first
|
0
<?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 "selected" and <options>

          // 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 ='';
                // Compare access_id from table1 against access_id from table2
                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); 

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.