Jump to content

Recalling Preset Values in PHP


Arsenal

Recommended Posts

Let's try it this way:

 

CREATE TABLE settings (

layout set('aba', 'aaa', 'abc') default 'aba'

)

 

This is the table design. I want PHP to take the options that layout can be set as and display them as a drop-down menu. Is it possible without having to type aba, aaa, abc into my php code?

 

Let's try it this way:

 

CREATE TABLE settings (

layout set('aba', 'aaa', 'abc') default 'aba'

)

 

This is the table design. I want PHP to take the options that layout can be set as and display them as a drop-down menu. Is it possible without having to type aba, aaa, abc into my php code?

 

Let's try it this way:

 

CREATE TABLE settings (

layout set('aba', 'aaa', 'abc') default 'aba'

)

 

This is the table design. I want PHP to take the options that layout can be set as and display them as a drop-down menu. Is it possible without having to type aba, aaa, abc into my php code?

Link to comment
Share on other sites

Let's try it this way:

 

CREATE TABLE settings (

layout set('aba', 'aaa', 'abc') default 'aba'

)

 

This is the table design. I want PHP to take the options that layout can be set as and display them as a drop-down menu. Is it possible without having to type aba, aaa, abc into my php code?

Link to comment
Share on other sites

$query = "DESC settings layout";
$result = mysql_query($query);
$type = mysql_result($result,0,'Type');

$matches = array();
preg_match_all("/(?<=')\w*?(?=')/",$type,$matches);

echo "<select name=\"layout\">";
foreach($matches[0] as $v)
{
   echo "<option value=\"$v\">$v</option>";
}
echo "</select>";

Link to comment
Share on other sites

Let's try it this way:

 

CREATE TABLE settings (

layout set('aba', 'aaa', 'abc') default 'aba'

)

 

This is the table design. I want PHP to take the options that layout can be set as and display them as a drop-down menu. Is it possible without having to type aba, aaa, abc into my php code?

 

Let's try it this way:

 

CREATE TABLE settings (

layout set('aba', 'aaa', 'abc') default 'aba'

)

 

This is the table design. I want PHP to take the options that layout can be set as and display them as a drop-down menu. Is it possible without having to type aba, aaa, abc into my php code?

Link to comment
Share on other sites

Your SQL query should be:

 

SHOW COLUMNS FROM settings LIKE 'layout'

 

Then retrieve the column named 'Type', extract the options by removing the leading "set(" and trailing ")" [with substr], and then you can split the extracted string on "','" [with preg_split] -- this will give you the array you desire.

 

Hope that helps.

Link to comment
Share on other sites

Ok, step by step:

 

1) get back the column defintion in SQL

2) retrieve the desired piece of the definition, the type

3) since the type is in the form "set('option1','option2','option3')", we need to get an the "inside" of the parenthesis -- we can do this with substr, since it's always 4 chars at the beginning [set(] and 1 at the end [)]

4) now we're left with our options list, where each value is separated by 3 characters, [','] -- that's what split is used for.

 

Where did I lose you?

Link to comment
Share on other sites

You lost me at the functions. I had never used those before now. Ok, now that I have done that, I ended up with this array:

 

Array
(
    [0] => 
    [1] => 0|0
    [2] => 
    [3] => 
    [4] => 0|1
    [5] => 
    [6] => 
    [7] => 1|0
    [8] => 
    [9] => 
    [10] => 1|1
    [11] => 
)

 

How do i clean it up so that the spaces are not part of the array?

Link to comment
Share on other sites

Spaces? That's strange... do you actually have spaces in the options themselves? It looks from the output like you have a space both before & after each option! If so, simply add a leading & trailing space to the split, and change the substr indicies so that you trim the entire string.

Link to comment
Share on other sites

I do not know if it is my sytax. Here check it out.

 

$result = mysql_query("SHOW COLUMNS FROM ".$DB['pref']."_settings LIKE 'layout'");
$type = mysql_result($result,0,'Type');
$type = substr($type, 4, -1);
echo $type;
$type = split('[\',\']', $type);
echo "<pre>";
print_r($type);
echo "</pre>";

 

Results in:

 

'0|0','0|1','1|0','1|1'

Array
(
    [0] => 
    [1] => 0|0
    [2] => 
    [3] => 
    [4] => 0|1
    [5] => 
    [6] => 
    [7] => 1|0
    [8] => 
    [9] => 
    [10] => 1|1
    [11] => 
)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.