Identifier list inside a list

8 posts by 2 authors in: Forums > CMS Builder
Last Post: September 30, 2014   (RSS)

By Mohaukla - September 21, 2014 - edited: September 22, 2014

I have a list "resource_documents" of uploaded documents that I added another list "resource_type"  inside it to identify what type of document it is.

I need to make a list of each depending on the type of document it is.

So here is the list of types (lable = title)

Case Study 
White Paper
Newsletter
Form
Webinar
Recorded Webinar
Sample Product Report

So if I need a list of documents that are just Case Studies or whatever.... what would that look like?

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

if resource_type:label = Case Study 

<?php endforeach ?>

Im not sure if it needs an if statement or if that can be worked out inside the foreach statement

Thanks in advance

And just an FYI this will lead into a similar followup question.

Michael Moyers



Owner of Just Rite Design Inc. A growing network of professionals in web design, graphic design, flash development, programming, and audio & video productions.



"Due to budget constraints, the Light at the end of the tunnel will be temporarily out!"

Hi Michael

This depends on whether you need all the records in that section and you're just sorting them, or whether you just want Case Studies.

Getting them all and then sorting them would look something like this:

list($resourceDocsRecords, $resourceDocsRecordsMeta) = getRecords(array(
    'tableName' => 'resource_documents',
    'allowSearch' => false
));

$resources = array();

foreach ($resourceDocsRecords as $doc) {
    $resources[$doc['resource_type:label']][] = $doc;
}

So this gives you a two level array of the records, sorted by their type, then you output them like so:

