重复if语句12次

时间:2013-12-19 09:22:37

标签: php if-statement

是否有一种简单的方法可以在递增数字上重复if次查询12次。我的问题是

<?php 
global $post; $text = get_post_meta( $post->ID, '_cmb_music_file_url', true );
if (!empty($text)) {  ?>
<div class="row audiostyle" style="margin-top:45px;">
  <h3 class="col-md-1">1</h3>
  <h3 class="col-md-6"><?php global $post; $text = get_post_meta( $post->ID, '_cmb_music_title', true ); echo $text; ?> </h3>
  <h3 class="col-md-3" style="font-size:1em;"><a href="<?php global $post; $text = get_post_meta( $post->ID, '_cmb_buy_url', true ); echo $text; ?>" class="singlebutton" style="margin:0;">Buy Track</a></h3>
  <h3 class="col-md-2"><audio class="audio-player"src="<?php global $post; $text = get_post_meta( $post->ID, '_cmb_music_file_url', true ); echo $text; ?> " width="100%" preload="none"></audio> </h3> 
</div>

对于每个变量,我都有旁边的升序数字。例如,下一个if语句是

<?php } global $post; $text2 = get_post_meta( $post->ID, '_cmb_music_file_url2', true );
if (!empty($text2)) {  ?>
<div class="row audiostyle">
  <h3 class="col-md-1">2</h3>
  <h3 class="col-md-6"><?php global $post; $text = get_post_meta( $post->ID, '_cmb_music_title2', true ); echo $text; ?> </h3>
  <h3 class="col-md-3" style="font-size:1em;"><a href="<?php global $post; $text = get_post_meta( $post->ID, '_cmb_buy_url2', true ); echo $text; ?>" class="singlebutton" style="margin:0;">Buy Track</a></h3>
  <h3 class="col-md-2"><audio class="audio-player"src="<?php global $post; $text = get_post_meta( $post->ID, '_cmb_music_file_url2', true ); echo $text; ?> " width="100%" preload="none"></audio> </h3>
</div>

现在它可以工作,但必须有一个简单的方法来编写它来减少代码。它从testtest 12

7 个答案:

答案 0 :(得分:1)

使用它

<?php 
global $post; 
$text = get_post_meta( $post->ID, '_cmb_music_file_url', true );
if (!empty($text)) {  ?>
<div class="row audiostyle" style="margin-top:45px;">
        <h3 class="col-md-1">1</h3>
        <h3 class="col-md-6"><?php global $post; $text = get_post_meta( $post->ID, '_cmb_music_title', true ); echo $text; ?> </h3>
        <h3 class="col-md-3" style="font-size:1em;"><a href="<?php global $post; $text = get_post_meta( $post->ID, '_cmb_buy_url', true ); echo $text; ?>" class="singlebutton" style="margin:0;">Buy Track</a></h3>
        <h3 class="col-md-2"><audio class="audio-player"src="<?php global $post; $text = get_post_meta( $post->ID, '_cmb_music_file_url', true ); echo $text; ?> " width="100%" preload="none"></audio> </h3> 
</div>
<?php
}
for($i=2; $i<13; $i++){
$text = get_post_meta( $post->ID, '_cmb_music_file_url'.$i, true );
if (!empty($text)) {  ?>
<div class="row audiostyle" style="margin-top:45px;">
        <h3 class="col-md-1"><?php echo $i; ?></h3>
        <h3 class="col-md-6"><?php global $post; $text = get_post_meta( $post->ID, '_cmb_music_title'.$i, true ); echo $text; ?> </h3>
        <h3 class="col-md-3" style="font-size:1em;"><a href="<?php global $post; $text = get_post_meta( $post->ID, '_cmb_buy_url'.$i, true ); echo $text; ?>" class="singlebutton" style="margin:0;">Buy Track</a></h3>
        <h3 class="col-md-2"><audio class="audio-player"src="<?php global $post; $text = get_post_meta( $post->ID, '_cmb_music_file_url'.$i, true ); echo $text; ?> " width="100%" preload="none"></audio> </h3> 
</div>
<?php 
}
}
?>

答案 1 :(得分:1)

您也可以通过在php中使用range()函数来实现这一目标。

<?php
global $post;

foreach (range(1, 12) as $count) {
    $count = ($count == 1) ? '' : $count;
    $text = get_post_meta($post->ID, '_cmb_music_file_url' . $count, true);
    if (!empty($text)) {
        ?>
        <div class="row audiostyle" style="margin-top:45px;">
            <h3 class="col-md-1">1</h3>
            <h3 class="col-md-6">
                <?php
                $text = get_post_meta($post->ID, '_cmb_music_title' . $count, true);
                echo $text;
                ?> 
            </h3>
            <h3 class="col-md-3" style="font-size:1em;">
                <a href="<?php
                $text1 = get_post_meta($post->ID, '_cmb_buy_url' . $count, true);
                echo $text1;
                ?>" class="singlebutton" style="margin:0;">Buy Track</a>
            </h3>
            <h3 class="col-md-2">
                <audio class="audio-player"src="<?php
                       $text2 = get_post_meta($post->ID, '_cmb_music_file_url' . $count, true);
                       echo $text2;
                       ?> " width="100%" preload="none"></audio> 
            </h3> 
        </div>
        <?php
    }
}
?>

答案 2 :(得分:0)

你应该将12个变量放在这样的数组中:

$tests = array($test, $test2, ...);

然后写一个foreach语句:

<?php 
foreach($tests as $text) {
    if (!empty($text)) {  ?>
    <div class="row audiostyle" style="margin-top:45px;">
        <h3 class="col-md-1">1</h3>
        <h3 class="col-md-6"><?php global $post; $text = get_post_meta( $post->ID, '_cmb_music_title', true ); echo $text; ?> </h3>
        <h3 class="col-md-3" style="font-size:1em;"><a href="<?php global $post; $text = get_post_meta( $post->ID, '_cmb_buy_url', true ); echo $text; ?>" class="singlebutton" style="margin:0;">Buy Track</a></h3>
        <h3 class="col-md-2"><audio class="audio-player"src="<?php global $post; $text = get_post_meta( $post->ID, '_cmb_music_file_url', true ); echo $text; ?> " width="100%" preload="none"></audio> </h3> 
    </div>
    <?php
    } 
}
?>

答案 3 :(得分:0)

如果更改输入的名称,则可以使用数组:

而不是

<input name="_cmb_music_file_url1">
<input name="_cmb_music_file_url2">
<input name="_cmb_music_file_url3">

您可以使用

<input name="_cmb_music_file_url[1]">
<input name="_cmb_music_file_url[2]">
<input name="_cmb_music_file_url[3]">

然后在您的代码中,您可以将其作为数组循环。

答案 4 :(得分:0)

您可以考虑基本上有3种循环方法(为简单起见,我还遗漏了许多方法):

while()将重复,除非您设置条件使其停止,

foreach()将对数组的每个元素重复一次,并且

for()会重复一定次数。

我建议你对每个人都感到满意。 for可能有最大的陷阱,但最适合您的目的。

答案 5 :(得分:0)

for ($i = 1; $i <= 12; $i++)
{
    if ($i > 1)
    {
        $file_url = '_cmb_music_file_url' . $i;
        $title = '_cmb_music_title' . $i;
        $buy_url = '_cmb_buy_url' . $i;
    }
    else
    {
        $file_url = '_cmb_music_file_url';
        $title = '_cmb_music_title';
        $buy_url = '_cmb_buy_url';
    }

   global $post; $text = get_post_meta( $post->ID, $file_url, true );

   if (!empty($text)) {
?>
    <div class="row audiostyle" style="margin-top:45px;">
        <h3 class="col-md-1">1</h3>
        <h3 class="col-md-6"><?php global $post; $text = get_post_meta( $post->ID, $title, true ); echo $text; ?> </h3>
        <h3 class="col-md-3" style="font-size:1em;"><a href="<?php global $post; $text = get_post_meta( $post->ID, $buy_url, true ); echo $text; ?>" class="singlebutton" style="margin:0;">Buy Track</a></h3>
        <h3 class="col-md-2"><audio class="audio-player"src="<?php global $post; $text = get_post_meta( $post->ID, $file_url, true ); echo $text; ?> " width="100%" preload="none"></audio> </h3> 
    </div>
<? }
}

这样的事情可能有用。

答案 6 :(得分:0)

这是你的答案。您必须将增量变量与文本变量连接,如下所示。

    <?php for($i=1, $i<=12, $i++){

    global $post; $text.$i = get_post_meta( $post->ID, '_cmb_music_file_url'.$i, true );
if (!empty($text.$i)) {  ?>
<div class="row audiostyle" style="margin-top:45px;">
  <h3 class="col-md-1"><?php echo $i; ?></h3>
  <h3 class="col-md-6"><?php global $post; $text.$i = get_post_meta( $post->ID, '_cmb_music_title'.$i, true ); echo $text; ?> </h3>
  <h3 class="col-md-3" style="font-size:1em;"><a href="<?php global $post; $text = get_post_meta( $post->ID, '_cmb_buy_url'.$i, true ); echo $text; ?>" class="singlebutton" style="margin:0;">Buy Track</a></h3>
  <h3 class="col-md-2"><audio class="audio-player"src="<?php global $post; $text = get_post_meta( $post->ID, '_cmb_music_file_url'.$i, true ); echo $text; ?> " width="100%" preload="none"></audio> </h3>
</div>

<?php 
} //End if (!empty($text))

} //End of Foor loop 
?>