Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Generate SELECT options

Codes here !

Moderators: macek, egami, gesf

Generate SELECT options

Postby alibahaloo » Sun Jun 23, 2013 5:05 pm

Hi Guys,
I'm working on the following code and I have this problem:
Code: Select all
<?php
//Generates a select input based on the given paramters tablename, columnname,
function GenerateSelect($table, $column, $style, $controlname, $valuecolumn) {
   mysql_connect('localhost', 'db', 'password');
   mysql_select_db('db');
   try {

      $sql = "SELECT $column FROM $table";
      $result = mysql_query($sql);

      $style = "'" . $style . "'";
      echo "<select id='$controlname' name='$controlname' style=$style>";
      while ($row = mysql_fetch_array($result)) {
                         // this is where get the error when using string for column
         echo "<option value='" . $row[$valuecolumn]  . "'>" . $row[$column] . "</option>";
      }
   } catch (Exception $e) {
      echo 'Caught exception: ', $e -> getMessage(), "\n";
   }
   echo "</select>";
}
?>

using the code above, I can generate customized SELECT options ... well, that's the idea...
We can call the function like this:
Code: Select all
<?php
GenerateSelect('table', 'FirstName', 'width: 100%', 'users', 'Age');
?>

notice the parameters. the function receives table name, table column, style, control's name and value.
When I use integer data tables, the code works nicely:
Code: Select all
<select id="users" name="users" style="width: 100%">
<option value="35">35</option>
</select>

Now, here's the problem. When I sting type data columns, It messes up.
The HTML Output is:
Code: Select all
<select id="users" name="users" style="width: 100%">
Notice:  Undefined index: Age in D:\xampp\htdocs\trunk\resources\functions.php on line 29
<option value="">Peter</option>
</select>

It gotta be related to the php code where it generates but I just can't figure it out. Can you guys help?
I appreciate any help in advance.

cheers.
alibahaloo
New php-forum User
New php-forum User
 
Posts: 2
Joined: Wed Jun 19, 2013 5:36 pm

Re: Generate SELECT options

Postby alibahaloo » Sun Jun 23, 2013 5:29 pm

I found out the problem. When selecting from db, the sql statement was not selecting the proper columns, therefore it would get messed up, here's the correct code:
Code: Select all
<? php
//Generates a select input based on the given paramters tablename, columnname,
function GenerateSelect($table, $column, $style, $controlname, $valuecolumn) {
   mysql_connect('localhost', 'db', 'password');
   mysql_select_db('db');
   try {

                // Now, I'm selecting both columns from the table, so it's all good.
      $sql = "SELECT $column,$valuecolumn  FROM $table";
      $result = mysql_query($sql);

      $style = "'" . $style . "'";
      echo "<select id='$controlname' name='$controlname' style=$style>";
      while ($row = mysql_fetch_array($result)) {
         echo "<option value='" . $row[$valuecolumn]  . "'>" . $row[$column] . "</option>";
      }
   } catch (Exception $e) {
      echo 'Caught exception: ', $e -> getMessage(), "\n";
   }
   echo "</select>";
}
?>

problem solved.
alibahaloo
New php-forum User
New php-forum User
 
Posts: 2
Joined: Wed Jun 19, 2013 5:36 pm


Return to mySQL & php coding

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.