<?php foreach ($resources as $type => $record): ?>
    <?php if($type == 'Case Study') : ?>
        <h1 class="casestudy"><?php echo $record['title']; ?></h1>
        <?php echo $record['description']; ?>
        ... echo more record stuff for case studies here...
    <?php elseif ($type == 'White Paper) : ?>
        <h1 class="whitepaper"><?php echo $record['title']; ?></h1>
        <?php echo $record['description']; ?>
        ... echo more record stuff for white papers here...
    <?php else : ?>
        <h1 class="default"><?php echo $record['title']; ?></h1>
        <?php echo $record['description']; ?>
        ... echo more default stuff here...
    <?php endif; ?>
<?php endforeach ?>

Hopefully you get the idea. Just make sure you change the variable names - I've inserted title and description here but I assume you have more than that.

If you just want a list of Case Studies, then all you need is an extra parameter added to the getRecords function, like so:

list($resourceDocsRecords, $resourceDocsRecordsMeta) = getRecords(array(
    'tableName' => 'resource_documents',
    'allowSearch' => false,
    'where' => 'resource_type = "Case Study"' // insert the VALUE, not the label, of the resource type you want here.
));

Does this all make sense?

--------------------

Claire Ryan
interactivetools.com

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

That is great thanks! Based on what you have shown me I understand a little better.

My next question would be how to come from a category page to a specific type list page

If I make a foreach loop to create a list of documents by types.

So I would start on the category page (lets say "resources.php") with:

  list($resource_typeRecords, $resource_typeMetaData) = getRecords(array(
    'tableName'   => 'resource_type',
    'loadUploads' => false,
    'allowSearch' => false,
  ));
$type = mysql_select ('resource_type');

And then show the categories in columns: 

  <section class='white trans' id='content'>
    <?php foreach ($resource_typeRecords as $record): ?>
      <div class='col-md-12 col-md-offset-0'>
        <div class='col-md-3 service'>
          <h3>
            <span class='fa fa-search'></span>
            <?php echo htmlencode($record['title']) ?>
          </h3>
          <p class='small'><?php echo $record['content']; ?></p>
          <a class='btn btn-default btn-md'>
            <span class='fa fa-caret-right'></span>
            <a href="categorytype.php?type=<?php echo $type['title'];?>">Read more</a>
          </a>
        </div>
      </div>
   <?php endforeach ?>
  </section>

SOOO

If I want the link to be built to go to a single category list page (I suppose I would NOT set that up in the "viewer links tab" ) using the "title" in the link (example categorytype.php?type=)

And then what the code look like on the receiving list page (categorytype.php) to understand that we are looking for a list of resources based on the type in the link

I am only guessing  on this

  list($resource_documentsRecords, $resource_documentsMetaData) = getRecords(array(
    'tableName'   => 'resource_documents',
    'loadUploads' => false,
    'allowSearch' => true,
  ));

I think I am close on some but not everything.

Thanks for your help

Michael Moyers



Owner of Just Rite Design Inc. A growing network of professionals in web design, graphic design, flash development, programming, and audio & video productions.



"Due to budget constraints, the Light at the end of the tunnel will be temporarily out!"

Hi Michael

This line here:

<a href="categorytype.php?type=<?php echo $type['title'];?>">Read more</a>

This isn't going to get you anything. The title is likely a string with spaces and whatnot, so it's not going to produce a valid link. Change it like so:

<a href="categorytype.php?type=<?php echo $type['num'];?>">Read more</a>

Then on the categorytype.php page, change getRecords like so:

list($resource_documentsRecords, $resource_documentsMetaData) = getRecords(array(
    'tableName'   => 'resource_documents',
    'loadUploads' => false,
    'where'       => 'type = '.mysql_escape(@$_REQUEST['type']),
    'allowSearch' => true,
  ));

I'm assuming that 'type' is the right fieldname, but if not, change it to whatever it should be. What you need to pass in the link is whatever is stored in the documents table that identifies the particular records you want to show.

--------------------

Claire Ryan
interactivetools.com

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

By Mohaukla - September 27, 2014 - edited: September 27, 2014

It is coming together but an error has occurred in the link

Header:

  list($resources_pageRecords, $resources_pageMetaData) = getRecords(array(
    'tableName'   => 'resources_page',
    'where'       => '', // load first record
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $resources_pageRecord = @$resources_pageRecords[0]; // get first record
  if (!$resources_pageRecord) { dieWith404("Record not found!"); } // show error message if no record found
  
    list($resource_typeRecords, $resource_typeMetaData) = getRecords(array(
    'tableName'   => 'resource_type',
    'loadUploads' => false,
    'allowSearch' => false,
  ));
  $type = mysql_select ('resource_type');

In the page I put:

<?php foreach ($resource_typeRecords as $record): ?>
        <div class='col-md-3 service'>
          <h3>
            <span class='fa fa-<?php echo htmlencode($record['resource_type_icon']) ?>'></span>
            <?php echo htmlencode($record['title']) ?>
          </h3>
          <p class='small'><?php echo $record['content']; ?></p>
          <a class='btn btn-default btn-md' href="categorytype.php?type=<?php echo $type['num'];?>">
            <span class='fa fa-caret-right'></span>
             Read more
          </a>
        </div>
      <?php endforeach ?>

On the page when I rollover the link I can see the link itself has an error saying:

categorytype.php?type=Notice:%20Undefined%20index:%20num%20in%20/data/13/0/142/86/142086/user/143888/htdocs/newsite/pages/resources.php%20on%20line%20109

What could be causing it not to grab the "num"?

Thanks

Michael Moyers



Owner of Just Rite Design Inc. A growing network of professionals in web design, graphic design, flash development, programming, and audio & video productions.



"Due to budget constraints, the Light at the end of the tunnel will be temporarily out!"

Hi Michael

You'll have to see if the $type includes what you think it does just as that echo statement is being processed.

Just before that line, enter this:

<?php showme($type); ?>

Take a look at the output on the page.

--------------------

Claire Ryan
interactivetools.com

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

Hi Michael

I know it looks intimidating but this makes perfect sense - your $type variable is an array of arrays, not a single array. I'm not sure if you need it, assuming the $record holds some reference to the type's num.

How about this:

<?php foreach ($resource_typeRecords as $record): ?>
        <div class='col-md-3 service'>
          <h3>
            <span class='fa fa-<?php echo htmlencode($record['resource_type_icon']) ?>'></span>
            <?php echo htmlencode($record['title']) ?>
          </h3>
          <p class='small'><?php echo strip_tags($record['content']); ?></p>
          <a class='btn btn-default btn-md' href="categorytype.php?type=<?php echo $record['type'];?>">
            <span class='fa fa-caret-right'></span>
             Read more
          </a>
        </div>
      <?php endforeach ?>

--------------------

Claire Ryan
interactivetools.com

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