Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8332369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:42:10+00:00 2026-06-09T02:42:10+00:00

I have an array with all the results of a query search. My problem

  • 0

I have an array with all the results of a query search.

My problem is that i need to select one row from this array, and then be able to select the next and previous row.

here is my code

function getUserProf(array $data, $faceid)
{
    //make sure that all data is saved in an array
    $userprof = array();

    //makes the array with the info we need
    foreach ($data as $val)
        if ($val['faceid'] == $faceid){
            $userprof = array ("id" => $val["id"], "total" => $val["total"], "faceid" => $val["faceid"], "lname" => $val["lname"], "fname" => $val["fname"], "hand" => $val["hand"], "shot1" => $val["shot1"], "shot1" => $val["shot1"], "shot2" => $val["shot2"], "shot3" => $val["shot3"], "shot4" => $val["shot4"], "shot5" => $val["shot5"]);
        }
        $next = next($data);//to get the next profile
        $prev = prev($data);//to get the next profile
        $userprofinfo = array('userprof' => $userprof, 'next' => $next);//make a array with the profile and the next prof and the prev prof

    //we return an array with the info
    return $userprofinfo;
}

This works somehow, but it doesn’t give me the correct next and previous row?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:42:11+00:00Added an answer on June 9, 2026 at 2:42 am

    Your problem is that prev() moves the array pointer -1 and next() moves it +1 again, resulting in $next being the exact same row as the current one where you started before invoking prev().

    Also, you get $prev and $next after the complete foreach() run, which leaves the array pointer at the end of the array. (so you will always get the last element)

    Try this instead:

    function getUserProf(array $data, $faceid) {
        foreach ($data as $key => $val) {
            if ($val['faceid'] == $faceid) {
                return array( 
                    'userprof' => $val,
                    'prev'     => isset($data[$key-1]) ? $data[$key-1] : array(),
                    'next'     => isset($data[$key+1]) ? $data[$key+1] : array()
                );
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just have the array of 15 values that all need to be inserted
I have a query which shows all images from a user. (In this case
I have a query which shows all images from a user. (In this case
I have a search result from MySQL query or Array or something else. The
Ok so I have an array that looks like this. [Enter Sandman, One, Nothing
I have an array that contains objects from two models: @search_results = User.find(:all, :conditions
I have an array of variables all containing text. I want to select only
I have an array returned by the function Search.all : => [#<Search id: 7,
I have an array in php that contains all the lines of a text
I have an array of views, and I need to hide them all, so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.