Help using array to keep track of what rows have been used.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tymlls05
Forum Commoner
Posts: 30
Joined: Tue Nov 01, 2005 1:30 pm

Help using array to keep track of what rows have been used.

Post by tymlls05 »

Hi I have a script that randomly selects a row in a database and then runs a function after that. I need help building an array (or maybe you know a better way) that will keep track of which rows have been used so that they are not used again until all other rows have been used.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Help using array to keep track of what rows have been used.

Post by AbraCadaver »

tymlls05 wrote:Hi I have a script that randomly selects a row in a database and then runs a function after that. I need help building an array (or maybe you know a better way) that will keep track of which rows have been used so that they are not used again until all other rows have been used.
You would probably build the array from a unique key in the table. When you get the row just add a new item to the array with the id and then later you can check the array:

Code: Select all

// $result = select * from table
// $row = fetch array from result
if(!in_array($row['id'], $used)) {
    $used[] = $row['id'];
    your_function($row);
}
If this is needed across page loads then you can put the array in the session.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
tymlls05
Forum Commoner
Posts: 30
Joined: Tue Nov 01, 2005 1:30 pm

Re: Help using array to keep track of what rows have been used.

Post by tymlls05 »

Thank you for the reply. it definitely helped me progress a bit. it will run my query a few times and then I get a time out error.

Fatal error: Maximum execution time of 30 seconds exceeded in /home/tymlls05/public_html/sallison/autoEvent.php on line 220


Here is an example of what I am trying to run

Code: Select all

 
<?
    //Select RANDOM EVENT (AGENT) TO ASSIGN TO DAY
    $offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `event_categories_events` WHERE event_category_id = '2'  ") or die(mysql_error());
    $offset_row = mysql_fetch_object( $offset_result );
    $offset = $offset_row->offset;
    $floor_events = mysql_query( " SELECT * FROM `event_categories_events` WHERE event_category_id = '2' LIMIT $offset, 1 " ); 
 
    //Grab Random Event from All Events  (Agents) Assigned To The Floor Schedule Category
    while($grabFloorEvent = mysql_fetch_array($floor_events)){
        //For every event id we grab, grab the event (agent) details.
        $agent=$grabFloorEvent['event_id'];
 
//The next line is line 219, the line after is where I am getting an error.
        while(in_array($agent, $used)) {
            while($grabFloorEvent = mysql_fetch_array($floor_events)){
                $agent=$grabFloorEvent['event_id'];
                break;
            }
        }
?>
 
I do not want the while loop to exit until $grabFloorEvent = mysql_fetch_array($floor_events) has selected a row that has not been used yet.
Post Reply