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:
see1.png
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?
LVL 1
Jazzy 1012Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gr8gonzoConsultantCommented:
Sure. Take the following steps:
1. Keep the number of rows in a variable, so change:
if(mysqli_num_rows($result_meal_option3_entree) > 1)

Open in new window

to:
$total_rows = mysqli_num_rows($result_meal_option3_entree);
if($total_rows > 1)

Open in new window


2. Make sure you reset $c before your loop, so change:
while ($row_meal_option3_entree = mysqli_fetch_assoc($result_meal_option3_entree)) {
				    $c ++;

Open in new window

to:
$c = 0;
while ($row_meal_option3_entree = mysqli_fetch_assoc($result_meal_option3_entree)) {
				    $c ++;

Open in new window


3. Change the delimiter based on whether $c is the last row or not, so change:
echo ($c>1 ? "or ":"").$row_meal_option3_entree['label']. "</span>";

Open in new window

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

Open in new window


(There's a chance it might need to be $c == ($total_rows - 1) but I can't test at the moment)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gr8gonzoConsultantCommented:
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()
0
VanagaSCommented:
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.
0
gr8gonzoConsultantCommented:
Vanagas, that's essentially what I suggested in my second comment already.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.