PHP Query result into two columns

Jon C
Jon C used Ask the Experts™
on
Hi All

I am having a few issues trying to get the following to work, I am querying a DB and want the results to display into two equal columns, I can get all the results to display correctly in the first column, but i need half of them to display in the second column, the query displays a picture and a product title from the DB:


<div class="grid_6 alpha" id="left-column">				
	
		<div class="left-column">					
				<h1>Man<span style="color: #FF0">Products</span></h1>
				<p>Our Man Products..</p> <br />
<?php

	//session_start();
	include "admin/sql_comm_helper.php";
	include "admin/sql_data.php";  // File that contains the SQL connection information.
	login_mysql();
	select_db(FALSE);
	$fid = $_POST['fid'];
$data = mysql_query("SELECT * FROM upload WHERE manufacturer='Man'") 
or die(mysql_error()); 

Print "<table border cellpadding=3>";  
while($info = mysql_fetch_array( $data ))  
{  
print "<tr>";  
print '<td><p><img src="download.php?fid='.$info['fid'] . '" width="125" height="125"></td>'; 
print "<td><a href='product.php?fid=".$info['fid'] . "'><span style='font-size: 18px;'>".$info['productname'] ."</span></a></P></td> "; 
}  
print "</table>";
?> 



		</div>	<!--END SERVICES--> 
		
	</div>	<!--END LEFT COLUMN  id="left-column"-->

	
<!--========================================RIGHT COLUMN================================================-->
		<div class="grid_6" id="right-column">
		  <div class="left-column">
		    <h1>&nbsp;</h1>
		    <p>&nbsp;</p>
<br />
		    




	      </div>
		</div>
  <p>
		  <!--END RIGHT COLUMN-->

Open in new window



Many thanks

Jon
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Do you mean two columns within the table, or spread across the two DIVs - left-column / right-column.

Author

Commented:
Hi, thanks for the reply, sorry if my explanation wasnt clear

I need it spread across two DIVs

I have included the second DIV in the code above, the code is in the first DIV which appears on the left of the page, but if there are a lot of results the page ends up being quite long, therefore I want half of the results to appear in the second DIV on the right of the page

Thanks
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
OK.

Couple of ways to achieve it. First one is to run your query, count the records, create the left div, loop through half, and then create the right div and loop through the rest.

$numberOfRecords = mysql_num_rows($data);

<div class="left-column">
for ($i = 1; $i <= ceil($numberOfRecords/2); $i++) {
     $info = mysql_fetch_array( $data )
     //echo your data here
}
</div>

<div class="right-column">
     while($info = mysql_fetch_array( $data )) {
     //echo your data here
}
</div>

Open in new window

Another way is to alternative create a left and right div. If you have 30 records for example, you will then end up with 30 'wrapper' divs that you can style using CSS, like so

<div class="left">content</div>
<div class="right">content</div>
<div class="left">content</div>
<div class="right">content</div>
<div class="left">content</div>
<div class="right">content</div>


$i = 0;
while($info = mysql_fetch_array( $data )) {
     <div class="<?php echo ($i%2==0) ? 'left' : 'right' ?>">your data</div>
$i++;
}

Open in new window

Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Hi  

Thanks again for your reply, I am trying to use your first suggestion as i think that would fit best with what i am trying to achive, I have edited the code as below, but I am getting a 500 Internal Server Error:

<div class="grid_6 alpha" id="left-column">				
		<div class="left-column">					
		<h1>Man <span style="color: #FF0">Products</span></h1>
		<p>Our Man Products..</p> <br />
<?php

	//session_start();
	include "admin/sql_comm_helper.php";
	include "admin/sql_data.php";  // File that contains the SQL connection information.
	login_mysql();
	select_db(FALSE);
	$fid = $_POST['fid'];
$data = mysql_query("SELECT * FROM upload WHERE manufacturer='Man'") 
or die(mysql_error()); 
$numberOfRecords = mysql_num_rows($data);

