Jump to content

Archived

This topic is now archived and is closed to further replies.

Arsenal

Recalling Preset Values in PHP

Recommended Posts

I have a field in my MySQL database set to layout. The type is set('aba', 'aaa', 'abc'). Is there a way to recall these values in the type into PHP?

Share this post


Link to post
Share on other sites

Hey

Its hard to tell what you have given us for information, and the question is a little unclear, could you rephrase that?

 

Chris

Share this post


Link to post
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?

 

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?

Share this post


Link to post
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?

Share this post


Link to post
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>";

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

is there soemthing wrong with your computer or what.... you're posting the same thing over and over

Share this post


Link to post
Share on other sites

Actually there was a problem with my connection and my browser. Nothing was displaying. Sorry about that. So, back to my problem. I tried that coding but nothing is being put into the $matches[0] array.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Wow, you just lost me. I do not understand the structure of preg and "/(?<=')\w*?(?=')/". I have never had to use it before.

Share this post


Link to post
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?

Share this post


Link to post
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?

Share this post


Link to post
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.

Share this post


Link to post
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] => 
)

Share this post


Link to post
Share on other sites

now that's why my preg matched nothing... you have numbers and special chars instead of alphabets (\w means alphabets, ?<=' means look for ' before the word, etc). is 0|1... actually what you have in the set?

Share this post


Link to post
Share on other sites

I really have no idea why PHP decides to catch empty strings on the side of each delimeter -- that's just odd. Try using explode() instead, since it's not a regex anyway:

 

$types = explode("','", $type);

Share this post


Link to post
Share on other sites

the reason is because of "[',']", which in regex means match any quote or comma that are present. In the case of split, it splits around quote OR comma, creating empty strings. Remove the square brackets and the regex should match correctly.

Share this post


Link to post
Share on other sites

I just figured it was some silly PHP thing -- but clearly there's nothing special about PHP's implementation of the standard Perl regex; character classes are still there.

Share this post


Link to post
Share on other sites

because he used character classes, so [','] matched quotes and comma. the correct expression should be ',' with no brackets. there's nothing to do with the implementation of regex in PHP.

Share this post


Link to post
Share on other sites

And yes taking out the brackets worked, thanks. Except for one thing, my out put is now this:

 

Array
(
    [0] => '0|0
    [1] => 0|1
    [2] => 1|0
    [3] => 1|1'
)

 

 

Share this post


Link to post
Share on other sites

Change your substr() start & end index values to drop the leading & trailing quote.

Share this post


Link to post
Share on other sites

×

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.