Question

I have got stuck with for loop as I not getting the syntax right, I need it to run in between the html and foreach loop so that it can echo , I am trying to modify the shortcode on one of the wp theme that I have, I am new to php and I am facing issues with the syntax so help will be much appreciated

  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']++;
    }
Was it helpful?

Solution

So, you can't run a for loop like you tried above as it would be considered as string concatenation and wouldn't really execute and assign the result. Your for loop was incorrect as well. Take a look here to learn more about for loops in PHP.

I have moved your for loop and assigned only the result of the loop back to the string. Hope that helps.

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++;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top