Print "<table border cellpadding=3>";  
for ($i = 1; $i <= ceil($numberOfRecords/2); $i++) {
     $info = mysql_fetch_array( $data )
print "<tr>";  
print '<td><p><img src="download.php?fid='.$info['fid'] . '" width="125" height="125"></td>'; 
print "<td><a href='product.php?fid=".$info['fid'] . "'><span style='font-size: 18px;'>".$info['productname'] ."</span></a></P></td> "; 
}

</div>

print "</table>";
?> 
		</div>	<!--END SERVICES--> 
		
	</div>	<!--END LEFT COLUMN  id="left-column"-->

	
<!--========================================RIGHT COLUMN================================================-->
		<div class="grid_6" id="right-column">
		  <div class="left-column">
		    <h1>&nbsp;</h1>
		    <p>&nbsp;</p>
<br />
		    

<?
while($info = mysql_fetch_array( $data )) {
   print "<tr>";  
print '<td><p><img src="download.php?fid='.$info['fid'] . '" width="125" height="125"></td>'; 
print "<td><a href='product.php?fid=".$info['fid'] . "'><span style='font-size: 18px;'>".$info['productname'] ."</span></a></P></td> "; 
}

</div>
?>
      </div>
		</div>
  <p>
		  <!--END RIGHT COLUMN-->

Open in new window

Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
OK, couple of things I've spotted.

Line 19 needs terminating properly with a semi-colon

     $info = mysql_fetch_array( $data );

On lines 25 and 49 you have a </div> within the php block. Delete that and try again.

You might also want to add some error reporting to the start of your page.

<?php
   error_reporting(E_ALL ^ E_NOTICE);
   ini_set('display_errors', 1);
?>

Author

Commented:
Hi

Thanks for your reply again

I have changed the code as you suggested and added the error reporting but it still gives the Internal Server Error

Thanks

Jon

Author

Commented:
Sorry, think it was some of my typo errors, here is what worked, thanks to your help:

		<div class="left-column">					
				<h1>Man <span style="color: #FF0">Products</span></h1>
				<p>Our Man Products..</p> <br />
				
				





<?php

	//session_start();
	include "admin/sql_comm_helper.php";
	include "admin/sql_data.php";  // File that contains the SQL connection information.
	login_mysql();
	select_db(FALSE);
	$fid = $_POST['fid'];
$data = mysql_query("SELECT * FROM upload WHERE manufacturer='Man'") 
or die(mysql_error()); 
$numberOfRecords = mysql_num_rows($data);


Print "<table border cellpadding=3>";  
for ($i = 1; $i <= ceil($numberOfRecords/2); $i++) {
     $info = mysql_fetch_array( $data );  
print "<tr>";  
print '<td><p><img src="download.php?fid='.$info['fid'] . '" width="125" height="125"></td>'; 
print "<td><a href='product.php?fid=".$info['fid'] . "'><span style='font-size: 18px;'>".$info['productname'] ."</span></a></P></td> "; 
}  
print "</table>";
?> 

		</div>	<!--END SERVICES--> 
		
	</div>	<!--END LEFT COLUMN  id="left-column"-->

	
<!--========================================RIGHT COLUMN================================================-->
		<div class="grid_6" id="right-column">
		  <div class="left-column">
		    <h1>&nbsp;</h1>
		    <p>&nbsp;</p>
<br />
		    
<?
while($info = mysql_fetch_array( $data )) {
     print "<tr>";  
print '<td><p><img src="download.php?fid='.$info['fid'] . '" width="125" height="125"></td>'; 
print "<td><a href='product.php?fid=".$info['fid'] . "'><span style='font-size: 18px;'>".$info['productname'] ."</span></a></P></td> "; 
}
?>

	      </div>
		</div>
  <p>
		  <!--END RIGHT COLUMN-->

Open in new window

Author

Commented:
Many thanks, very detailed help, greatly appreciated, Jon
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
No worries, glad I could help.

You might want to check your HTML as well. You have a couple of loose tags. On lines 28 and 49 you have an opening P tag, but no closing P. On lines 29 and 50, you have a closing P tag but no opening one. I'm guessing these are supposed to be a matching pair, but you can't have a P tag span over several <TD>s

Also, the code for the right column is creating TRs and TDs but has no TABLE associated with it.

Good luck with your project :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial