Jump to content

using a swtich case to tell which variable to use??


emehrkay

Recommended Posts

[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
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]
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
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?
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.
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

Archived

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

×
×
  • Create New...

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.