1

我被 for 循环卡住了,因为我没有得到正确的语法,我需要它在 html 和 foreach 循环之间运行,以便它可以回显,我正在尝试修改我拥有的 wp 主题之一的简码,我是 php 新手,遇到语法问题,非常感谢您的帮助

  function testimonial( $atts, $content = null ) {
        $GLOBALS['tms_count'] = 0;
        $i = 1;
        $randomid = rand();

        do_shortcode( $content );

        if( is_array( $GLOBALS['tmss'] ) ){

            foreach( $GLOBALS['tmss'] as $tms ){

                $wrp[] = '<div>
                <div class="tm">
                    <p>'.$tms['content'].'</p>
                    <div>'.$tms['author'].'</div>
                    <div class="nonedi">
                        <div><strong>Services</strong></div>
                        <meta content="'.$tms['date'].'">'.$tms['date2'].'
                    </div>
                    <span>'.$tms['rating'].'</span>
                    Rating:<span class="star-img">'
                      /* This is where things go worng as my syntax is not right */
                        .$tms['rating'] = $test;
                    for ($x=1; $x==$test; $x++){
                        echo "<img>";
                    }
                    '</span>
                </div>';
                $i++;
            }
            $return = '<div class="nonedi">
                            <div class"name">Services</div>
                       </div>
                       <div class="testimonial-wrapper">'.implode( "\n", $wrp ).'</div>';
        }
        return $return;
    }


    function tms( $atts, $content = null) {
        extract(shortcode_atts(array(
            'author' => '',
            'date' => '',
            'date2' => '',
            'rating' => '',
        ), $atts));

        $x = $GLOBALS['tms_count'];
        $GLOBALS['tmss'][$x] = array( 'author' => sprintf( $author, $GLOBALS['tms_count'] ), 'date' => $date, 'date2' => $date2, 'rating' => $rating, 'content' =>  $content );
        $GLOBALS['tms_count']++;
    }
4

1 回答 1

0

因此,您不能像上面尝试的那样运行 for 循环,因为它会被视为字符串连接并且不会真正执行并分配结果。您的 for 循环也不正确。查看此处以了解有关PHP 中 for 循环的更多信息。

我已经移动了你的 for 循环,并且只将循环的结果分配回了字符串。希望有帮助。

foreach( $GLOBALS['tmss'] as $tms ){

    $img = "";
    for ($x = 1; $x <= $tms['rating']; $x++){
        $img .= "<img>";
    }

    $wrp[] = '<div>
    <div class="tm">
        <p>'.$tms['content'].'</p>
        <div>'.$tms['author'].'</div>
        <div class="nonedi">
            <div><strong>Services</strong></div>
            <meta content="'.$tms['date'].'">'.$tms['date2'].'
        </div>
        <span>'.$tms['rating'].'</span>
        Rating:<span class="star-img">'.$img.'</span>
    </div>';
    $i++;
}
于 2014-04-02T09:27:19.167 回答