linaa
linaa

Reputation: 11

how to get next value of array in PHP

I want when user enter a number, if number was in database, get next value from array
I want get next value from current value in array, but this code go first of array

    if ($value['number'] == $number) {

            $result = mysqli_query( $con,"SELECT user_name FROM tbl_users WHERE  user_name ='".$value['name']."'" );
            if ($result && mysqli_num_rows($result) > 0) {

                $iter = new \ArrayIterator($data);
                $iter->next();
                $nextKey = $iter->key();
                $nextValue = $iter->current();
                $user_id = $value['number'];
                $user_name = $value['name'];

            } 
        }

Upvotes: 1

Views: 105

Answers (2)

Oscar Lidenbrock
Oscar Lidenbrock

Reputation: 816

I think that your problem is the iterator, but you don't need

Change this:

$iter = new \ArrayIterator($data);
$iter->next();
$nextKey = $iter->key();
$nextValue = $iter->current();
$user_id = $value['number'];
$user_name = $value['name'];

for this:

/* This will return an array with "key" and "value" items and advance the array curson one item */
$nextValue = each($data); 
print $nextValue["key"];
print $nextvalue["value"];

Upvotes: 0

dWinder
dWinder

Reputation: 11642

This is not the way to loop over mysqli result (php documantation). you should try this way:

while ($row = mysqli_fetch($result)) {
   $user_id = $row['number'];
   $user_name = $row['name'];
   // do what ever you need with those value
}

(also, your $data var in the question is not define)

Upvotes: 1

Related Questions