CodeIgniter Forums
Extra commas in my admin panel - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Extra commas in my admin panel (/showthread.php?tid=66435)



Extra commas in my admin panel - flaboi - 10-21-2016

Hello,

When I view an order and look at the accessories section, it will always have an extra comma at the end:

Charger, Box,

Instead of:

Charger, Box

This is what my code is, please let me know what I did wrong.

Thank you!  Cool

Code:
<?php if($accepted_offer->accessories != ''): ?>
                        <div class="item">
                            <b>Accessories:</b>
                            <?php foreach (explode(',', $accepted_offer->accessories) as $accessory): ?>
                                <?= htmlspecialchars(isset($accessories[$accessory])?$accessories[$accessory]->title:'Deleted') ?>,
                            <?php endforeach ?>



RE: Extra commas in my admin panel - JayAdra - 10-21-2016

It's because that's what you've put in the code:

PHP Code:
<?= htmlspecialchars(isset($accessories[$accessory])?$accessories[$accessory]->title:'Deleted'?>

That comma on the end. What you want to do is either build your string in that loop and then remove the last character from the string (which you know is going to be a comma), or have a conditional statement which doesn't print the comma if this is the last item in the array (I would recommend changing to a for loop if doing it this way).


RE: Extra commas in my admin panel - flaboi - 10-21-2016

Hi Jay,

I am not a developer, just trying to fix bugs made by one who did a job for me and will no longer support me. I tried to remove that comma at the end but it just broke the page.  Huh


RE: Extra commas in my admin panel - JayAdra - 10-21-2016

You mean you just removed that comma from the code? Or you tried to remove it programmatically using substr or similar?

Looking at it again, it seems like the whole process is unnecessary? Look at this part of the code:

PHP Code:
explode(','$accepted_offer->accessories

This is presumably taking a comma separated string of accessories and turning it into an array, which it then loops over to echo out as a comma separated string? I don't know why they would have set it up this way?

It seems to me like you could just echo out $accepted_offer->accessories and not bother with the loop at all. Unless the logic with the "Deleted" title factors into it, but not sure what that's doing anyway.

So either remove the loop and just echo the initial string, or re-read my other two initial suggestions and try those.


RE: Extra commas in my admin panel - flaboi - 10-21-2016

I am not a developer so I am having issues, sorry.


RE: Extra commas in my admin panel - Wouter60 - 10-22-2016

Replace the code with:

PHP Code:
<?php if($accepted_offer->accessories != ''): ?>
  <div class="item">
    <b>Accessories:</b>
    <?php
      $titles 
= array();
     
 foreach (explode(','$accepted_offer->accessories) as $accessory) {
        if (isset(
$accessories[$accessory])) {
         
  $titles[] = $accessories[$accessory]->title;
        } 
 
        
else {
         
  $titles[] = 'Deleted';    
        } 
 
      
}
     
 echo implode(',' $titles);
    
?>



RE: Extra commas in my admin panel - flaboi - 10-26-2016

All fixed, thank you both for the help.