Forum Moderators: coopster
The 'dates' column for a record in the 'programs' look like this: &62&74&84&90, etc.
Each number represents a date with more details in the 'dates' table.
Echoing out $datenum will give one number like '63'. The problem is I want only first date and to ignore the rest per record. The SQL looks like this. Is it possible to only get the first search result of date per record?
foreach ($chosen_dates as $c) {
if ($c['day'] == $day_num) {
if ($section == false) { // This runs for printing courses and not programs
$coursenum = $c['coursenum'];
$name = mysql_query("SELECT name FROM courses WHERE keyword='$coursenum'");$name_row = mysql_fetch_object($name);
// if there are courses for this $day_num
print "<p><a href='course.php?index=".$c['coursenum']."'\n";
print "title='".$c['stime']."-".$c['etime']." ".$c['section']."'>".$name_row->name."</a></p>\n";
}
else { // This runs to print programs and not courses.
[b]$datenum = $c['datenum'];
echo "Datenum=$datenum";
$date_search = mysql_query("SELECT num, dates, category FROM programs WHERE dates REGEXP '&$datenum&'");
$date_found = mysql_num_rows($date_search);[/b]
//echo "Number of dates found = ".$date_found;
if ($date_found > 0) {
while($row = mysql_fetch_object($date_search)) {
//echo $c['section'] ." ";
//echo "cat= ". $row->num;
foreach($isPrinted as $printed){
if(!in_array(array('section'=>$c['section'], 'courseNum'=>$row->num), $isPrinted)){
print "<p><a href='program.php?index=".$row->num."'\n";
print "title='".$c['stime']."-".$c['etime']." ".$c['section']."'>".$row->category."</a></p>\n";
$isPrinted[] = array('section'=>$c['section'], 'courseNum'=>$row->num);
break;
}
}
}...
Is there such thing as maybe using a LIMIT with this kind of SQL statement? The one above is:
SELECT num, dates, category FROM programs WHERE dates REGEXP '&$datenum& Could you use something to the effect of:
SELECT num, dates, category FROM programs WHERE dates REGEXP '&$datenum& LIMIT 1?
I hope I have been clear enough.
Buddy
Yes, you can either use LIMIT or you can just get one object instead of using a while. That's this line:
while($row = mysql_fetch_object($date_search)) {
change to
$row = mysql_fetch_object($date_search))
And remove a closing } like the one right after the break; line.
Adding the LIMIT (just exactly like you posted) is easier for experimentation purposes.
==========================
CALENDAR (Sorted by sdate)
==========================
datenum ¦ sdate (has other columns not important)
--------------------------
39 ¦ 2007-07-21
43 ¦ 2007-08-20 etc...
This is the query that runs on the calendar to get the dates:
$dates = mysql_query("SELECT datenum, coursenum, UNIX_TIMESTAMP(sdate) AS 'sdate_chosen',
UNIX_TIMESTAMP(edate) AS 'edate_chosen', stime,
etime, section FROM calendar WHERE
MONTH(sdate)='$month' AND YEAR(sdate)='$year' ORDER BY sdate");
Dates and time are formatted and then put into an array with this code:
for ($i=$test_sday; $i <= $row.count; $i++) {
$chosen_dates[] = array('day'=>$i, 'stime'=>$test_stime, 'etime'=>$test_etime, 'section'=>$test_section, 'coursenum'=>$test_coursenum, 'datenum'=>$test_datenum);
}
}
$datenum is then used to get the search results of programs with this sql:
$date_search = mysql_query("SELECT num, dates, category FROM programs WHERE dates REGEXP '&$datenum&'");
$daDates = mysql_fetch_object($date_search);
The 'dates' column looks like this:
&63&80&72&52&73
It prints the $daDates with print_r() just fine which looks like this when printed out:
stdClass Object ( [num] => 3 [dates] => &70&83&72&84&73&85&74&86&75&87&69&88& [category] => System Analyst Level 1 )
The line in bold above does not work though. I would assume that would make
$alldates = &70&83&72&84&73&85&74&86&75&87&69&88&.
Array ( [0] => )
Any suggestions?
Thanks!
[edited by: jatar_k at 1:06 pm (utc) on Sep. 17, 2007]
[edit reason] fixed sidescroll [/edit]