Populate PHP Combo Box using MySQL Database Field Enum Values - php

I would like to create a combo box (eventually operating it using jQuery Ajax), which is populated with the enum values from a database table field.
My goal is to later use these options to filter mysql_query results on a page.
I'm thinking the best way to do this is to echo the entire form and combo box with the enum values as the option tags values. Where I fall short is knowing how to call the enum values in the first place.
If you have a better suggestion on how to do this, please let me know!
Thanks.
I found the following code, which appears to do what I need. However, it seems to grab all the enum arrays from my table, and convert each array into a combo box. This isn't a problem as I was hoping to create combo boxes for each of the separate arrays anyway. However, I wish to change the order of it. Is there any way I can edit this code to control each set? Or even have it specify which field I would like to echo, and then repeat as many times as I need? I know that doing that would mean more code, and would not be as efficient.
The PHP:
$table="images";
$describe=mysql_query("describe $table");
while ($ligne=mysql_fetch_array($describe)){
extract($ligne);
if (substr($Type,0,4)=='enum'){
$liste=substr($Type,5,strlen($Type));
$liste=substr($liste,0,(strlen($liste)-2));
$enums=explode(',',$liste);
if (sizeof($enums)>0){
echo "<select name='enum'>\n";
for ($i=0; $i<sizeof($enums);$i++){
$elem=strtr($enums[$i],"'"," ");
echo "<option value='".$elem."'>".$elem."</option>\n";
}
echo "</select>";
}
}
}
Thought I should mention, the three fields that contain enums are imgFamily, imgClass, and imgGender. I would like the combo boxes in that order preferably.

You can use:
SELECT column_type FROM information_schema.columns WHERE table_name = :myTable AND column_name = :myColumn
This will return something like this:
enum('one','two','three')
Which you could parse in php. However, I think it would perhaps be better to maintain the list in code or another table than as enum values.

Related

How GET/REQUEST all values from a field in a database (phpMyAdmin) using SQL and PHP?

I am currently doing work with a database online. The is entries from different countries and I need to do a drop down list for these countries. Rather than typing the countries into the HTML is there any way of using GET/REQUEST or anything to get the data from the database to create a drop down?
Obviously, there are a few duplicate countries such as I think USA occurs 3 or 4 times but I would like that to appear on the list only once.
Update: coming to think of it GET/REQUEST can't be used so is there an alternative possibly using an SQL statement?
Basic structure of an HTML dropdown is:
<select>
<option>Option1</option>
<option>Option2</option>
<option>Option3</option>
</select>
Knowing that, simple make a query from your database, and loop through the values. Assuming you already know how to do that.
<select>
<?php
while(<loop through each value in database>){
echo "<option>{$data}</option>"; //Assuming $data has current iteration's value
}
?>
</select>
the equivalent sql statement for GET is SELECT.

Compare Mysql Field Value

I have a table named 'jobs' with field 'categories'.
Now Values in my category field is like this
368,369,372,379,380,381
368,369,372
369,373,374,375,376
491,492
& i am getting values with search like this
'368','374','490'
Now how to compare myvalue to field value, I can't use "IN" in this stage.
In above case,
If i have values like "'368','374','490'" this, i want first three rows, beacause it contains atleast one value from my list.
how can i do this? Please suggest.
if you happy to use in programming end (i.e. using php) , you can pull those database data into an associative array. Then you can easily that array for your data manipulation. In php there are many inbuilt functions for arrays. you can use those.
http://de3.php.net/manual/en/language.types.array.php
http://www.php.net/manual/en/ref.array.php

Explain this SQL code in PHP script

I found this piece of code from an answer on Several drop down list input to one field table. It really seems like something I can use in my current project. The current project is about populating a table with two dropdown boxes (which are populated themselves with tables). However, I don't understand what personal changes I need to do in order to make it work. attributes? :attributes? Can someone make sense of it?
$levels = $_POST['level1'][0].",".$_POST['level1'][1];
//Now you have a string called $levels
// Which contains a comma seperated list, to insert into db / one field
//Insert into your table...change your table and field names to real values...
$sql = "INSERT INTO yourTable (attributes) VALUES (:attributes)";
$q = $db->prepare($sql);
$q->execute(array(':attributes'=>$levels));
?>
:attributes is a bound parameter in a PHP prepared statement which inserts a row into a table with an attributes field, setting just that field.

How to Store Multi Select Box in php?

am having select box with multiple options, like option1,option2,option3,option4,option5.Now i want to select more than one option.
For Example: Select option1,option5,option3.and click submit.after submitting the select box need to show the selected items.
in which format to be stored in mysql and what's the exact coding to do that?please help me to get rid of this problem...
You could serialize the data in and out.
Use name[] as your name which results in an array in PHP.
$_POST['name'];
serialize($name);
//now insert into DB
Do the opposite to retrieve:
unserialize($name);
But to be honest you are better off looking at more granular methods of storing the data, rather than trying to stuff it all into one field in a database. You could take the array and loop through it storing each choice in a separate row or field.
foreach($name as $value){
//insert $value into db
}
use implode
$var = implode ( "|", $_POST['selectname']);
Try this:
http://www.aleixcortadellas.com/main/2009/03/20/492/

PHP - Pulling single value from multidimensional array

this is probably quite simple but I could do with some help.
I am trying to create a small PHP function that will display a single value form a multidimensional array when the user used two dropdown boxes to select the row and column of the array.
So, the user will make a selection from the first dropdown box, which will contain the titles of the rows, and then make a selection from a second dropdown box, which will contain the titles of the columns. Once the selections have been made, the function then needs to output the value for the specific row and column selected.
I thought I had created an array that would work but, sadly no. I have 6 rows and 6 columns in my data table.
Also, is there a JQuery or Javascript alternative?
Just looking for a few pointers to get me on my way.
Thanks in advance,
Micanio
You could either do this on the server-side or through JS.
JS: Have the script update a hidden form field with the value using the onChange() event of the drop downs. Then simply grab that hidden field when the form is posted back to the server (of source always checking for valid data).
PHP: The form will provide the two values $_POST['field1'] and $_POST['field2'] (which of course you will sanitize before using). The script could define a multidimensional array that you could feed those two values into:
$finalValue = $mdArray[$SanitizedField1][$SanitizedField2];
From there just store the $finalValue however you'd like.
$data = array();
for ($i=0;$i<6;$i++) {
$data[$i] = array();
for ($j=0;$j<6;$j++)
$data[$i][$j] = rand(0,100);
}
This should create an array similar to what you have described.
You can then access it like so...
echo $data[0][3];
Your form would need two select fields, both will values 0-5 then you can take the both of them and use them to access a value in your array.
If I understand your question correctly, you need something like a user selects a drop down list. Once selected, the option populates a second list. A real world example would be a user selects on Country to fill in a form and it will populates the States drop down list.
This kind of functionality is usually done with javascript not server side php.
http://javascript.internet.com/forms/country-state-drop-down.html

Categories