Link to home
Start Free TrialLog in
Avatar of Adam
AdamFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Convert my php code and divs to sit within a php echo statement - How?

Hi there.
I'm adapting a tutorial which uses ajax and php to pull information from my database, when  the user scrolls to the bottom of the page.

I noted however, that when I was reaching the bottom of the page, information wasn't always getting pulled from my database. I amended my code slightly to include my div tags within the php echo  (as per the original code) and the code now works (to an extent)  - it's pulling information as specified from the database.

So my working php code which displays the information on the page, and reloads is the following :

while($teacher = mysqli_fetch_array($data))
		{
			//here we are displaying the information from the database
			echo '
			<div class="container">
			<div class="col-sm-6 col-md-4 buffer">
			<div class="card" style="width: 100%;">
			
				<div class="card-header">
				<h1>'.$teacher['id'].'</h1>
				<img src="../public/img/flags/'.$teacher['nationality'].'flag.gif">
				<h2>'.$teacher['firstname'].'</h2>
				</div>
				
				<div class="card-body">
				<h1>'.$teacher['id'].'</h1>
				<h2>'.$teacher['firstname'].'</h2>
				</div>
			
			</div>
			</div>
			</div>
			';
		}

Open in new window


However, I want to display more information within the while.. The following code represents the information I want to display within the while loop and how it should appear. This code displays everything correctly on it's own but the pulling of the data does not seem to work with it.  

<div class="row">
                			  	   
				
							 <?php while($teacher = mysqli_fetch_array($result)) //Goes through the results and carries out the following action while (for every) row in the result set
							 { ?> 
																		
						 
						 
<!-- card--------------------------------------------------------------------------------------------------------->				
							 <div class="col-sm-6 col-md-4 buffer">
                    				<div class="card" style="width: 100%;">
        										<div class="card-header">
        										<img src="public/img/flags/<?php echo h ($teacher['nationality']) ?>flag.gif" alt="teacher nationality" />
                                                <a href = "<?php echo 'Profile.php?id=' . $teacher['id'];?>"><strong>&nbsp;&nbsp;&nbsp;<?php echo h($teacher['firstname']); ?></strong></a>&nbsp; - &nbsp;<?php echo h($teacher['language']); ?> teacher&nbsp;&nbsp;
												
                                                </div>
    									
                                          		<div class="card-body">
        											 <div align="center"> 
                									 <a href = "<?php echo 'Profile.php?id=' . $teacher['id'];?>"><img src="http://orangutanenglish.com/upload/<?php echo h($teacher['photoid']); ?>" class="ojborder " width="176" height="156" alt="" title="" /></a></div>
                        									  <div class="col-sm text-center">
                        									  <br />
                    									 	  	<?php
																if($teacher['teflconfirm'] == "yest")
																{print ("<IMG SRC =public/img/icons/yes_t_icon.gif>");}
																else  
																{print ("<IMG SRC =public/img/icons/no_t_icon.gif>");}
															   
																?>&nbsp;&nbsp;
                    											<?php
																if($teacher['degreeconfirm'] == "yesd")
																{print ("<IMG SRC =public/img/icons/yes_d_icon.gif>");}
																elseif ($teacher['degreeconfirm'] == "studying")  
																{print ("<IMG SRC =public/img/icons/studying_icon.gif>");}
																else  
																{print ("<IMG SRC =public/img/icons/no_d_icon.gif>");}
																?>&nbsp;&nbsp;
                    											<?php
																if($teacher['expconfirm'] == "expconfirm")
																{print ("<IMG SRC =public/img/icons/yes_exp_icon.gif>");}
																elseif ($teacher['expconfirm'] == "someexpconfirm")  
																{print ("<IMG SRC =public/img/icons/some_exp_icon.gif>");}
																else  
																{print ("<IMG SRC =public/img/icons/no_d_icon.gif>");}
																?>&nbsp;&nbsp;
                    									  		<?php
																if($teacher['vid'] == "1")
																{print ("<IMG SRC =public/img/icons/video1.gif>");}
																
																?>&nbsp;&nbsp;
																<br><br>
                                                                <h6 class="card-subtitle mb-2 text-muted"><div align="center">profile added: May 2017</div></h6>
                                                                <p class="card-text"><div align="center">Price per lesson: <?php echo h($teacher['private_price']); ?><br>Teaching Locations: <?php echo h($teacher['prefecture']); ?></div></p>
                                                       		  </div>
															
															<button type="button" data-toggle="popover" class="btn btn-success btn-sm" data-placement="top" data-content="Sorry - only members can contact teachers" data-original-title="Members Only" data-trigger="focus">Ask about availabilty&nbsp;&nbsp;<i class="fa fa-comment-o" aria-hidden="true"></i></button> 
															<button type="button" data-toggle="popover"  class="btn btn-info btn-sm"  data-placement="bottom"  data-content="Sorry - only members can add favourites" data-original-title="Members Only" data-trigger="focus">Favourite&nbsp;&nbsp;<i class="fa fa-thumbs-o-up" aria-hidden="true"></i></button> 

													</div>
                                        	  
                    				
                				  </div>
							</div>	
					 
					  <?php } ?> 
					 
<!-- card-------------------------------------------------------------------------------------------------------------> 				 
			
			

	
	</div>

Open in new window


I tried just adding this information in the while loop but for some reason (which I don't understand) the pulling of the records doesn't work. My thinking is that I have to adapt that code to sit within the php echo statement. I tried pasting it in, but my formatting of that code isn't working.

Am I thinking along the correct lines here, and if so, can anybody help or point me to a resource which shows how I can 'convert' my code to work within the php echo statement?

Hope that makes sense. It's all a tad jumbled in my head.

Many thanks for reading.
SOLUTION
Avatar of Swatantra Bhargava
Swatantra Bhargava
Flag of India 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
ASKER CERTIFIED SOLUTION
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
Avatar of Adam

ASKER

Hi Swatantra, Chris,

Thanks both for your comments and apologies for letting this post run dry a bit.

Taking what you both send into account I'm still struggling to get the code to pull fresh data from the sql table when I scroll to the bottom of the page. It seems to be inconsistent; sometimes pulls 20 records and stops, sometimes pulls 60 records and stops.

I'm not really sure what's going on so I think I'll shelf this idea and try to use pagination to get my records to display. If I can get that to work and the rest of the site working, I'll probably re-visit this method.

Many thanks.

Adam
Avatar of Adam

ASKER

Thanks both. Despite not resolving my refresh issues this time, I appreciate your  helpful suggestions.