close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Best practices - PHP rtrim() on Looping Recordset

Thread began 6/15/2018 7:46 am by Steve | Last modified 12/11/2019 5:37 pm by Ray Borduin | 1792 views | 7 replies |

Steve

Best practices - PHP rtrim() on Looping Recordset

What would be the best means to trim the last comma from a looping recordset result?

One option is putting the looped results in an array and then using the php rtrim() function.
echo rtrim($r_results, ',');

Would this be the best way or are there other options?

Current Code:

// Multiple Markers
var markers = [
<?php
$wa_startindex = 0;
while(!$Locations->atEnd()) {
$wa_startindex = $Locations->Index;
?>
['<?php echo($Locations->getColumnVal("pageName")); ?>',<?php echo($Locations->getColumnVal("lat")); ?>,<?php echo($Locations->getColumnVal("lng")); ?>],
<?php
$Locations->moveNext();
}
$Locations->moveFirst(); //return RS to first record
unset($wa_startindex);
unset($wa_repeatcount);
?>
];



The goal being to get rid of that last comma after a group of bracketed results (......tions->getColumnVal("lng")); ?>],)

Sign in to reply to this post

Ray BorduinWebAssist

php:
// Multiple Markers

    var markers = [    
<?php
$wa_startindex 
0;
while(!
$Locations->atEnd()) {
  
$wa_startindex $Locations->Index;
  
$Locations->moveNext();
?>
        ['<?php echo($Locations->Results[$wa_startindex]["pageName"]); ?>',<?php echo($Locations->Results[$wa_startindex]["lat"]); ?>,<?php echo($Locations->Results[$wa_startindex]["lng"]); ?>]<?php echo($Locations->atEnd()?"":","); ?>    
<?php
}
$Locations->moveFirst(); //return RS to first record
unset($wa_startindex);
unset(
$wa_repeatcount);
?>    
            ];
Sign in to reply to this post
Did this help? Tips are appreciated...

Steve

What is this crazy magic you're sewing Ray? :-)


public function atEnd() {
return $this->Index == sizeof($this->Results);
}

Am I correct in seeing this function (atEnd) being used to determine the end of an iterated array?

Next a "Ternary Operator" / comparison is done between a comma and the replacement ""

https://en.wikipedia.org/wiki/%3F:

Thats pretty cool thanks!!

Maybe at some point, (when your not busy) it would be worth while to create a Cheat Sheet that has all the customer WA functions and how they work?

Sign in to reply to this post

Ray BorduinWebAssist

When I'm not busy ;)

Sign in to reply to this post
Did this help? Tips are appreciated...

Steve

Hello,
Any reason this would not work? Any changes to rsobj.php? I am using version 2.32

<?php
$wa_startindex = 0;
while(!$rsStates->atEnd()) {
$wa_startindex = $rsStates->Index;
?>
<?php echo($rsStates->getColumnVal("state_abv")); ?><?php echo($rsStates->atEnd()?"":","); ?>
<?php
$rsStates->moveNext();
}
$rsStates->moveFirst(); //return RS to first record
unset($wa_startindex);
unset($wa_repeatcount);
?>



I've checked the recordset count for any hidden values but that is not the case.

Sign in to reply to this post

Ray BorduinWebAssist

atEnd() won't be true until after the move next.

I think this code will do what you want:

php:
<?php

        $wa_startindex 
0;
        while(!
$rsStates->atEnd()) {
          
$wa_startindex $rsStates->Index;
        
?>
           <?php echo($rsStates->Index == 0?"":","); ?><?php echo($rsStates->getColumnVal("state_abv")); ?>
        <?php
          $rsStates
->moveNext();
        }
        
$rsStates->moveFirst(); //return RS to first record
        
unset($wa_startindex);
        unset(
$wa_repeatcount);
        
?>
Sign in to reply to this post
Did this help? Tips are appreciated...

Steve

Thanks Ray, that works well!

Not sure how the first record shown has a comma "after" it, when this logic is showing the conditional comma code "before" the echoing of "state_abv".

Sign in to reply to this post

Ray BorduinWebAssist

It skips the first one. That way it is effectively adding it after the one before if there is one before.

Sign in to reply to this post
Did this help? Tips are appreciated...

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...