Get a recordset correctly into an array

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
buddyq
Forum Newbie
Posts: 5
Joined: Wed Sep 12, 2007 4:23 pm

Get a recordset correctly into an array

Post by buddyq »

I have a problem. I have a column called 'dates' inside table called 'Programs'.

The dates are strung together with '&'. For example: 2007-10-01&2007-01-15... etc...

I need to print only the first date of each program. How can I do this? I know I would probably need to use explode to separate the dates??

I'm learning PHP and still a beginner. Thanks in advance.

Buddy
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Yes, explode() would be needed. However a better database design is likely in order. Normalized your table would be split in at least two. One for the dates another for all the other data that is in the rest of the existing record.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Yes, use explode(). If you already have the dates into a PHP variable, then getting the first date will be really easy.

Code: Select all

$dates = explode('&', $yourvariableholdingthedates);
echo $dates[0]; //prints the first date
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
buddyq
Forum Newbie
Posts: 5
Joined: Wed Sep 12, 2007 4:23 pm

Looping through

Post by buddyq »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hey guys I have this loop which populates the $isPrinted array.

Code: Select all

$isPrinted = array();
while ($day_num <= $days_in_month) { 
	$newDay = mktime(0,0,0,$month, $day_num, $year); 
	$day_of_week = date('D', $newDay);
	foreach ($chosen_dates as $c) {
		if ($c['day'] == $day_num) {
			if ($section == false){
			}
			else
			{
			$datenum = $c['datenum'];
			echo $datenum. " ";
			$date_search = mysql_query("SELECT num, category FROM programsWHERE dates REGEXP '&$datenum&'");
			$date_found = mysql_num_rows($date_search);
				if ($date_found > 0) {
					while($row = mysql_fetch_object($date_search)) {
						
						$isPrinted[] = array('section'=>$c['section'], 'courseNum'=>$row->num);
					}
				}
			}
		}
	}
	$day_num++;
}
When I do a print_r($isPrinted) the output looks like this:

Code: Select all

Array ( [0] => Array ( [section] => 2 [courseNum] => 9 ) [1] => Array ( [section] => 1 [courseNum] => 3 ) [2] => Array ( [section] => 1 [courseNum] => 7 ) [3] => Array ( [section] => 2 [courseNum] => 3 ) [4] => Array ( [section] => 2 [courseNum] => 10 ) [5] => Array ( [section] => 3 [courseNum] => 9 ) [6] => Array ( [section] => 1 [courseNum] => 3 ) [7] => Array ( [section] => 1 [courseNum] => 3 ) )
As you can see there duplicates in this array. How do I check for duplicates when adding to this array? I dont know how to use in_array with an array since this is a multidimensional array.

Buddy


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply