I can't get my table to display on smaller screen resolutions

Hello,

I am trying to show different elements based on the size of the screen resolution. I am using the following commands. <div class="d-none d-sm-block"> and  <div class="d-block d-sm-none">.

These commands are working as I hoped, and the elements (tables) which I want to be displayed based on the screen resolution are being displayed correctly. However, when I try to add dynamic data to the tables using php, the table (in the smaller screen resolution) does not display. I do not understand why.

This is my code:

<?php
					  
					  $day   = isset($_POST['day'  ]) ? $_POST['day'  ] : null;
					  $month = isset($_POST['month']) ? $_POST['month'] : null;
					  $year  = isset($_POST['year'])  ? $_POST['year'] : null;
					  
					  
					  //$whereClause = [];
					  
					// if ($day) {
						 //$whereClause[] = 'day = "'. mysqli_real_escape_string($db_connection, $day) .'"';
					// }
					 
					// if ($month) {
						// $whereClause[] = 'month = "'. mysqli_real_escape_string($db_connection, $month) .'"';
					 //}
					 
					// if ($year) {
						 //$whereClause[] = 'year = "'. mysqli_real_escape_string($db_connection, $year) .'"';
					// }
					 
					 
										 
					 $sql = "SELECT * FROM songs_table WHERE day = '".$day."' AND month = '".$month."' AND year >= '".$year."'";
					 $result = mysqli_query($db_connection, $sql);
					 
					 if (mysqli_num_rows($result) > 0) {
						 
					 ?>
					 


					<br><br><br><br><br>
				
					<div class="d-none d-sm-block">
						
						Show me on everything but smallest screen.
						
						<table class="table table-dark">
								  <thead>
									<tr>
									<th scope="col"><h4>act</h4></th>
									<th scope="col"><h4>title</h4></th>
									<th scope="col"><h4>year</h4></th>
									<th scope="col"><h4>Youtube</h4></th>
									<th scope="col"><h4>Amazon</h4></th>
									</tr>
								  </thead>
								 
								 <tbody>
									<?php while($song = mysqli_fetch_array($result)) 
											{ 															//WE ARE USING A WHILE LOOP TO POPULATE THE TABLE
									?> 				
											<tr>
											<td><?php echo $song['artist']; ?></td>
											<td><?php echo $song['title']; ?></td>
											<td><?php echo $song['year']; ?></td>
											<td><iframe width="220" height="145" src="<?php echo $song['youtube'];?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></td>
											<td><a href = "<?php echo $song['amazon']; ?>"target="_blank" >Amazon<a/></td>
											</tr>
											</tbody>
									  <?php } ?>	
								 
								 
						</table>
						
						
					</div>
					<div class="d-block d-sm-none">
						Show me on smallest screen only.
						
						<table class="table">
						  <thead>
							<tr>
							  <th scope="col"><h4>BirthdaySong</h4></th>
							</tr>
						  </thead>
						  
						  <tbody>
						  
						  <?php while($song = mysqli_fetch_array($result)) 
											{ 															//WE ARE USING A WHILE LOOP TO POPULATE THE TABLE
									?> 
						  
							<tr>
							 <td><?php echo $song['artist']; ?> <br> 1976 <br> Youtube <br> Amazon</td>
							</tr>
							
							<tr>
							 
							  <td>Mark, Dance with somebody <br> 1976 <br> Youtube <br> Amazon</td>
							</tr>
							
							<tr>
							  
							  <td>Mark, Dance with somebody <br> 1976 <br> Youtube <br> Amazon</td>
							</tr>
							
						
							
						  </tbody>
						  
						   <?php } ?>
						</table>
						
						
					</div>
				
					
					 
				 <?php } ?>

Open in new window


To confirm, the table displays correctly on the large, medium and small resolutions but in the xtra small resolution, the dynamic table is not displaying. I noticed when I remove the line:

 <?php while($song = mysqli_fetch_array($result)) 
											{ 															
									?> 

Open in new window


... the table and the static text displays. When the line is then re-added the table disappears and no text appears.

Any advice on why this is happening and how to resolve would be great. Many thanks.
AdamTrying to learn phpAsked:
Who is Participating?
 
Chris StanyonConnect With a Mentor Commented:
When you call mysqli_fetch_array it moves the pointer on (so it can loop through the records). By the time you call it a second time on line 81, the pointer is already at the end of the set, so there are no more records to show, which is why your while loop doesn't run through a second time.

You need to reset the pointer before looping through the second time. Add this just before your second loop:

mysqli_data_seek($result, 0);
1
 
AdamTrying to learn phpAuthor Commented:
Wonderful. Many thanks Chris Stanyon.
0
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.

All Courses

From novice to tech pro — start learning today.