Filtering Records To Be Displayed With PHP Include

25 posts by 4 authors in: Forums > CMS Builder
Last Post: May 18, 2016   (RSS)

Hi Guys,

Hoping to get a little guidance with the below idea.

I created the below page, it has a php include statement that's pulling the additional yoga classes(records) at the bottom of the page from my CMS.

http://www.yogamerge.com/classpages/nikkanadia/restorative-flow-test.php

I just need to filter them so that it doesn't display all records. In this case, I would want to display all records for instructor Nikka Nadia. I would link to those records with this link, http://www.yogamerge.com/online-yoga-classes.php?categories_keyword=nikka-nadia

How would I setup the below php to only display the records for Nikka Nadia?

I spoke briefly with Ross about this, I seem to need to use a php request statement somehow, $_request

Thanks much, any help would be appreciated!

Kent

Here's the page code that my include statement displays on restorative-flow-test.php to show the list of yoga classes, it currently displays every record -

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
/* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */

// load viewer library
$libraryPath = 'cmsb/lib/viewer_functions.php';
$dirsToCheck = array('/web/sites/greywood/yogamerge.com/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

// load records from 'all_classes'
list($all_classesRecords, $all_classesMetaData) = getRecords(array(
'tableName' => 'all_classes',
'perPage' => '10',
'loadUploads' => true,
'allowSearch' => true,
));

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
</style>


<TITLE></TITLE>

<script language="JavaScript">
<!--//BEGIN Script
function new_window(url) {

link = window.open(url,"Link","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=750,height=675,left=80,top=180");

}
//END Script-->
</script>


</head>

<body font face="Verdana, Arial, Helvetica, sans-serif" size="2" text="#000000" link="#000000">

<?php foreach ($all_classesRecords as $record): ?>


<table width="950" table cellspacing="5" valign="TOP" nowrap>

<tr align="CENTER" bgcolor="#000000" link="#FFFFFF" vlink="#FFFFFF">

<Td><font color="white" font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Class</b></Td><td width="950" colspan="3"><b><font color="white" font face="Verdana, Arial, Helvetica, sans-serif" size="2"><?php echo htmlencode($record['title']) ?></b></td></a>

<TD><font color="white" font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b><?php echo htmlencode($record['download_price']) ?></b></TD></tr>

<tr>

<td rowspan="4" align="CENTER" valign="TOP">

<a href="<?php echo htmlencode($record['preview_link']) ?>"><?php echo $record['thumb']; ?><font face="Verdana, Arial, Helvetica, sans-serif" font size="2" font color="000000" link="#FFFFFF" vlink="#FFFFFF"><B>PREVIEW</B></a><P>
</TD>

</TD></tr>

<tr>

<td nowrap><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Style: </b><?php echo htmlencode($record['style']) ?></td>

<td nowrap>

</td>

<td nowrap><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Length: </b><?php echo htmlencode($record['class_length']) ?></td>

<td nowrap> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Media: </b><?php echo htmlencode($record['media']) ?></TD>

</tr>

</TR>

<tr>

<td colspan="3" class="desc" width="400"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Instructor: </b><?php echo htmlencode($record['instructor']) ?></TD>
<TD noWrap><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>HD: </b><?php echo htmlencode($record['format']) ?></TD></TR>

<tr><td width="1100" colspan="3" class="desc"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Level: </b><?php echo htmlencode($record['class_level']) ?>
<?php echo htmlencode($record['description']) ?><P>


</td>

<td align="center">

<?php echo $record['add_to_cart']; ?><P>


</tr>

</table>



<?php endforeach ?>

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

<br>
<br>

</body>
</html>

By ross - May 5, 2016

Hi Kent

Thanks for posting.

The first thing is you'll need to find the line in "restorative-flow-test.php" where you include the list of classes.

Right before that line, create a variable like this:

$category = "nikka-nadia"; // name of category to load
include("THIS IS WHERE YOUR INCLUDE WOULD BE");

Next, on the page you are including, in this case, online-yoga-classes.php,  go to where you "load records" and add in the following custom "where"

// load records from 'all_classes'list($all_classesRecords, $all_classesMetaData) = getRecords(array(
     'tableName' => 'all_classes',
     'perPage' => '10',
     'loadUploads' => true,
     'allowSearch' => false,
     'where'       => "categories = '". mysql_escape($category) ."'",
));

