If or where statement from another table

12 posts by 3 authors in: Forums > CMS Builder
Last Post: May 23, 2016   (RSS)

I've searched the forum but can't seem to find an answer...

I have a list of courses, where the title field is the name of the course.  

     Course Name: <?php echo htmlencode($coursesRecord['title']) ?><br/>

Each course has one or multiple course dates, so I've created a separate section called calendar, so I can sort by date, and also hide one if that date has expired (unless there's an easier, automatic way to do that?).  I've used the drop down list to select the course name to (hopefully) somehow link the two.  Now I am stuck.

<?php foreach ($calendarRecords as $record): ?>
Record Number: <?php echo htmlencode($record['num']) ?><br/>
Course (value): <?php echo $record['title'] ?><br/>
Course (label): <?php echo $record['title:label'] ?><br/>
Date: <?php echo date("D, M jS, Y g:i:s a", strtotime($record['date'])) ?><br/><!-- For date formatting codes see: http://www.php.net/date -->
_link : <a href="<?php echo $record['_link'] ?>"><?php echo $record['_link'] ?></a><br/>
<hr/>
<?php endforeach ?>

If for example, it's Course ABC showing on the page, how do I get the respective course dates to appear from the calendar?  I get that it has to do with the record number (which they share in common), but am stuck on whether it's a IF or WHERE or something else?

Any suggestions would be gratefully received.

Thanks in advance.

Sorted out the date issue, but not the other part...

// load records from 'calendar' that have a date greater than 1 second before midnight today.
list($calendarRecords, $calendarMetaData) = getRecords(array(
'tableName' => 'calendar',
'where' => "'date' > '$today'",
'loadUploads' => true,
'allowSearch' => false,
));

By Damon - May 18, 2016

Hi,

From your code example so far, I can see the fields in the Calendar section editor. Is the other section editor just for the course names?

What is the end result you are after? The course dates grouped under the course name?

Let me know and then I can help put together some code examples.

Thanks!

Cheers,
Damon Edis - interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By degreesnorth - May 18, 2016 - edited: May 18, 2016

Hi Damon

I have another list called Calendar, which just lists all of the courses and their specific date.  Course A might have 3 different dates, Course B 6 different dates, etc.  And new dates will be added as the calendar year progresses.  What I want to do is list these dates on the Course A or Course B, etc detailed pages, and have the flexibility to order by first forthcoming date, next sequential date.  An example of a course details (eg. Course A) is http://cp124.ezyreg.com/~coco6119/codelime_courses2.php?1 

// load records from 'calendar'
list($calendarRecords, $calendarMetaData) = getRecords(array(
'tableName' => 'calendar',
'loadUploads' => true,
'allowSearch' => false,
));

and the two fields are 

Record Number: <?php echo htmlencode($record['num']) ?><br/>
Course (value): <?php echo $record['title'] ?><br/>
Course (label): <?php echo $record['title:label'] ?><br/>
Date: <?php echo date("D, M jS, Y g:i:s a", strtotime($record['date'])) ?><br/><!-- For date formatting codes see: http://www.php.net/date -->

It's sort of a fix in the absence of a calendar type plugin.  The calendar they have at https://www.phpjabbers.com/event-booking-calendar/ is a great piece of calendar software, but it doesn't plug in with CMS Builder and I can't extract just a string of single course dates for a specific course.  So this is the compromise, combined with www.machform.com as the form/payment editor.

Any help you could provide with the where/if statement would be wonderful.

Thanks in advance.

Carole

By garyhoffmann - May 19, 2016 - edited: May 19, 2016

You probably have, in your calendar table, a field that cross-references the course table.  For example, maybe you have a "coursenum" field that uses the "num" from the course table as it's value.

Assuming you have your current course record in an array called $courseRecord, and assuming I understand your question properly, you can add where => "coursenum={$courseRecord['num']}" to your getRecords command.

Also, regarding your dates, you can add publishDate and removeDate fields to your section and then the getRecords command will not pull records that haven't been published yet or records that have expired (past their remove date) so you don't have manually expire them.  However, you can also use the way you are doing it.  Either way works fine.

Thanks, but that doesn't work.  It's giving me a 500 Server Error if I put any variation of that code in the line.

Without knowing the exact setup of your tables, it's hard to say, but I use this type of thing all the time, so I'm guessing I just don't know how your tables are setup.

Sorry I didn't understand your environment correctly.  If you share the schema files in your post, it may assist in giving you an answer that will work.

Hi Gary

The tables are set up in 2 different sections.  I hope this is the schema that is required?

1.  COURSE DETAIL - it's on this page where I would like to list the dates

<h1>Courses - Detail Page Viewer</h1>
Record Number: <?php echo htmlencode($coursesRecord['num']) ?><br/>
Course Name: <?php echo htmlencode($coursesRecord['title']) ?><br/>
Duration: <?php echo htmlencode($coursesRecord['duration']) ?><br/>
Location: <?php echo htmlencode($coursesRecord['location']) ?><br/>
Cost: <?php echo htmlencode($coursesRecord['cost']) ?><br/>
_link : <a href="<?php echo $coursesRecord['_link'] ?>"><?php echo $coursesRecord['_link'] ?></a><br/>

2.  CALENDAR LIST - this table has the dates for all of the courses.  The Course value below is a drop down pulled from Courses table above, using the title as the idenfier

<?php foreach ($calendarRecords as $record): ?>
Record Number: <?php echo htmlencode($record['num']) ?><br/>
Course (value): <?php echo $record['title'] ?><br/>
Course (label): <?php echo $record['title:label'] ?><br/>
Date: <?php echo date("D, M jS, Y g:i:s a", strtotime($record['date'])) ?><br/><!-- For date formatting codes see: http://www.php.net/date -->
_link : <a href="<?php echo $record['_link'] ?>"><?php echo $record['_link'] ?></a><br/>
<hr/>
<?php endforeach ?>

<?php if (!$calendarRecords): ?>
No records were found!<br/><br/>
<?php endif ?>

Thanks so much.

Carole

Hi Gary

Thanks for providing some further insight.  I finally got the code to work using:

// load records from 'calendar'
$whereValue = "title='{$coursesRecord['title']}'";

list($calendarRecords, $calendarMetaData) = getRecords(array(

'tableName' => 'calendar',
'where' => $whereValue,
'loadUploads' => true,
'allowSearch' => false,
));

The date / today field code doesn't work as it just lists everything, so I'll do this part manually.

Thanks again for your help.

Regards

Carole