2012-04-09 88 views
0

我正在爲新聞顯示創建一個模塊。我想知道是否有更好的方式來顯示我的文章的信息。我希望能夠以不同的方式顯示信息部分,並具有開/關功能。例如:以不同方式顯示字符串

  1. 標題
  2. 文本
  3. Readmore
  4. 日期
  5. 作者

或只是

  1. Readmore
  2. 標題

我在考慮這段代碼,但我相信有更好的解決方案嗎?

<div id="pos1"> 
<?php 
if (show = 1) { 
echo 'Here will be title'; 
elseif (show = 2) { 
echo 'Here will be text'; 
elseif (show = 3) { 
echo 'Here will be readmore'; 
..... 
else { $string = 'nothing here'; } 
?></div> 

<div id="pos2"> 
<?php 
if (showsecond = 1) { 
echo 'Here will be title'; 
elseif (showsecond = 2) { 
echo 'Here will be text'; 
elseif (showsecond = 3) { 
echo 'Here will be readmore'; 
..... 
else { $string = 'nothing here'; } 
?></div> 
+0

是'表演,showsecond'變量?它們來自 – safarov 2012-04-09 06:30:14

+0

是的,它是一個變量 – sdfgsdfg 2012-04-09 06:36:17

+1

'show'和'showsecond'不是有效變量,php會將它們解釋爲文字字符串'show'和'showsecond'所有變量都必須以「$」作爲前綴 – nathanjosiah 2012-04-09 06:38:41

回答

0

你可以做這樣的事情:

$info = array(
    1 => 'This will be the title', 
    2 => 'this will be the text', 
    3 => 'This is the readmore', 
    4 => 'Date', 
    5 => 'Author' 
); 

echo $info[$show]; 

編輯:

$info = array(
    1 => array(
     'text' => 'This will be the title', 
     'styles' => 'styling info goes here' 
    ), 
    2 => array(
     'text' => 'This will be the author', 
     'styles' => 'styling info goes here' 
    ), 
    3 => array(
     'text' => 'This will be the date', 
     'styles' => 'styling info goes here' 
    ) 
); 

echo $info[$show]['text']; 
+0

我需要添加divs爲他們每個人造型風格。我看不出這可以在這裏完成..? – sdfgsdfg 2012-04-09 06:40:53

+0

請看我更新的答案 – nathanjosiah 2012-04-09 06:43:42

+0

這是最好的!謝謝。 – sdfgsdfg 2012-04-09 06:54:30

相關問題