See how you set $category on the main page and then use it in the include file?

Give that a shot and let me know how you make out.

If you run into anything setting this up, could you post both pages (the main page and the one your are including) as attachments (ie: not just copying and pasting the code into a reply). That will help me read your code.

Let me know any questions or feedback.

Thanks!

-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/

Hi Ross,

Update:

I changed the code in the where statement, it looked like there might be a typo with this:

'where'       => "categories = '". mysql_escape($category) ."",

I changed it to:

'where'       => "categories = "". mysql_escape($category) ."", 

I no longer get the error, however I get no classes at all displayed via the include statement on restorative-flow-test.php

Thanks much.

By Damon - May 6, 2016

Hi,

It looks like the where statement is missing both of the single quotes.

Copy and paste this as your where statement:

'where'       => "categories = '". mysql_escape($category) ."'",

Let us know if that works for you.

Thanks!

Cheers,
Damon Edis - interactivetools.com

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

Thanks Damon,

I now get the following error after making the changes.

Notice: Undefined variable: category in /web/sites/greywood/yogamerge.com/includes/nikka-nadia-classes.php on line 17 No records were found!

Thanks much,

Kent

By gregThomas - May 9, 2016

Hey Kent, 

This error means that PHP can't find the variable $category. This is probably because this line:

$category = "nikka-nadia"; // name of category to load 

isn't currently set in restorative-flow-test.php as it was in your previous post. If you add this to the code base, it should work. 

Cheers,

Greg

Greg Thomas







PHP Programmer - interactivetools.com

Hi Greg,

Thanks for the response!

I do have that line in the code on restorative-flow-test.php

I have actually tried it two different ways shown below:

Right now it's set as this:

<?php $category = "nikka-nadia"; // name of category to load ?>
       <?php include("http://www.yogamerge.com/includes/nikka-nadia-classes.php");?>

 Before it was set as this:

<?php $category = "nikka-nadia"; // name of category to load 
      include("http://www.yogamerge.com/includes/nikka-nadia-classes.php");?>

I wasn't sure if the category variable should be in the same php statement as the include?

I get the error either way. I attached my current restorative-flow-test.php page to this reply.

Attachments:

restorative-flow-test.php 16K

By gregThomas - May 9, 2016

Hey Kent, 

I think I know what the issue is, if you change these lines:

<?php $category = "nikka-nadia"; // name of category to load ?>
<?php include("http://www.yogamerge.com/includes/nikka-nadia-classes.php");?>

to this:

<?php
  $category = "nikka-nadia"; // name of category to load
  include("includes/nikka-nadia-classes.php");
?>

the code should work. The problems is the current include is using a web request to get the contents of the nikka-nadia-classes.php file, and this doesn't let the file access the current pages variables. 

But if you include the file using includes/nikka-nadia-classes.php then PHP will include the file directly from the server, and the includes/nikka-nadia-classes.php file can access variables from the current file. 

I hope that makes sense! Let me know if you have any questions. 

Cheers

Greg

Greg Thomas







PHP Programmer - interactivetools.com

Hi Greg,

I made your change which makes sense to me, but unfortunately I still get no records displayed. I no longer have the error saying no records found, but there's no records displayed on the page, you can see it using the below link:

http://www.yogamerge.com/classpages/nikkanadia/restorative-flow-test.php

So far it only works when I take it back to displaying all records with no attempt to filter the records with a category.

Thanks for all the help!

Kent