We help IT Professionals succeed at work.

Add - in a loop to first and last element.

Jazzy 1012
Jazzy 1012 asked
on
172 Views
Last Modified: 2017-04-19
<?php
				$counter_price = 0;
				if(mysqli_num_rows($result_meal_price_option) > 1)
			while ($row_meal_price_option = mysqli_fetch_assoc($result_meal_price_option)) {
				$counter_price ++;
				$q_price_filters = "select * from main_filters where meal_options_id = '{$row_meal_price_option['id']}'";
				$result_price_filters = mysqli_query($conn,$q_price_filters);
				$row_price_filterd= mysqli_fetch_assoc($result_price_filters);
						?>
						<span class= "deliveryItem1
							 <?php 
                    		 if($row_price_filterd["vegan"] == 'Y'){echo "vegan ";}
                    		 if($row_price_filterd["vegetarian"] == 'Y'){echo "vegetarian ";}
                    		 if($row_price_filterd["pescatarian"] == 'Y'){echo "pescetarian ";}
                    		 if($row_price_filterd["dairy-free"] == 'Y'){echo "dairy-free ";}
                    		 if($row_price_filterd["egg-free"] == 'Y'){echo "egg-free ";}
                    		 if($row_price_filterd["fish-free"] == 'Y'){echo "fish-free ";}
                    		 if($row_price_filterd["shellfish-free"] == 'Y'){echo "shellfish ";}
                    		 if($row_price_filterd["tree nut-free"] == 'Y'){echo "tree ";}
                    		 if($row_price_filterd["peanut-free"] == 'Y'){echo "peanut ";}
                    		 if($row_price_filterd["soybean-free"] == 'Y'){echo "soy ";}
                    		 if($row_price_filterd["low-total-fat"] == 'Y'){echo "total-fat ";}
                    		 if($row_price_filterd["low-saturated-fat"] == 'Y'){echo "saturated-fat ";}
                    		 if($row_price_filterd["low-cholesterol"] == 'Y'){echo "cholesterol ";}
                    		 if($row_price_filterd["low-sodium"] == 'Y'){echo "sodium ";}
                    		 if($row_price_filterd["protein(25g)"] == 'Y'){echo "protein ";}
                    		 if($row_price_filterd["calories(450)"] == 'Y'){echo "calories ";}
                    		 ?>
                    		 ">
                    		 <?php
                    		 $price_soup= $row_meal_price_option['price'];
                    		 $price_soup = number_format($price_soup/100, 2);
                    		 $price_soup = "$" . $price_soup;

                    		 
                    		 //echo($counter_price>1 ? "- ":""). $price_soup;
                    		 
                    		 //echo $price_soup;
                    		 ?>
						</span>
					
						
						<?php }
								else{
									print_r($price1);
								}
								?>

Open in new window


I have this code, how can I echo out that when I have 2 prices it echos out price1-price2, if I have 3 it echos out price1-price3, if 4 = price1-price4, so technically, price1 - the last price, how is this possible with my code?
Comment
Watch Question

Most Valuable Expert 2011
Author of the Year 2014

Commented:
Please show us a sample of the data, thanks.  Then we can show you how it might be handled.

Author

Commented:
It is like this in the image
see.png
I want it to be 18-23, then if it lamb gets filter, it becomes 18-20, so just the first and last element.
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Thanks, that's helpful.  Please show us a sample of the data.  You can use var_dump() to print out the data.  It will be easier to read if you use your browser's view source feature.  Just copy that and paste it into the code snippet here.

Commented:
greetings Jasmine Ikhreishi , , , , You already have the elements in this do do what you want, you can get the numRows  in the mysqli_num_rows($result_meal_price_option)  into a variable, and if your  $counter_price  is equal to the  numRows then echo out the
 last   $price_soup .
Here is some code, I could not test it, but the part that does the $price_soup testing is this
        if ($counter_price == 1) echo $price_soup;
            elseif($counter_price == $numRows) echo '-'.$price_soup;
				$counter_price = 0;
				$numRows = mysqli_num_rows($result_meal_price_option);
				if($numRows> 1)
			while ($row_meal_price_option = mysqli_fetch_assoc($result_meal_price_option)) {
				$counter_price ++;
				$q_price_filters = "select * from main_filters where meal_options_id = '{$row_meal_price_option['id']}'";
				$result_price_filters = mysqli_query($conn,$q_price_filters);
				$row_price_filterd= mysqli_fetch_assoc($result_price_filters);
						?>
						<span class= "deliveryItem1
							 <?php 
                    		 if($row_price_filterd["vegan"] == 'Y'){echo "vegan ";}
                    		 if($row_price_filterd["vegetarian"] == 'Y'){echo "vegetarian ";}
                    		 if($row_price_filterd["pescatarian"] == 'Y'){echo "pescetarian ";}
                    		 if($row_price_filterd["dairy-free"] == 'Y'){echo "dairy-free ";}
                    		 if($row_price_filterd["egg-free"] == 'Y'){echo "egg-free ";}
                    		 if($row_price_filterd["fish-free"] == 'Y'){echo "fish-free ";}
                    		 if($row_price_filterd["shellfish-free"] == 'Y'){echo "shellfish ";}
                    		 if($row_price_filterd["tree nut-free"] == 'Y'){echo "tree ";}
                    		 if($row_price_filterd["peanut-free"] == 'Y'){echo "peanut ";}
                    		 if($row_price_filterd["soybean-free"] == 'Y'){echo "soy ";}
                    		 if($row_price_filterd["low-total-fat"] == 'Y'){echo "total-fat ";}
                    		 if($row_price_filterd["low-saturated-fat"] == 'Y'){echo "saturated-fat ";}
                    		 if($row_price_filterd["low-cholesterol"] == 'Y'){echo "cholesterol ";}
                    		 if($row_price_filterd["low-sodium"] == 'Y'){echo "sodium ";}
                    		 if($row_price_filterd["protein(25g)"] == 'Y'){echo "protein ";}
                    		 if($row_price_filterd["calories(450)"] == 'Y'){echo "calories ";}
                    		 ?>
                    		 ">
                    		 <?php
                    		 $price_soup= $row_meal_price_option['price'];
                    		 $price_soup = number_format($price_soup/100, 2);
                    		 $price_soup = "$" . $price_soup;

                                if ($counter_price == 1) echo $price_soup;
                                  elseif($counter_price == $numRows) echo '-'.$price_soup;
                    		 ?>
						</span>
					
						
						<?php }
								else{
									print_r($price1);
								}
								?>

Open in new window

But I am not sure about the HTML containers you have? ? ? the <span> creation, may need to be Skipped altogether, if so you can use the while loop "continue" to just skip the output except for the first and last prices. . .

while ($row_meal_price_option = mysqli_fetch_assoc($result_meal_price_option)) {
     $counter_price ++;
     if (($counter_price > 1) && ($counter_price < $numRows)) continue;


also there may be an Error in code as you have these lines
<?php } // this bracket ends the WHILE LOOP, not an IF test
        else{ // maybe Error here the "else" does not seem to belong to a WHILE block ?
            print_r($price1);
            }

Author

Commented:
<div class="col col-md-9" id="display_menu" style="margin-left:-80px;">
  	<p class="orange OldStandard pull-left title_menu">Soups </p>  
  	  	<button  class=" disabled1   	  	
  	  	 white cbtn1 pull-right" type="submit" id="submit" onclick="window.location.href='../check-out'" >Checkout  <i class="fa fa-long-arrow-right"></i></button>
  
		
		  	
		<div class= "flex rowWrap foodDiv">
			
			<form method="get" action= "../menu-option" class= "flex">
				<span class="deliveryItem pointer vertical-top textCenter
				vegan vegetarian pescetarian dairy-free egg-free fish-free shellfish tree peanut soy total-fat saturated-fat cholesterol sodium calories ">
				<input type="hidden" name="meal_id" value="17">				 <img src="https://res.cloudinary.com/hazgel4lg/image/upload/c_scale,w_1200/v1466896899/tagine_00520_c6slsm.jpg" width="280px" height= "200px;" class="foodImage">
				<div class="menuDelivery">
				<h3 class= "textCenter OldStandard dark-orange font1"> Moroccan Tajine </h3>
				<p class= "OldStandardItalic text-black font2 vertical1"> with Herbed Quinoa </p>
				<hr>
				<span class="open_sanssemibold text-black">
										<span class= "deliveryItem1
							 vegan vegetarian pescetarian dairy-free egg-free fish-free shellfish tree peanut soy total-fat saturated-fat cholesterol sodium calories                     		 ">
                    		 $18.00						</span>
					
						
												<span class= "deliveryItem1
							 dairy-free egg-free fish-free shellfish tree peanut soy total-fat saturated-fat cholesterol sodium calories                     		 ">
                    		 - $20.00						</span>
					
						
												<span class= "deliveryItem1
							 dairy-free egg-free fish-free shellfish tree peanut soy saturated-fat sodium protein                     		 ">
                    		 - $23.00						</span>
					
						
												
				
				<br>
				</span>
				<span class="text-black font2">
				                    		 
                    		 <span class="deliveryItem1 
                    		 vegan vegetarian pescetarian dairy-free egg-free fish-free shellfish tree peanut soy total-fat saturated-fat cholesterol sodium calories                     		 
                    		 ">
                    		 Veggie</span>                    		 
                    		 <span class="deliveryItem1 
                    		 dairy-free egg-free fish-free shellfish tree peanut soy total-fat saturated-fat cholesterol sodium calories                     		 
                    		 ">
                    		 | Chicken</span>                    		 
                    		 <span class="deliveryItem1 
                    		 dairy-free egg-free fish-free shellfish tree peanut soy saturated-fat sodium protein                     		 
                    		 ">
                    		 | Lamb</span>				
				<br>
				</span>
				<button type="submit" class="view white cbtn1 open_sansbold check_menu">View/Add</button>
				</div>
				</span>
				</form>

Open in new window


here is the output, what javascript code can I use to take the first price - lastprice, and even to change if it is filtered
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.