Page 1 of 1
Get a recordset correctly into an array
Posted: Sat Sep 15, 2007 7:31 pm
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
Posted: Sat Sep 15, 2007 7:33 pm
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.
Posted: Sat Sep 15, 2007 7:34 pm
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
Looping through
Posted: Sat Sep 15, 2007 9:30 pm
by buddyq
feyd | Please use 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
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]