user1951739
user1951739

Reputation: 135

what the best way to write this without repeating myself

A php file is going to receive a variable between 1 and 30 from a form using

 $style = $_GET['style'];

i want to then pass $style to help me choose the css stlye the user has selected so for example if the choice is style 3 then css stlye three will be passed to an image like this

<img style="<?php $choice3?>";  src="xxx.png" />

so what i have so far is this

<?php
 $style = $_GET['style'];

function choice1()
{
$choice1 = "display: block;padding:5px; etc, etc etc";
      return $choice1;      }   
function choice2()
{
$choice2 = "display: block;padding:10px; etc, etc etc";
    return $choice2;        }   
function choice3()
{
$choice3 = "display: block;padding:20px; etc, etc etc";
    return $choice3;        }   

if ($style == 1){
?>
<img style="<?php $choice1?>";  src="xxx.png" />
<?php 
}
if ($style == 2){
?>
<img style="<?php $choice2?>";  src="xxx.png" />
<?php 
}
.......and so on till 30        
?>                  

I am pretty sure there is an easier smarter way to convey my logic.

Upvotes: 2

Views: 115

Answers (5)

Dark Falcon
Dark Falcon

Reputation: 44181

How about an array? Don't forget to check that $style is in the proper range before using the code below.

<?php
$style = $_GET['style'];
$styles = array(
  1=>"display: block;padding:5px; etc, etc etc",
  2=>"display: block;padding:15px; etc, etc etc",
  3=>"display: block;padding:35px; etc, etc etc",
  4=>"display: block;padding:55px; etc, etc etc",
  // etc.
);
?>

<img style="<?php echo $styles[$style]; ?>";  src="xxx.png" />

Upvotes: 6

Steven McElveen
Steven McElveen

Reputation: 459

I might set it up like this:

$myStyles = new Array();
$myStyles[0] = "display: block;padding:5px; etc, etc etc";
$myStyles[1] = "display: block;padding:10px; etc, etc etc";

etc...

function getStyles($style){
      return $myStyles[$style - 1];
}

<img style="<?php echo getStyles($_GET['style']; ?>";  src="xxx.png" />

Not tested...

Upvotes: 0

peterchon
peterchon

Reputation: 250

I think your code looks ok - but needs a print/echo before the var.

<img style="<?php print $choice2; ?>" src="..." />

Also, you could try using a switch statement.

----- edit -----

Instead of applying the style, I would pass a class name.

<img class="<?php print $choice2; ?>" src="..." />

Then you can always specify the CSS in your stylesheet instead of inline.

Upvotes: 0

user849001
user849001

Reputation:

I would change your function to handle all the borders instead of having multiple so something like:

  function border($style){
  switch ($style) {
    case 0:
        $border = "display: block;padding:5px; etc, etc etc";
        break;
    case 1:
        $border = "display: block;padding:5px; etc, etc etc";
        break;
    case 2:
        $border = "display: block;padding:5px; etc, etc etc";
        break;
    default:
        $border = "display: block;padding:5px; etc, etc etc";
   }
   return $border;
  }

Upvotes: 0

Harshal
Harshal

Reputation: 3622

It seems that Only the Padding Value is changing than ,why you are not trying this :

 $style = $_GET['style'];

    <img style="display: block;padding:<?php echo $choice3; ?>px;"  src="xxx.png" />

Upvotes: 0

Related Questions