$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Return to the beginning of the results of a for loop|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Return to the beginning of the results of a for loop

matteradmin9PV0评论

I am currently doing a little script (and I am rather beginner), I would like to know how to return to the result number one of a for loop after the table containing the results have been traveled?

here's the code:

$currenttPostId = get_the_ID();
$theCategory = get_the_terms(get_the_ID(),'category');
$prevNext = array();

if (!empty($search_args)) {

    $search_args['posts_per_page'] = -1;


    for ($i = 0; $i < count($search_results); $i++) {
        if ( $search_results[$i]->ID == $currenttPostId ) {
            $prevNext[] = $search_results[$i - 1];
            $prevNext[] = $search_results[$i + 1]; 
        }
    }
}

I am currently doing a little script (and I am rather beginner), I would like to know how to return to the result number one of a for loop after the table containing the results have been traveled?

here's the code:

$currenttPostId = get_the_ID();
$theCategory = get_the_terms(get_the_ID(),'category');
$prevNext = array();

if (!empty($search_args)) {

    $search_args['posts_per_page'] = -1;


    for ($i = 0; $i < count($search_results); $i++) {
        if ( $search_results[$i]->ID == $currenttPostId ) {
            $prevNext[] = $search_results[$i - 1];
            $prevNext[] = $search_results[$i + 1]; 
        }
    }
}
Share Improve this question edited Feb 14, 2019 at 11:47 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Feb 14, 2019 at 11:37 Frenchy_WPFrenchy_WP 191 silver badge6 bronze badges 2
  • 1 What exactly do you mean by "return to the result number one of a for loop after the table containing the results have been traveled"? What do you want to achieve? – Krzysiek Dróżdż Commented Feb 14, 2019 at 11:46
  • Let's say that I have a query that shows me a page of 12 articles, when we click on one of these articles, it brings us back to the single page, on this single page, I created a pagination brings up to the twelfth article and what I would like is that the twelfth article brings me back to the first one when I click on next – Frenchy_WP Commented Feb 14, 2019 at 11:55
Add a comment  | 

1 Answer 1

Reset to default 0

You can instantly access any position in an array by specifying the index.

i.e. to get the first element of the $search_results array:

$first_result = $search_results[0];

Note if you're not used to working with arrays, they are "0" indexed in most languages, which means the first element's index is 0, the second is 1, etc.

This is what your loop is basically doing as well. Each time it accesses a different number directly, $search_results[$i] is just putting the value of $i in the same spot as I had the 0 above.

You can also change the value of $i in your loop if you want to start the loop over, for instance. Adding the line:

$i = 0;

...would send you back to the beginning. However I would not recommend you work with the index of a loop until you are quite comfortable with them.

Post a comment

comment list (0)

  1. No comments so far