Re: Finding next recored in a array

From: Date: Mon, 17 Sep 2007 19:57:56 +0000
Subject: Re: Finding next recored in a array
References: 1 2 3 4 5 6 7 8  Groups: php.general 
Request: Send a blank email to php-general+get-262179@lists.php.net to get a copy of this message
Richard Kurth wrote:
What I am trying to is get all the days from a table where email campaign = number. and then look at the last day that was sent and find it in the list and get the next day that follows that day. At this point the script below is not working. So if you have any Ideas that would be a better way of doing this please let me know. $query = "SELECT day FROM emailcampaign where campaign_id = '$emailcampaign' AND member_id = '$members_id'"; $DB_Change_Campaign_Results = safe_query($query); for ($i=0; $i < mysql_num_rows($DB_Change_Campaign_Results); $i++) { $Change_Campaign_row = mysql_fetch_array($DB_Change_Campaign_Results); $Campaign = $Change_Campaign_row['day']; $Campaign_array[$Campaign] = $Change_Campaign_row; } $k = array_search($val,$Campaign_array); if ($k + 1 < count($Campaign_array)){ echo $Campaign_array[$k + 1]; }
You can simplify this greatly: $Campaign_array = Array(); $DB_Change_Campaign_Results = safe_query($query); while ($row = mysql_fetch_array($DB_Change_Campaign_Results)) { array_push($Campaign_array, $row['day']); } Because you're only selecting day from the table, your row data consists of only one column. You're repeating stuff unnecessarily by doing this: $Campaign = $Change_Campaign_row['day']; $Campaign_array[$Campaign] = $Change_Campaign_row; IOW, for the row that contains '5', you'd end up with $Campaign_array[5] == an array that essentially just points to the value '5'. I also noticed that your dump shows that the days begin with 0. If these are calendar days you're adding a further layer of complexity. brian

« previous php.general (#262179) next »