Arsenal Posted December 21, 2005 Share Posted December 21, 2005 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? Quote Link to comment Share on other sites More sharing options...
marker5a Posted December 21, 2005 Share Posted December 21, 2005 Hey Its hard to tell what you have given us for information, and the question is a little unclear, could you rephrase that? Chris Quote Link to comment Share on other sites More sharing options...
Arsenal Posted December 21, 2005 Author Share Posted December 21, 2005 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? Quote Link to comment Share on other sites More sharing options...
Arsenal Posted December 21, 2005 Author Share Posted December 21, 2005 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? Quote Link to comment Share on other sites More sharing options...
ryanlwh Posted December 21, 2005 Share Posted December 21, 2005 $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>"; Quote Link to comment Share on other sites More sharing options...
Arsenal Posted December 21, 2005 Author Share Posted December 21, 2005 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? Quote Link to comment Share on other sites More sharing options...
ryanlwh Posted December 22, 2005 Share Posted December 22, 2005 is there soemthing wrong with your computer or what.... you're posting the same thing over and over Quote Link to comment Share on other sites More sharing options...
Arsenal Posted December 22, 2005 Author Share Posted December 22, 2005 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. Quote Link to comment Share on other sites More sharing options...
fenway Posted December 23, 2005 Share Posted December 23, 2005 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. Quote Link to comment Share on other sites More sharing options...
Arsenal Posted December 23, 2005 Author Share Posted December 23, 2005 Wow, you just lost me. I do not understand the structure of preg and "/(?<=')\w*?(?=')/". I have never had to use it before. Quote Link to comment Share on other sites More sharing options...
fenway Posted December 23, 2005 Share Posted December 23, 2005 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? Quote Link to comment Share on other sites More sharing options...
Arsenal Posted December 24, 2005 Author Share Posted December 24, 2005 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? Quote Link to comment Share on other sites More sharing options...
fenway Posted December 24, 2005 Share Posted December 24, 2005 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. Quote Link to comment Share on other sites More sharing options...
Arsenal Posted December 24, 2005 Author Share Posted December 24, 2005 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] => ) Quote Link to comment Share on other sites More sharing options...
ryanlwh Posted December 25, 2005 Share Posted December 25, 2005 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? Quote Link to comment Share on other sites More sharing options...
fenway Posted December 25, 2005 Share Posted December 25, 2005 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); Quote Link to comment Share on other sites More sharing options...
ryanlwh Posted December 26, 2005 Share Posted December 26, 2005 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. Quote Link to comment Share on other sites More sharing options...
fenway Posted December 27, 2005 Share Posted December 27, 2005 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. Quote Link to comment Share on other sites More sharing options...
ryanlwh Posted December 27, 2005 Share Posted December 27, 2005 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. Quote Link to comment Share on other sites More sharing options...
Arsenal Posted December 28, 2005 Author Share Posted December 28, 2005 Yes, those are the actual input values in the database. Quote Link to comment Share on other sites More sharing options...
Arsenal Posted December 28, 2005 Author Share Posted December 28, 2005 '0|0','0|1','1|0','1|1' These are the four exact values in the database. Quote Link to comment Share on other sites More sharing options...
Arsenal Posted December 28, 2005 Author Share Posted December 28, 2005 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' ) Quote Link to comment Share on other sites More sharing options...
fenway Posted December 28, 2005 Share Posted December 28, 2005 Change your substr() start & end index values to drop the leading & trailing quote. Quote Link to comment Share on other sites More sharing options...
Arsenal Posted December 29, 2005 Author Share Posted December 29, 2005 Thank you, I am all set. You guys are excellent! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.