spidermann Posted December 27, 2008 Share Posted December 27, 2008 I am having trouble with getting array data from an external file using case switching. I have two separate examples of switching, one with a single external file with multiple page contents set in arrays, and the other has multiple external files with separate contents in each file. What I need to do is blend both techniques so that I can call multiple page contents with a variable in the switch. I know that's a little convoluted, so I'll provide examples: The first example uses a switch for calling one page and outputting the called content using $mode: $mode = request_var('mode', ''); // Load the appropriate content switch ($mode) { case 'example1': $message = 'EXAMPLE1_TEXT'; break; default: $message = 'FOOBAR_TEXT'; break; } $template->assign_vars(array( 'EXAMPLE1_TEXT' => $user->lang[$message], 'FOOBAR_TEXT' => $user->lang[$message], )); And it's external file: $lang = array_merge($lang, array( 'FOOBAR_TEXT' => '<p class="gen">Lorem ipsum dolor sit amet.</p>', 'EXAMPLE1_TEXT' => '<p class="gen">In eleifend consequat orci.</p>', )); The second example uses switch to call multiple files with separate contents in each, using a foreach to display the contents properly: $mode = request_var('mode', ''); // Load the appropriate file switch ($mode) { case 'example1': $user->add_lang('example1', false, true); break; default: $user->add_lang('foobar', false, true); break; } // Pull the array data $stuff_blocks = array(); foreach ($user->stuff as $stuff_ary) { if ($stuff_ary[0] == '--') { $template->assign_block_vars('stuff_block', array( 'BLOCK_TITLE' => $stuff_ary[1]) ); continue; } $template->assign_block_vars('stuff_block.stuff_row', array( 'STUFF_QUESTION' => $stuff_ary[0], 'STUFF_ANSWER' => $stuff_ary[1]) ); } And one of the external files for the above: $stuff = array( array( 0 => '--', 1 => 'This is a Block Title' ), array( 0 => 'What is this?', 1 => 'An example of a question. And this is the answer.' ), Now, where I'm stuck is; how can I call one file, holding multiple page contents, like the first example, but set it up so that the arrays for the contents are variables like in the second example. I believe the switch will need to handle a variable of some sort, but I'm not sure where to begin. If I've confused you with that explanation, I'll try to clarify. I think I confused myself with that question... Quote Link to comment https://forums.phpfreaks.com/topic/138519-solved-using-switch-to-call-arrays-from-seperate-file/ Share on other sites More sharing options...
spidermann Posted December 27, 2008 Author Share Posted December 27, 2008 I'm sorry. That was a terrible way to ask my question. Let me try again, this time a bit more straightforward. What I have is a set of pages that are called from one language file, via the $mode command. The pages are called like page.php?mode=foo. The pages are called in the php file from a switch, the mode name hard-coded. That all works perfectly. I was wondering, can the mode call a variable, called from a separate function? Like this: switch ($mode) { case 'a variable called from elsewhere, using value in language file': $message = 'text from file'; break; default: $message = 'default message'; break; } and then in the language file: $message = array( array( 0 => 'file name, used as $mode case variable', 1 => 'output page text contents' ), array( 0 => 'file name, used as $mode case variable', 1 => 'output page text contents' ), etc... This is so that I can programatically add, edit and delete entries in the language file, setting the mode to be called from the URI in the language file as well. Thanks for your time, and I apologize for the first post. Quote Link to comment https://forums.phpfreaks.com/topic/138519-solved-using-switch-to-call-arrays-from-seperate-file/#findComment-724366 Share on other sites More sharing options...
RussellReal Posted December 27, 2008 Share Posted December 27, 2008 not to be rude.. but neith of your posts make much sence.. I can help you.. but seeing real code not dummy text would be really helpful.. and I was wondering, can the mode call a variable, called from a separate function? Like this: is just a TIIIINY bit confusing, because I don't see any function declarations, and $mode can't call any variable since its a variable itself..? :S I'd really like to help you but I don't want to say anything since I don't completely understand what you mean lol Quote Link to comment https://forums.phpfreaks.com/topic/138519-solved-using-switch-to-call-arrays-from-seperate-file/#findComment-724410 Share on other sites More sharing options...
spidermann Posted December 28, 2008 Author Share Posted December 28, 2008 lol! Don't worry, you're not being rude. Quite the opposite. And I think you answered my question. ...$mode can't call any variable since its a variable itself..? :S That's what I was curious about. I'm stumped on how to get the switch to call it's case from the file that holds both the case, and the output text, where the case is the same, repeated multiple times. I will be adding, editing and deleting entries in the language file, so the switch needs to be able to handle changes 'on the fly,' so to speak. I thought maybe a foreach could do it, but I'm not very familiar with how to use it (obviously). I don't really have any working code to show, as this is mostly hypothetical, but this is as close as I can come to an example: $user->setup('language/foobar_en'); $mode = request_var('mode', ''); switch ($mode) { case 'example': $message = 'text'; break; default: $message = 'default message'; break; } and in the language/foobar_en file: $message = array( array( example => 'foo', text => 'output page text contents', ), array( example => 'bar', text => 'output page text contents', )); The key ('example') in the above language file will repeat, but it's value will change, and the value ('foo' and 'bar') is what I would like to use as the case in the switch. So that means that the case in the main file will need something like this: case 'example[value]': I know that won't work, so how do I tell it to use the value instead of 'example'? Did that make sense? Tell me how I can explain it better, and I'll give it a shot. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/138519-solved-using-switch-to-call-arrays-from-seperate-file/#findComment-724707 Share on other sites More sharing options...
RussellReal Posted December 28, 2008 Share Posted December 28, 2008 do you have msn or yahoo? Quote Link to comment https://forums.phpfreaks.com/topic/138519-solved-using-switch-to-call-arrays-from-seperate-file/#findComment-724804 Share on other sites More sharing options...
spidermann Posted December 28, 2008 Author Share Posted December 28, 2008 LOL! That bad, eh? I've got MSN, but don't waste your time trying to figure this out. If you guys have no idea what I'm talking about, then the idea must be too obscure. I figured that if the case for the switch could be a variable, I could put it in the same file as the page content. But I'll re-think my approach, and probably come out all the better for it. Thanks again for your time, and for the help. Oddly enough, not being able to help was very helpful -- now I won't spend a week bashing my head against a wall, only to figure out it can't be done that way. Quote Link to comment https://forums.phpfreaks.com/topic/138519-solved-using-switch-to-call-arrays-from-seperate-file/#findComment-724832 Share on other sites More sharing options...
RussellReal Posted December 28, 2008 Share Posted December 28, 2008 where mode is you can use a function() instead.. what switch is.. its not a function switch ($hello) { case "hi": break; case "omg": break; } is the same as doing if ($hello == "hi") { } else if ($hello == "omg") { } btw you can add me.. RussellonMSN@hotmail.com Quote Link to comment https://forums.phpfreaks.com/topic/138519-solved-using-switch-to-call-arrays-from-seperate-file/#findComment-724945 Share on other sites More sharing options...
spidermann Posted December 29, 2008 Author Share Posted December 29, 2008 Right, I understand that. What I'm trying to do is get (using your example above) "hi" and "omg" to be a variable. So that the case doesn't have to be hard-coded, because the cases will change, and I'd like to not have to change the switch every time. Usually, using a switch like this to call a page, you have to tell the switch "I want to call the 'hi' page or the 'omg' page" via the case. Trouble is, if the pages being called are added or removed, you have to go back to the switch and change it to reflect the new or removed case. I thought that if I could make the pages an array, I could put it's case value in with the page itself, like: $message = array( array( case => 'hi', text => 'output page text contents', ), array( case => 'omg', text => 'output page text contents', )); Then I could call the switch and case somehow (maybe a foreach or something) and the switch would look like this: switch ($mode) { case 'case': // 'case' would be variable $message = 'text'; break; } Is that a little clearer? I understand that this is unorthodox, but I figured that if it could be done, you guys would be the ones who would be able to do it. Quote Link to comment https://forums.phpfreaks.com/topic/138519-solved-using-switch-to-call-arrays-from-seperate-file/#findComment-725058 Share on other sites More sharing options...
RussellReal Posted December 29, 2008 Share Posted December 29, 2008 <? for ($i = 0; $i < count($modes); $i++) { if ($modes[$i]['case'] == $mode) { $text = $modes[$i]['text']; break; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/138519-solved-using-switch-to-call-arrays-from-seperate-file/#findComment-725102 Share on other sites More sharing options...
spidermann Posted December 29, 2008 Author Share Posted December 29, 2008 Upon the back of a giant, this has been solved. Through much tribulation, RussellReal got it worked out. The solution follows: In the main php file, foobar.php: foreach ($user->lang['modes'] as $k => $v) { if ($v['mode'] == $mode) { $text = $v['text']; break; } } $template->assign_vars( array( 'FOOBAR_TEXT' => ((isset($text))? $text:'no text') ) ); And in the language file, foobar_en.php: $thisArray = array( 'modes' => array( array( 'mode' => 'foo', 'text' => '<p class="gen">Lorem ipsum dolor sit amet.</p>' ), array( 'mode' => 'bar', 'text' => '<p class="gen">In eleifend consequat orci.</p>' ) ) ); $lang = array_merge($lang,$thisArray); This works perfectly. Elegant and clean. Thank you so much for your help, RussellReal, I couldn't have done it without you. Quote Link to comment https://forums.phpfreaks.com/topic/138519-solved-using-switch-to-call-arrays-from-seperate-file/#findComment-725211 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.