Code Help

5 posts by 2 authors in: Forums > CMS Builder
Last Post: September 27, 2010   (RSS)

Hello, Folks -

I wonder if someone would be so good as to give me some much-needed advice. I'm trying to set-up a simple download counter for a client (which will be run alongside CMSB). The aim is to count the number of times a range of attachments are downloaded.

In order to set-up the download counter I need to input the list of files to be monitored. Typically these would be added manually using the following format:

$settings['allowedownloads'] = array(
"test.zip",
"another_file.pdf",
"download_me.pdf"
);


I want to replace this list (in red) with data pulled straight from CMSB. Sounds simple. The trouble is, although I am able to compile the list of files, somehow it isn't in the right format for the script.

I assumed it should be something like this...

<?php foreach ($itemRecords as $record): ?>
<?php foreach ($record['attachments'] as $attachment): ?>
<?php $files[] = $attachment['filename']; ?>
<?php endforeach ?>
<?php endforeach ?>

$settings['allowedownloads'] = $files;


I've tried a host of different things - including imploding and exploding the array/string - but it just won't work.

What am I doing wrong?!

:0S

Perch

Re: [Jason] Code Help

By Perchpole - September 27, 2010 - edited: September 27, 2010

Hi, Jason -

Thanks for your help with this. It's much appreciated.

I took your advice and discovered the code was generating the list correctly. This, however, only appeared to initiate a host other error messages - all regarding some kind of issue with the "header information".

What I discovered (by trial and error!) was that I'd made a mess of entering the CMSB code at the top of the script. I'd simply added the code as a block - which is standard practice with CMSB:

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

etc...

etc...

?>
<?php

Download Counter script here....

etc...

etc...

?>


In effect there are two blocks of code. What I should have done was combine the two into one...

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

etc...

etc...



Download Counter script....

etc...

etc...

?>


Now it works fine!

I've often encountered "header" errors before when working with CMSB so I really feel like I've learned something. I'm just not entirely sure what!

Perhaps you could explain why combining the blocks of code makes such a difference...

:0)

Perch

Re: [Perchpole] Code Help

By Jason - September 27, 2010

Hi Perch,

Some servers are a little more picky about header information than others. What was probably happening is there was a hidden character (probably a space or something) between the 2 php blocks. The server interpreted this as information that needs to be outputted to the browser. One you start outputting information to the screen,you can no longer change header information.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

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

Re: [Jason] Code Help

Once you start outputting information to the screen,you can no longer change header information.


A-ha! That's a valuable bit of info to commit to memory.

Thanks again for your help.

:0)

Perch