TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Returning pointer to 1st element in associative array (http://www.talkphp.com/absolute-beginners/4899-returning-pointer-1st-element-associative-array.html)

Dave 08-26-2009 06:31 PM

Returning pointer to 1st element in associative array
 
Hi folks,

I have an associative array. After some processing, the pointer is now past the last element.

How can I move the pointer back up to the first element without recalling the original query?

I searched for an answer, but most of the examples show how to "get" the first array. But I simply want to move the pointer back to the first array element.

Thanks!
Dave

sketchMedia 08-26-2009 07:07 PM

would reset be of any use to you?

Dave 08-26-2009 07:44 PM

Quote:

Originally Posted by sketchMedia (Post 28082)
would reset be of any use to you?

Thanks, sketchMedia. I tried reset(), but it does not return the pointer to the first element. Please take a look and see if I've used it wrongly.

Thanks!!

Code:

$c_pettype = 'horse' ; // From user.
$q = "SELECT *
        FROM t_petdata
      WHERE pettype = '$c_pettype' " ;
$q_result = mysql_query($q) or die('no data selected') ;
$q_rowarray = mysql_fetch_assoc($q_result) ;

echo "ELEMENT 1";
echo "<pre>";
print_r($q_rowarray);
echo "</pre>";

echo "using RESET() here..." ;
reset($q_rowarray) ;

$q_rowarray = mysql_fetch_assoc($q_result)
                        or die('query failed') ;
echo "SHOULD BE ELEMENT 1 AGAIN";
echo "<pre>";
print_r($q_rowarray);
echo "</pre>";

output:
==================
ELEMENT 1

Array
(
[petpk] => 1
[petname] => unicorn
[pettype] => horse
[petdesc] => spiral horn centered on forehead
[price] => 999.99
)

using RESET() here...SHOULD BE ELEMENT 1 AGAIN

Array
(
[petpk] => 2
[petname] => pegasus
[pettype] => horse
[petdesc] => flying: wings sprouting from back
[price] => 999.99
)

adamdecaf 08-26-2009 11:05 PM

I'm not sure but the ID of the array values may not change, so it looks like you are printing out the first value's data (array).

Wildhoney 08-27-2009 01:32 AM

You could use mysql_data_seek like below. Although it seems peculiar to me how you're using this.

php Code:
$c_pettype = 'horse' ; // From user.
$q = "SELECT *
        FROM t_petdata
       WHERE pettype = '$c_pettype' "
;
$q_result = mysql_query($q) or die('no data selected') ;
$q_rowarray = mysql_fetch_assoc($q_result) ;

echo "ELEMENT 1";
echo "<pre>";
print_r($q_rowarray);
echo "</pre>";

echo "using MYSQL_DATA_SEEK() here..." ;
mysql_data_seek($q_result, 0) ;

$q_rowarray = mysql_fetch_assoc($q_result)
            or die('query failed') ;
echo "SHOULD BE ELEMENT 1 AGAIN";
echo "<pre>";
print_r($q_rowarray);
echo "</pre>";

Dave 08-27-2009 01:43 PM

Wildhoney --

Thanks very much. That was very helpful. In all my Googling, I did not come across a mention of "mysql_data_seek." Wonder why?

Anyway, I read more about the mysql_data_seek function on php.net, and the user posts were very interesting.

Here is Kenneth Nash's interesting UDF from that discussion:

Code:

/*here is a nice function for converting
a mysql result row set into a 2d array,
a time saver if need small data from
several rows, saves you from having to
do Alot of queries... would be nice to
have this built into PHP future versions :) */

// simple example query

$r = mysql_query("select user,id,ip from accounts limit 10");

// starts the for loop, using mysql_num_rows() to count
// total amount of rows returned by $r

for($i=0;
    $i<mysql_num_rows($r);
    $i++)
{
  // advances the row in the mysql resource $r
  mysql_data_seek($r,$i);
 
  // assigns the array keys, $users[row][field]
  $users[$i]=mysql_fetch_row($r);
}

// simple, hope someone can use it :)
// -Kenneth Nash



All times are GMT. The time now is 09:00 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0