Jump to content

Need help with php increment ...


AshleyQuick

Recommended Posts

I believe it's called increment. Either way, I cannot figure out how to simply count up beginning at 1 (in red in the div below) ...

 

function wp_rss( $url, $num_items = -1 ) {

    if ( $rss = fetch_rss( $url ) ) {

        echo '<div id="charts">';

 

        if ( $num_items !== -1 ) {

            $rss->items = array_slice( $rss->items, 0, $num_items );

        }

 

        foreach ( (array) $rss->items as $item ) {

            printf(

                '<div id="chart_lt">1</div><div id="chart_rt">%3$s</div>',

                esc_url( $item['link'] ),

                esc_attr( strip_tags( $item['description'] ) ),

                htmlentities( $item['title'] )

            );

        }

 

        echo '</div>';

    } else {

        _e( 'An error has occurred, which probably means the feed is down. Try again later.' );

    }

}

Link to comment
Share on other sites

increment is done with ++

in a loop, $count++; would increment $count + 1 for each loop.

 

function wp_rss( $url, $num_items = -1 ) {
    if ( $rss = fetch_rss( $url ) ) {
        echo '<div id="charts">';

        if ( $num_items !== -1 ) {
            $rss->items = array_slice( $rss->items, 0, $num_items );
        }
//start count
$i = 1;
        foreach ( (array) $rss->items as $item ) {
            printf(
                '<div id="chart_lt">'.$i.'</div><div id="chart_rt">%3$s</div>',
                esc_url( $item['link'] ),
                esc_attr( strip_tags( $item['description'] ) ),
                htmlentities( $item['title'] )
            );
//increment
$i++;
        }

        echo '</div>';
    } else {
        _e( 'An error has occurred, which probably means the feed is down. Try again later.' );
    }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.