PHP ORM – Create select box with mysql set or enum


Hits: 5964  

PHP have rich set of functions to solve real time problems in programming and web development.. thanks to Rasmus Lerdorf

eval — Evaluate a string as PHP code

A SET datatype can hold any number of strings from a predefined list of strings. The ENUM datatype restricts to a single member of the set of predefined strings, the SET datatype allows to store any of the values together, from none to all of them.

CREATE TABLE svnlabs_set (
name SET(‘lamp’,’web’,’linux’,’joomla’)
);

CREATE TABLE svnlabs_enum (
name ENUM(‘lamp’,’web’,’linux’,’joomla’)
);

If we want to fill the html select box with these two tables…

SHOW COLUMNS FROM svnlabs_set;
> set(‘lamp’,’web’,’linux’,’joomla’)

SHOW COLUMNS FROM svnlabs_enum;
> enum(‘lamp’,’web’,’linux’,’joomla’)

now based on column type set list of string values from mysql to php array…

<?php

…….

// if column type is set
$arr = “\$set=”.str_replace(“set”, “array(“, $column[‘Type’]));
eval($arr);

// if column type is enum
$arr = “\$set=”.str_replace(“enum”, “array(“, $column[‘Type’]));
eval($arr);

…………..

?>

Use $set PHP array to fill select box….