Solved

PHP loop not working

Posted on 2016-11-18
4
61 Views
Last Modified: 2016-11-22
I have a modal that is inside of a loop.
And i am trying to make a new loop inside of the loop.

I am trying to loop this
Lets call this Color loop

$get_color = "SELECT * FROM gallery2 WHERE number_='$number'"; // i want to get the data from $number that comes from the button from another function.
$run_color = mysqli_query($conn,$get_color);
while ($rows = mysqli_fetch_assoc($run_color)){
	$id=$rows['id']	;
	$color=$rows['image'] == '' ? '' : ' <div class="col-sm-3 col-md-3">

	<div class="thumbnail" >
		<h4 class="text-center"><span class="label label-info center-block" style="background-color:#CF000F;">Liso '.$number.'</span></h4>
		<a id="carousel-selector-0" ><img src="../'.$rows['image'].'" ></a>
                    <div class="caption">
                    <div class="row">
		<button type="button" class="btn btn-primary actions add btn-block " data-action="add"   product_id="'.$id.'"  ><i class="fa fa-cart-plus" aria-hidden="true"></i></span> Add</button>
		     </div>
		     </div>
                </div>
	       </div>';


			}

Open in new window


The problem is that my modal is already in a loop
Modal loop

$sql = "SELECT * FROM gallery2 WHERE liso = 1 ";
			$run = mysqli_query($conn,$sql);
			while ($rows = mysqli_fetch_assoc($run)){
			 $vari = $rows['variante'];
			 $num = $rows['title'];
			 $isLiso = $rows['liso'];

			 $id=$rows['id'];
			 if($vari< 1){
			        $dash="";
			         $vari="";
			        }else{
			          $dash="-";
			         $vari = $rows['variante'];
			        }
                               $dashcol="-";
				
				$variI = $rows['variante'];
				$number=$num.$dashcol.$variI;
				
				echo '<div class="modal fade" id="lisos'.$number.'" role="dialog">

<div class="modal-dialog modal-xl">
<div class="modal-content">
<div class="modal-header">

<div class="btn-group pull-right">
<button type="button" class="btn btn-default pull-right" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<h3 class="modal-title">'.$num.''.$dash.''.$vari.'</h3>
</div>
<div class="modal-body">
		
            <div class="col-md-12"> 

			 '.$color.' //////////Need to loop here the Color loop

            </div>
			
     
 <div class="clearfix"></div>
</div>
</div><!-- /.modal-content -->
						
</div><!-- /.modal-dialog--->
    	
</div><!-- /.modal ending  -->

';} 

Open in new window


Another problem is that both loops are in one function called function Modal_Color().

So its like this ...
function Modal_Color{
///Color loop

///Modal loop

}

And what fires the modal is a buttons from another function.
What i am trying to do is display multiple products in the modal that contains the data from the Color Loop.
You will see that $number is not defined in the query in Color loop.
Only inside of the modal loop.
So all this is not working.

How can i loop the Color loop inside of the modal loop?
0
Comment
Question by:James Allan
  • 2
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
Chris Harte earned 500 total points
ID: 41893162
This is how to nest the colour loop inside the modal loop

$sql = "SELECT * FROM gallery2 WHERE liso = 1 ";
$run = mysqli_query($conn, $sql);

while ($rows = mysqli_fetch_assoc($run))
{
    $vari = $rows['variante'];
    $num = $rows['title'];
    $isLiso = $rows['liso'];

    $id = $rows['id'];
     
    if($vari < 1)
    {
        $dash = "";
        $vari = "";
    }
    else
    {
        $dash="-";
        $vari = $rows['variante'];
    }

    $dashcol = "-";
    $variI = $rows['variante'];
    $number = $num.$dashcol.$variI;

    echo '<div class="modal fade" id="lisos'.$number.'" role="dialog">

<div class="modal-dialog modal-xl">
<div class="modal-content">
<div class="modal-header">

<div class="btn-group pull-right">
<button type="button" class="btn btn-default pull-right" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<h3 class="modal-title">'.$num.''.$dash.''.$vari.'</h3>
</div>
<div class="modal-body">

            <div class="col-md-12">';
    
    $get_color = "SELECT * FROM gallery2 WHERE number_= $number ";
    $run_color = mysqli_query($conn, $get_color);
    
    while ($crows = mysqli_fetch_assoc($run_color))
    {
        $id = $crows['id']	;
        $color = $crows['image'] == '' ? '' : ' <div class="col-sm-3 col-md-3">

	<div class="thumbnail" >
		<h4 class="text-center"><span class="label label-info center-block" style="background-color:#CF000F;">Liso '.$number.'</span></h4>
		<a id="carousel-selector-0" ><img src="../'.$crows['image'].'" ></a>
                    <div class="caption">
                    <div class="row">
		<button type="button" class="btn btn-primary actions add btn-block " data-action="add"   product_id="'.$id.'"  ><i class="fa fa-cart-plus" aria-hidden="true"></i></span> Add</button>
		     </div>
		     </div>
                </div>
	       </div>';

    }               

    echo $color.'</div>		   
 <div class="clearfix"></div>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog--->  
</div><!-- /.modal ending  -->';
}

Open in new window

1
 
LVL 1

Author Comment

by:James Allan
ID: 41893414
Great no errors!
But inside of my modal i am still getting only  one product,i checked in my DB i have 2 products for test inside of the modal.
The modal loop for each product in my gallery.
And each product have a sub product inside of the modal.
E.g

My cushion 1 have 3 colors on it drawing,(black,pink and red) so the color modal will load the cushions that has the same title.
exempleIts working but its looping one only.
modal
For this modal its suppose to be 2 products inside of it.
0
 
LVL 17

Expert Comment

by:Chris Harte
ID: 41897520
Oops! You have to save the results in an array. Use square brackets.
    while ($crows = mysqli_fetch_assoc($run_color))
    {
        $id[] = $crows['id']	;
        $color[] = $crows['image'] == '' ? '' : ' <div class="col-sm-3 col-md-3">

	<div class="thumbnail" >
		<h4 class="text-center"><span class="label label-info center-block" style="background-color:#CF000F;">Liso '.$number.'</span></h4>
		<a id="carousel-selector-0" ><img src="../'.$crows['image'].'" ></a>
                    <div class="caption">
                    <div class="row">
		<button type="button" class="btn btn-primary actions add btn-block " data-action="add"   product_id="'.$id[].'"  ><i class="fa fa-cart-plus" aria-hidden="true"></i></span> Add</button>
		     </div>
		     </div>
                </div>
	       </div>';

    }

Open in new window

and you may need to use a foreach loop to display them as well.
0
 
LVL 1

Author Comment

by:James Allan
ID: 41897975
Your first solution did resolve my errors.
But it was looping only one product.
What i did was change a bit.

   echo '<div class="modal fade" id="lisos'.$number.'" role="dialog">

<div class="modal-dialog modal-xl">
<div class="modal-content">
<div class="modal-header">

<div class="btn-group pull-right">
<button type="button" class="btn btn-default pull-right" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<h3 class="modal-title">'.$num.''.$dash.''.$vari.'</h3>
</div>
<div class="modal-body">

            <div class="col-md-12">';
    
    $get_color = "SELECT * FROM gallery2 WHERE number_= '$number' ";
    $run_color = mysqli_query($conn, $get_color);
    
    while ($crows = mysqli_fetch_assoc($run_color))
    {
        $id = $crows['id']	;
  ////////// //changed here to echo directly->    echo $crows['image'] == '' ? '' : ' <div class="col-sm-3 col-md-3">

	<div class="thumbnail" >
		<h4 class="text-center"><span class="label label-info center-block" style="background-color:#CF000F;">Liso '.$number.'</span></h4>
		<a id="carousel-selector-0" ><img src="../'.$crows['image'].'" ></a>
                    <div class="caption">
                    <div class="row">
		<button type="button" class="btn btn-primary actions add btn-block " data-action="add"   product_id="'.$id.'"  ><i class="fa fa-cart-plus" aria-hidden="true"></i></span> Add</button>
		     </div>
		     </div>
                </div>
	       </div>';

         }        

   echo '</div>	//changed here to echo this rest of divs only.	   
 <div class="clearfix"></div>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog--->  
</div><!-- /.modal ending  -->';

}

Open in new window


look the result
result
A big thx mate!
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now