• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extra commas in my admin panel

#1
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 ?>
Reply

#2
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).
Reply

#3
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
Reply

#4
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.
Reply

#5
I am not a developer so I am having issues, sorry.
Reply

#6
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);
    
?>
Reply

#7
All fixed, thank you both for the help.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.