0

ニュース表示用のモジュールを作成しています。記事の情報を表示するためのより良い方法があるかどうか知りたいです。オン/オフ機能を使用して、さまざまな方法で情報パーツを表示できるようにしたいと考えています。例えば:

  1. 題名
  2. 文章
  3. 続きを読む
  4. 日にち
  5. 著者

あるいは単に

  1. 続きを読む
  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>
4

1 に答える 1

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'];
于 2012-04-09T06:32:02.173 に答える