Jump to content

limit php results to 2 per row


Go to solution Solved by cyberRobot,

Recommended Posts

I have the following page:

<table width="80%" border="0" align="center" cellpadding="2" cellspacing="2">
      <tr>
        <td width="40%" valign="top">
         <div align="right"><a href="settings.php"><img src="img/buttons/main_settings.png" alt="" border="0" /></a><img src="img/spacer.png" alt="" width="10" height="1" /><a href="addtank.php"><img src="img/buttons/add_tank.png" alt="" border="0" /></a><img src="img/spacer.png" alt="" width="10" height="1" /></div>
        <div class="content-box column-left">
		<div class="content-box-header">
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="80%"><h3>My Tanks</h3></td>
  </tr>
</table>
		  </div>

                <div class="content-box-content">
				  <div>
                  
				    <table width="100%" border="0" cellspacing="0" cellpadding="4">
                            <tr>
                                                            <?php
 // Query to pull information from the "tanks" Database
$result = mysql_query("select * from tanks WHERE member_id=$_SESSION[SESS_MEMBER_ID] order by id DESC");
while ($row = mysql_fetch_object($result)) {

	$crypto = new phpFreaksCrypto(); 
    $id_to_be_encrypted = $row->id; 
    $id_encrypted = $crypto->encrypt($id_to_be_encrypted); 
    $id_decrypted = $crypto->decrypt($id_encrypted); 
    $dateNew = date('jS F, Y', strtotime($row->date));
	
	$encoded = urlencode($id_encrypted)
    
	
?><td width="50%" colspan="2" valign="top">
  
<table border="0" cellspacing="6" cellpadding="4">           
                                  <tr>
                                    <td colspan="2"><h3><?php echo $row->description; ?></h3></td>
                                  </tr>


                                  <tr>
                                    <td width="390"><table width="100%" class="tblImage">
  <tr>
    <td colspan="3"><img src="img/image.jpeg" alt="" border="0" /></td>
  </tr>
    <tr>
    <td><i class="fa fa-comment"></i>14 <i class="fa fa-heart"></i>20</td>
    <td>Details Go Below the image!</td>
    <td> </td>
    </tr>
</table></td>
                                    <td valign="top"><table border="0" cellspacing="4" cellpadding="4" style="background:#f7f7f7; color:#444444;">
                                      <tr>
                                          <td>Make: </td>
                                          <td><?php echo $row->make; ?></td>
                                      </tr>
                                        <tr>
                                          <td>Model: </td>
                                          <td><?php echo $row->model; ?></td>
                                        </tr>
                                        <tr>
                                          <td>Type: </td>
                                          <td><?php echo $row->type; ?></td>
                                        </tr>
                                        <tr>
                                          <td>Description:</td>
                                          <td><?php echo $row->details; ?></td>
                                        </tr>
                                        <tr>
                                          <td>Official Volume:</td>
                                          <td><?php echo $row->officialVolume; ?> litres</td>
                                        </tr>
                                        <tr>
                                          <td>Actual Volume: </td>
                                          <td><?php echo $row->actualVolume; ?> litres</td>
                                        </tr>
                                        <tr>
                                          <td>Dimensions:</td>
                                          <td><?php echo $row->d1; ?>" x <?php echo $row->d2; ?>" x <?php echo $row->d3; ?>"</td>
                                        </tr>
                                      </table><br />
<br /><?php

if($row->member_id == $_SESSION['SESS_MEMBER_ID']) {
?>
<a class="btn btn-default btn-sm" href="edittank.php?ID=<?php echo $encoded ?>"><i class="fa fa-pencil"></i> Edit</a> 
<a class="btn btn-default btn-sm" href="remove-confirm-tank.php?ID=<?php echo $encoded ?>&db=tanks&tank=<?php echo $row->description ?>"><i class="fa fa-times"></i> Delete</a> 
<a class="btn btn-default btn-sm" href="#"><i class="fa fa-gavel"></i> Retire</a>
<?php
} else {
echo "";
}
?>

                                    </td>
        </tr>
                                </table>
                              <br /></td><?php
}
mysql_free_result($result);
?>
                          </tr>
                            
                            <tr>
                              <td></td>
                            </tr>
                    </table>
				  </div> 
				</div> <!-- End .content-box-content -->
                </div>
        </td>
      </tr>
</table>

when I echo results it loops through the data and echo the code accordingly but if user has 3 or more entries in the database the data is just echo'd in the same row and goes out of the browser view port,

 

can I get these results and limit it to just echo them to a maximum of 2 per row then start a new row and repeat?

 

 

Link to comment
https://forums.phpfreaks.com/topic/286309-limit-php-results-to-2-per-row/
Share on other sites

went through some old code and pulled this out.

<?php
   echo '<table border="0" cellspacing="0" cellpadding="4">';
   $i = 0;
   $close = 0;
   $req = "select * from tanks WHERE member_id=$_SESSION[SESS_MEMBER_ID] order by id DESC";
   $query = mysql_query($req);
   while($row = mysql_fetch_array($query)) {

    if($i%50 == 0) {
        echo '<tr>';
        $close += 5;
    }
	
	$crypto = new phpFreaksCrypto(); 
    $id_to_be_encrypted = $row['id']; 
    $id_encrypted = $crypto->encrypt($id_to_be_encrypted); 
    $id_decrypted = $crypto->decrypt($id_encrypted); 
    $dateNew = date('jS F, Y', strtotime($row['date']));
	
	$encoded = urlencode($id_encrypted);
	
	echo '<td>'.$row['description'].'</td>';

    if($i == $close)
        echo '</tr>';
    $i++;
}
echo '</table>';
?>

can I work with this to limit the number of columns to 2x per row?

  • Solution

You could read the data into an array and break it apart with array_chunk(). Here's a quick example of how I've used array_chunk():

http://www.cyberscorpion.com/2013-08/build-html-tables-dynamically-with-php-part-2-simplify-with-array_chunk/

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.