3

I’m new to smarty. I’m trying to use switch and case function with smarty. This is the php code I use

$i=1;
while ($row = mysqli_fetch_array($sql)){

    switch($i%8){

            case 1:
            case 2:
                //DO Something Here
            break;
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
            case 0:
                //DO Something Else Here
            break;
        }
    $i++;

    }

My question is how do i apply this code to Smarty? Appreciate your time.

Ahmad Mobaraki
  • 7,426
  • 5
  • 48
  • 69
Jordyn
  • 1,133
  • 2
  • 13
  • 28
  • 1
    Not sure what you're wanting to do here exactly. Replicate this in Smarty? Send data to Smarty? Please add some details – Machavity Jan 26 '17 at 04:42
  • @Machavity yes exactly to replicated it in smarty. – Jordyn Jan 26 '17 at 04:59
  • I took a stab at it but your question is too vague to give you a solid answer. I suspect this is an [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem/66378#66378) – Machavity Jan 26 '17 at 13:12

1 Answers1

5

I'm not sure replicating in Smarty is the best idea. The concept of Smarty is to keep logic like this inside your controller. Furthermore, you would have to fully raw translate the data to Smarty to do this within Smarty. In other words, you'd have to loop over the data twice. Instead, I'd build the data into a structure like an array and pass that into Smarty. Than you can use a simple {foreach} in smarty to loop over the data.

$data = array();
$i=1;
while ($row = mysqli_fetch_array($sql)){

    switch($i%8){

            case 1:
            case 2:
                $data[$i][] = $row; //DO Something Here
            break;
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
            case 0:
                $data[$i][] = $row; //DO Something Else Here
            break;
        }
    $i++;

    }
$smarty->assign('data', $data);
Machavity
  • 30,841
  • 27
  • 92
  • 100