2013-05-16 55 views
3

重新排序我有三个变量:PHP - 使用循环

$title_order = 1; 
$image_order = 2; 
$content_order = 3; 

和用户可以重新排列上述变量/订货,像:

$title_order = 2; 
$image_order = 1; 
$content_order = 3; 

现在,按照这个变量我要重新排序如下HTML

<h1><?php title() ?></h1> 
<figure><?php thumbnail() ?></figure> 
<details><?php thumbnail() ?></details> 

如何显示这是每个变量的数字,比如:

<figure><?php thumbnail() ?></figure> // show image 1st if $image_orer = 1; 
<h1><?php title() ?></h1> // show h1 in 2nd if $title_order = 2; 
<details><?php thumbnail() ?></details> // show h1 3rd if $content_order = 3; 

请注意用户可以1,2和3

之间的变量设置为任何东西,所以请告诉我,我该如何实现这一目标。

+0

什么问题? – sectus

+3

什么?阅读问题,你会知道问题 – user007

回答

4
$result = ''; 
for ($i = 1; $i <= 3; ++$i) { 
    switch ($i) { 
     case $title_order: 
      $result .= '<h1>' . title() . '</h1>'; 
      break; 

     case $image_order: 
      $result .= '<figure>' . thumbnail() . '</figure>'; 
      break; 

     case $content_order: 
      $result .= '<details>' . thumbnail() . '</details>'; 
      break; 
    } 
} 

echo $result; 
+0

谢谢,我会尝试 – user007