php simplification question

GamesNET2
GamesNET2 used Ask the Experts™
on
Hi there,

Does anyone know how I could simplify the code below? It's not easily expandable how it is at the moment -

Thanks!


<?php if ($count == 1 or $count == 4 or $count == 7 ) : 
echo "testimonial_1" ;
elseif ($count == 2 or $count == 5 or $count == 8) : 
echo "testimonial_2" ;
elseif ($count == 3 or $count == 6 or $count == 9) :
echo "testimonial_3"; 
 endif ?>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
switch($count){

      case '1':

              echo "testimonial_1" ;
break;

    case '2':

              echo "testimonial_2" ;
break;

    case '3':

           echo "testimonial_3" ;
break;

     case '4'

          echo "testimonial_1" ;
break;

}

you can expand as much as you want on this.
Hi there, you could also reduce it to the following

 
switch($count){

      case '1':
      case '4':
      case '7':

        echo "testimonial_1" ;
        break;

    case '2':
    case '5':
    case '8':

        echo "testimonial_2" ;
        break;

    case '3':
    case '6':
    case '9':

       echo "testimonial_3" ;
       break;

}

Open in new window


--TheDoctor
Top Expert 2007
Commented:
simple just use mod 3 for counter and check the rest, so it's automaticly exands to any number.
<?php if ($count%3 == 1) : 
echo "testimonial_1" ;
elseif ($count%3 == 2) : 
echo "testimonial_2" ;
elseif ($count%3 == 0) :
echo "testimonial_3"; 
 endif ?>

Open in new window

Author

Commented:
Exactly what I was after! thanks,

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial