Help wih fputcsv() issue
3 posts by 2 authors in: Forums > CMS Builder
Last Post: August 5, 2020 (RSS)
By gkornbluth - August 5, 2020
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
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
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,
Technical Lead
interactivetools.com
By gkornbluth - August 5, 2020 - edited: August 5, 2020
Hi Daniel,
Thanks for catching that. I don't think I ever would.
I tried a list of <?php $location=" " ?> and the same ideas for all the other variables after the
<?php foreach ($booksRecords as $record): ?>
on line 1067
And that worked!
Thanks as always,
Jerry Kornbluth
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php