Forum Moderators: coopster

Message Too Old, No Replies

Grabbing only first var from results

         

buddyq

6:35 am on Sep 16, 2007 (gmt 0)

10+ Year Member



Hi, i'm working on a calendar program that I inherited from a sick Webmaster somewhere in the world. Below is part of the code that chooses and compares dates to print on the calendar. The part in bold is what I think is going to be the key to solving my problem, I just don't know how to go about it.

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

cameraman

7:21 am on Sep 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, buddyq!

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.

buddyq

8:36 pm on Sep 16, 2007 (gmt 0)

10+ Year Member



The reason for the while loop is because the date search could find multiple programs that start on the same date so it needs to loop through how many results it found and print each one out on that date. This is so confusing to put together for me. Here is a peek into the 'calendar' table.

==========================
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

So the search result returns a record if it finds $datenum in the 'dates' column. I believe the first number, in this case above would be 63 would be the first date the program starts on. I need to take that string and just get the first number '63' out of it and then see if that is equal to $datenum and if it is, print it on the calendar. I tried using explode but it's not working. When testing I am trying this code:[
$date_search = mysql_query("SELECT num, dates, category FROM programs WHERE dates REGEXP '&$datenum&'");
$daDates = mysql_fetch_object($date_search);
print_r($daDates);
$alldates = $daDates['dates'];
echo $alldates;
$startnum = explode("&",$alldates, 1);
print_r($startnum);

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&.

Then I could run it through explode to get just the first date of '70' but it doesn't work. The last line prints this:

Array ( [0] => )

Any suggestions?

Thanks!

[edited by: jatar_k at 1:06 pm (utc) on Sep. 17, 2007]
[edit reason] fixed sidescroll [/edit]

cameraman

1:12 am on Sep 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're getting a blank first element from the explode because of the leading & in the string.
You could either trim it off before exploding or just ignore element [0].