0

I have tried using next() function with foreach loop returns the current element value.I am fetching data from database in foreach loop.

$result = $wpdb->get_results ( "SELECT * FROM interestclass 
                                where SortingCode='bake1r20210113' || 
                                    SortingCode='bake1r20210225' || 
                                    SortingCode='bake1r20210506' || 
                                    SortingCode='bake1r20210612' || 
                                    SortingCode='bake1r20210813'" );
foreach ( $result as $print )   {   
    $nextRowValue=next(next($result));
    echo "<td> working".$nextRowValue->LessonCode."</td><br>";
} 
1
  • [From comments under a given answer] “Actually I want to get the value of next lesson code so i can compare the value and break” - do it the other way around then. Compare the value of the current record, with that of the previous one. To have access to the latter, you don’t need to call any functions that mess with the array pointer (dangerous thing to do, inside a loop over the very same array), all you need to do is store the current record into a variable at the very end of your loop, so that you have access to it in the next loop iteration then. Commented Jun 8, 2021 at 9:44

2 Answers 2

2

$wpdb->get_results returns an array of resultset rows and a foreach processes over an array, so the foreach does all the moving down an array, all you need to do is use the $row as the current row you are processing

$results = $wpdb->get_results ( "SELECT * FROM interestclass 
                                where SortingCode='bake1r20210113' || 
                                    SortingCode='bake1r20210225' || 
                                    SortingCode='bake1r20210506' || 
                                    SortingCode='bake1r20210612' || 
                                    SortingCode='bake1r20210813'" );
foreach ( $results as $row )   {   
    echo "<td> working {$row->LessonCode}</td>";
} 

Additional code as per your comment below.

So if you want to test this row against the next row in the array, a simple change to the format of the foreach will provide you with the key to the array. You can then use that on the $results array to gain access to the next occurance in the array. Be careful, its easy doing this to exceed the bounds of the array, so first you need to check that will not happen with a simple IF

$results = $wpdb->get_results ( "SELECT * FROM interestclass 
                                where SortingCode='bake1r20210113' || 
                                    SortingCode='bake1r20210225' || 
                                    SortingCode='bake1r20210506' || 
                                    SortingCode='bake1r20210612' || 
                                    SortingCode='bake1r20210813'" );

$resultCount = count($results);

foreach ( $results as $key => $row ) {   
    // first make sure we have a next row
    if ( $key < $resultCount ){
        $nextRow = $results[$key+1];
        if ( $row->LessonCode == $nextRow->LessonCode ) {
            echo "<td> working {$row->LessonCode}</td>";
        }
    }
} 
Sign up to request clarification or add additional context in comments.

6 Comments

Actually I want to get the value of next lesson code so i can compare the value and break.
Check out the additional code. Not sure exactly what you want to test, equal or not equal but that should give you a good start point
Wouldn't it be better to use a for loop here, since this code assumes that the keys are ordered and numeric anyway?
@DB Cant remember the last time I used a for loop to process the contents of an array. But of course you could, but Better not sure about that
@RiggsFolly This is exactly the only use-case where i'm using for loops. Imo it makes it more clear, what you is happening. But both variants will fail, if the array does not have numeric values or the result is a generator.
|
0

If you run foreach ( $result as $print ) { the program iterates the $result array. In every loop $print is a element of $result, so next is not necessary. I think the code would be: foreach ( $result as $print ) { echo " working".$print->LessonCode."
";}

And I see tht in your code you use next(next($result)) it would jump 2 places in array, not one.

1 Comment

It was not even jumping to one place.I was just testing it if it is working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.