Link to home
Start Free TrialLog in
Avatar of Jazzy 1012
Jazzy 1012

asked on

Make the "or" only show before the LAST variable.

I have this code:
if(mysqli_num_rows($result_meal_option3_entree) > 1)
				{
					?>
					<p class="all_options">available with 
					<?php 
				while ($row_meal_option3_entree = mysqli_fetch_assoc($result_meal_option3_entree)) {
				    $c ++;
                    		$q_filters3_entree = "select * from main_filters where meal_options_id = '{$row_meal_option3_entree['id']}'";
                    		$result_filters3_entree = mysqli_query($conn,$q_filters3_entree);
                    		 $row_filterd3_entree= mysqli_fetch_assoc($result_filters3_entree);
                    		 ?>
                    		 <span class="deliveryItem1 protien_options_text all_options  
                    		 <?php 
                    		 
                    		 if($row_filterd3_entree["vegan"] == 'Y'){echo "vegan ";}
                    		 if($row_filterd3_entree["vegetarian"] == 'Y'){echo "vegetarian ";}
                    		 if($row_filterd3_entree["pescatarian"] == 'Y'){echo "pescetarian ";}
                    		 if($row_filterd3_entree["dairy-free"] == 'Y'){echo "dairy-free ";}
                    		 if($row_filterd3_entree["egg-free"] == 'Y'){echo "egg-free ";}
                    		 if($row_filterd3_entree["fish-free"] == 'Y'){echo "fish-free ";}
                    		 if($row_filterd3_entree["shellfish-free"] == 'Y'){echo "shellfish ";}
                    		 if($row_filterd3_entree["tree nut-free"] == 'Y'){echo "tree ";}
                    		 if($row_filterd3_entree["peanut-free"] == 'Y'){echo "peanut ";}
                    		 if($row_filterd3_entree["soybean-free"] == 'Y'){echo "soy ";}
                    		 if($row_filterd3_entree["low-total-fat"] == 'Y'){echo "total-fat ";}
                    		 if($row_filterd3_entree["low-saturated-fat"] == 'Y'){echo "saturated-fat ";}
                    		 if($row_filterd3_entree["low-cholesterol"] == 'Y'){echo "cholesterol ";}
                    		 if($row_filterd3_entree["low-sodium"] == 'Y'){echo "sodium ";}
                    		 if($row_filterd3_entree["protein(25g)"] == 'Y'){echo "protein ";}
                    		 if($row_filterd3_entree["calories(450)"] == 'Y'){echo "calories ";}
                    		 
                    		 ?>
                    		 
                    		 ">
                    		 <?php 
                    		 echo ($c>1 ? "or ":"").$row_meal_option3_entree['label']. "</span>";
			            } echo "</p>"; } ?>

Open in new window


this is my output:
User generated image
I want it to be Tofu, Veggie, Chicken or Beef.

or
Tofu or Veggie.

Currently it puts "or" after every label.
is there a way to fix this?
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also, this kind of coding (lists of strings) is often a lot easier to handle with arrays. For example:

$options = array();
$options[] = "tofu";
$options[] = "chicken";
$options[] = "dairy-free";

echo implode(", ", $options);  // Gives you "tofu, chicken, dairy-free" (so you don't have to deal with extra spaces or commas at the beginning or ends of the final strings.

$options = array();
$options[] = "tofu";

echo implode(", ", $options); // Gives you "tofu" (no delimiters added for a single option)


You could even use this approach to add the "or" to the final option in the array, like this:

$options = array();
$options[] = "tofu";
$options[] = "chicken";
$options[] = "dairy-free";

if(count($options) > 1)
{
  $last_option = array_pop($options);
  $options[] = "or " . $last_option;
}
echo implode(", ", $options); // Gives you "tofu, chicken, or dairy-free"

It just tends to be a little cleaner and more flexible to use arrays with implode()
Avatar of VanagaS
VanagaS

Its been long since I've worked with PHP, but here is something you can test:

replace the line:
echo ($c>1 ? ", ":"").$row_meal_option3_entree['label']. "</span>";[/code]

with the following and immediately close the while loop with "}" :

    /* echo ($c>1 ? ", ":"").$row_meal_option3_entree['label']. "</span>"; */

    /* The following will create a new array named labels and add each entry into it */
    $labels[] = $row_meal_option3_entree['label'];
}

Open in new window

### Out of the while loop add the following lines of code:
# the following will take out the last element from the array
$last = array_pop($labels);  

# the following will construct the required string
$string = count($labels) ? implode(", ", $labels) . " or " . $last : $last; 

# the following will echo the final string
echo "$string</span>";

Open in new window

### continue with rest of your code
echo "</p>";


## This is an untested code. Please double check before using it.
Vanagas, that's essentially what I suggested in my second comment already.