Jump to content


Photo

Recalling Preset Values in PHP


  • Please log in to reply
23 replies to this topic

#1 Arsenal

Arsenal
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 21 December 2005 - 06:58 PM

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?

#2 marker5a

marker5a
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 21 December 2005 - 06:59 PM

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

Chris

#3 Arsenal

Arsenal
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 21 December 2005 - 09:04 PM

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?

#4 Arsenal

Arsenal
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 21 December 2005 - 09:17 PM

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?

#5 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 21 December 2005 - 10:14 PM

$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>";

Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#6 Arsenal

Arsenal
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 21 December 2005 - 11:03 PM

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?

#7 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 22 December 2005 - 02:45 AM

is there soemthing wrong with your computer or what.... you're posting the same thing over and over
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#8 Arsenal

Arsenal
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 22 December 2005 - 10:44 PM

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.

#9 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 23 December 2005 - 09:49 PM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#10 Arsenal

Arsenal
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 23 December 2005 - 10:14 PM

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

#11 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 23 December 2005 - 11:28 PM

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?
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#12 Arsenal

Arsenal
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 24 December 2005 - 01:08 AM

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?

#13 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 24 December 2005 - 01:11 AM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#14 Arsenal

Arsenal
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 24 December 2005 - 01:23 AM

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] => 
)


#15 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 25 December 2005 - 07:24 PM

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?
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#16 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 25 December 2005 - 10:59 PM

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);

Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#17 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 26 December 2005 - 06:27 PM

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.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#18 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 27 December 2005 - 05:22 AM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#19 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 27 December 2005 - 04:03 PM

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.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#20 Arsenal

Arsenal
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 28 December 2005 - 05:50 PM

Yes, those are the actual input values in the database.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users