Help wih fputcsv() issue

3 posts by 2 authors in: Forums > CMS Builder
Last Post: August 5, 2020   (RSS)

Hi All,

Back in January Daniel helped me with fputcsv() in this post: https://www.interactivetools.com/forum/forum-posts.php?postNum=2244435

I thought everything was working fine, but I’ve come up with a small problem that I’m hoping someone can help me with.

Up to now, when there was a pull down list field in a record in my table, there was always a value selected, and each row in the downloaded csv displayed its appropriate value.

I was asked to add some new pull down list fields, and I’ve discovered that unless a value is chosen for a particular record, the value from the last record with a value selected is repeated in each row in the resulting csv until a new value is encountered. The search results output does not show the same issue.

Hope that makes sense.

I’ve attached a screen shot of a sample search result output, the corresponding csv result and the complete viewer code. (the fputcsv code begins after line 1023)

Appreciate it if someone could take a look.

Thanks,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Attachments:

Search result.jpg 70K

csv.jpg 86K

spreadC.php 69K

By daniel - August 5, 2020

Hi Jerry,

If you check out the block of code starting around line 1479, this is where the values are being assigned to the csv row. For example:

if ($location_visible) { $csvLine[] = @$location; }

If you trace back where $location is being assigned, we find this block (lines 1081-1089):

              <?php if(($location_visible == 1)):?>
              <?php if($record['location'] ):?>
              <?php // Replace all commas with space and dash
					  $location = $record['location:label'];
					  $location = preg_replace("[,]", " -", $location); ?>
              <?php echo htmlencode($location)?>,
			    <?php else : ?>
              ,
              <?php endif ?> <?php endif ?>

As you can see, $location is only being assigned if the record has a location set. If the record doesn't have anything set for location, then the previously assigned value will remain unless the variable's value is explicitly reset (which it doesn't appear to be). I assume this is the case generally for the rest of the column values. You'll need to reset each of these variables at the top of the loop before they are to be assigned to make sure you're only using data relevant to the current record.

Let me know if this helps you out or if you have any further questions!

Thanks,

Daniel
Technical Lead
interactivetools.com