Page 1 of 1
Help using array to keep track of what rows have been used.
Posted: Mon Feb 15, 2010 2:26 pm
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.
Re: Help using array to keep track of what rows have been used.
Posted: Mon Feb 15, 2010 2:44 pm
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.
Re: Help using array to keep track of what rows have been used.
Posted: Mon Feb 15, 2010 3:29 pm
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.