emehrkay Posted December 16, 2006 Share Posted December 16, 2006 [code]switch($block){ case 'top': $div = '$this->_head_top'; break; case 'mid': $div = '$this->_head_mid'; break; case 'bot': $div = '$this->_head_bot'; break; }$div .= $foo;[/code]i basically want to say if $block is 'top' use the variable $this->_head_top and so on. how do i go about setting in the switch and then using it after the switch?thanks Link to comment https://forums.phpfreaks.com/topic/30822-using-a-swtich-case-to-tell-which-variable-to-use/ Share on other sites More sharing options...
trq Posted December 16, 2006 Share Posted December 16, 2006 Allmost had it. Don't forget, php doesn't get parsed when they are within single quotes.[code=php:0]switch($block){ case 'top': $div = $this->_head_top; break; case 'mid': $div = $this->_head_mid; break; case 'bot': $div = $this->_head_bot; break;}$div .= $foo;[/code] Link to comment https://forums.phpfreaks.com/topic/30822-using-a-swtich-case-to-tell-which-variable-to-use/#findComment-142187 Share on other sites More sharing options...
emehrkay Posted December 16, 2006 Author Share Posted December 16, 2006 thorpe, i thought doing it without any quotes would just assign the value of $this->_head_top to $div. i want to say if top, use the var $this->_head_top after the switchblock.so in that example, it would be $block = 'top'switch($block){ case 'top': $div = $this->_head_top; break; case 'mid': $div = $this->_head_mid; break; case 'bot': $div = $this->_head_bot; break;} //$div would hold the var to use$this->_head_top .= $foo; //$this->_block_top replaces $div Link to comment https://forums.phpfreaks.com/topic/30822-using-a-swtich-case-to-tell-which-variable-to-use/#findComment-142591 Share on other sites More sharing options...
trq Posted December 16, 2006 Share Posted December 16, 2006 Your method makes little sense, sorry. Doing it your way you would need another switch to see what the variable $div holds, and you'll just keep going around and around and.... Link to comment https://forums.phpfreaks.com/topic/30822-using-a-swtich-case-to-tell-which-variable-to-use/#findComment-142598 Share on other sites More sharing options...
emehrkay Posted December 16, 2006 Author Share Posted December 16, 2006 here is what i currently have, maybe you can see my problem.[code=php:0]switch($block){ case 'top': foreach($arr as $key => $value){ $this->_head_top .= $this->makeList($value); } break; case 'mid': foreach($arr as $key => $value){ $this->_head_mid .= $this->makeList($value); } break; case 'bot': foreach($arr as $key => $value){ $this->_head_bot .= $this->makeList($value); } break; }[/code]you see how in each case im just repeating the same foreach loop but the only difference is the variable that the return from makeList gets assigned to?i want to put that foreach loop after the switch, so that i dont have repeating code.[code=php:0]switch($block){ case 'top': //use $this->_head_top break; case 'mid': //use $this->_head_mid break; case 'bot': //use $this->_head_bot break; } foreach($arr as $key => $value){ $var_result_From_switch .= $this->makeList($value); }[/code]is it possible? Link to comment https://forums.phpfreaks.com/topic/30822-using-a-swtich-case-to-tell-which-variable-to-use/#findComment-142608 Share on other sites More sharing options...
trq Posted December 16, 2006 Share Posted December 16, 2006 One way would be to wrap it all in a function then have the switch make the descision and return the desired property. eg;[code]<?php function decide($block) { switch ($block) { case 'top': return $this->_head_top; break; case 'mid': return $this->_head_mid; break; case 'bot': return $this->_head_bot; break; } } $block = decide($block); foreach ($arr as $key => $value) { $block .= $this->makeList($value); }?>[/code]The whole logic stinks a bit but it should work. Im also assuming this is all taking place inside a class obviously.PS: What editor are you pasting these snippets from? Its doing terrible things to the formatting, I can't copy and paste your code properly. Link to comment https://forums.phpfreaks.com/topic/30822-using-a-swtich-case-to-tell-which-variable-to-use/#findComment-142615 Share on other sites More sharing options...
emehrkay Posted December 16, 2006 Author Share Posted December 16, 2006 dreamweaver. its tabbed over a few time for formatting.but i didnt even think to seperate that logic into another method (this is a class). or i could just put that foreach logic into the makeList method. and do[code=php:0]switch($block){ case 'top': $this->_head_top .= $this->makeList($arr); break; case 'mid': $this->_head_mid .= $this->makeList($arr); break; case 'bot': $this->_head_bot .= $this->makeList($arr); break;}[/code]thanks for your help Link to comment https://forums.phpfreaks.com/topic/30822-using-a-swtich-case-to-tell-which-variable-to-use/#findComment-142623 Share on other sites More sharing options...
trq Posted December 16, 2006 Share Posted December 16, 2006 That would probably be the cleanest option yeah... never thought of that. Link to comment https://forums.phpfreaks.com/topic/30822-using-a-swtich-case-to-tell-which-variable-to-use/#findComment-142624 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.