TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 08-26-2009, 06:31 PM   #1 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default 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
Dave is offline  
Reply With Quote
Old 08-26-2009, 07:07 PM   #2 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

would reset be of any use to you?
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
Dave (08-27-2009)
Old 08-26-2009, 07:44 PM   #3 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default

Quote:
Originally Posted by sketchMedia View Post
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
)
Dave is offline  
Reply With Quote
Old 08-26-2009, 11:05 PM   #4 (permalink)
The Addict
 
Join Date: May 2009
Posts: 287
Thanks: 5
adamdecaf is on a distinguished road
Default

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).
__________________
My Site
adamdecaf is offline  
Reply With Quote
The Following User Says Thank You to adamdecaf For This Useful Post:
Dave (08-27-2009)
Old 08-27-2009, 01:32 AM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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>";
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
Dave (08-27-2009)
Old 08-27-2009, 01:43 PM   #6 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default

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
Dave is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Returning an array from a function captainmerton Absolute Beginners 4 07-15-2009 08:45 PM
how to delete an element from an array without knowing its index position tech Absolute Beginners 9 06-25-2009 09:48 PM
How to delete array element in function benton General 4 04-02-2009 01:12 AM
array elements into variables and values Dave Absolute Beginners 7 06-20-2008 01:56 PM
Part 1: Getting Started with Array Functions Wildhoney Absolute Beginners 6 10-01-2007 10:53 AM


All times are GMT. The time now is 01:37 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design