Jump to content

split data in 4 columns in php


vinpkl

Recommended Posts

hi all

i m using this script to fetch and display products in my page.

the "for loop" creates four columns. and the other part fetches and display the products.

but my problem is that i m not able to insert the products in 4 columns created by "for loop". when i change ($i=result1) then the script doesnt work.

 

<?php
for($i=1;$i<=1;$i++)
{
//echo "<tr>";
echo "<td width=148>" .$i."</td>";
echo "<td width=148>" .$i."</td>";	
echo "<td width=148>" .$i."</td>";
echo "<td width=148>" .$i."</td>";
//echo "</tr>";			

$qry1="select * from product_table";	
$result1 = mysql_query($qry1);			  
if(mysql_num_rows($result1)>0)
{
while($row1=mysql_fetch_array($result1))
{

	echo "<table border=1 bgcolor=#ffddee>";
	echo "<tr>";
	echo "<td width=125>" . "<img width=116 height=117 src='admin/uploads/" . $row1['image'] . "'/>" . "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td valign=top  width=125>". $row1['product_name'] . "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td valign=top>". $row1['description'] . "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td valign=top class=cut>". $row1['cutout_price'] . "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td valign=top>". $row1['price'] . "</td>";
	echo "</tr>";
	echo "</table>";
}

}

}

Link to comment
Share on other sites

what you need to research is something similar to a pagination script. See the following code and see if you can figure it out for yourself what the script is doing and how to incorporate this into your script.

 

$max = 4; //max number of columns

if($i==$max){
$i = 1;
</TR><TR><TD>$product</TD>;
}else{
<TD>$product</TD>;
$i++;
}

 

As you can see from above it is not just as simple as creating a table with 4 columns and expecting PHP to know what to do.

Link to comment
Share on other sites

is this what you are trying

 

$qry1="select * from product_table";   
$result1 = mysql_query($qry1);           
   if(mysql_num_rows($result1)>0)
   {
   while($row1=mysql_fetch_array($result1))
   {
   
      echo "<table border=1 bgcolor=#ffddee>";
      echo "<tr>";
      echo "<td width=148>Image</td><td width=148>prod_name</td<td width=148>description</td><td width=148>cut outprice</td><td width=148>price</td>";
      echo "</tr>";
      echo "<tr>";
      echo "<td width=125>" . "<img width=116 height=117 src='admin/uploads/" . $row1['image'] . "'/>" . "</td>";
      echo "<td valign=top  width=125>". $row1['product_name'] . "</td>";
      echo "<td valign=top>". $row1['description'] . "</td>";
      echo "<td valign=top class=cut>". $row1['cutout_price'] . "</td>";
      echo "<td valign=top>". $row1['price'] . "</td>";
      echo "</tr>";
      echo "</table>";
   }
   
   }

Link to comment
Share on other sites

is this what you are trying

 

$qry1="select * from product_table";   
$result1 = mysql_query($qry1);           
   if(mysql_num_rows($result1)>0)
   {
   while($row1=mysql_fetch_array($result1))
   {
   
      echo "<table border=1 bgcolor=#ffddee>";
      echo "<tr>";
      echo "<td width=148>Image</td><td width=148>prod_name</td<td width=148>description</td><td width=148>cut outprice</td><td width=148>price</td>";
      echo "</tr>";
      echo "<tr>";
      echo "<td width=125>" . "<img width=116 height=117 src='admin/uploads/" . $row1['image'] . "'/>" . "</td>";
      echo "<td valign=top  width=125>". $row1['product_name'] . "</td>";
      echo "<td valign=top>". $row1['description'] . "</td>";
      echo "<td valign=top class=cut>". $row1['cutout_price'] . "</td>";
      echo "<td valign=top>". $row1['price'] . "</td>";
      echo "</tr>";
      echo "</table>";
   }
   
   }

 

hi rajuvs

 

thanks for reply. actually what u did is splitted my content into four rows but different columns. but i need these four rows content to come horizontally not vertically. means like 1|2|3|4.

 

let me make my question more clear again :

 

1) i need four columns in a table means 4 <td>.

2) i have a table which contains 5 rows in which first row contains product image, second row contains product name, third row contains product description, forth row contains product cutout price and fifth row contains product price.

3) now i need all this content of this table with 5 rows to be inserted in my 4 columns with separate products.

 

means the 2no. table that contains content will be inserted in all 4 columns with different image, description, price etc.

 

i think i may be clear with my question now.

 

vineet

Link to comment
Share on other sites

try this...

 

<?php
echo "<table border=1>";
      echo "<tr>";
$qry1="select * from product_table";  
$result1 = mysql_query($qry1);          
   if(mysql_num_rows($result1)>0)
   {
   while($row1=mysql_fetch_array($result1))
   {
  
            echo "<td width=148>";
      
      echo "<table border=1 bgcolor=#ffddee>";
      echo "<tr>";
      echo "<td width=125>" . "<img width=116 height=117 src='admin/uploads/" . $row1['image'] . "'/>" . "</td>";
      echo "</tr>";
      echo "<tr>";
      echo "<td valign=top  width=125>". $row1['product_name'] . "</td>";
      echo "</tr>";
      echo "<tr>";
      echo "<td valign=top>". $row1['description'] . "</td>";
      echo "</tr>";
      echo "<tr>";
      echo "<td valign=top class=cut>". $row1['cutout_price'] . "</td>";
      echo "</tr>";
      echo "<tr>";
      echo "<td valign=top>". $row1['price'] . "</td>";
      echo "</tr>";
      echo "</table>";
echo "</td>";
   }
  
   }
      echo "</tr>";
echo "</table>"; 
?>

Link to comment
Share on other sites

try this...

 

<?php
echo "<table border=1>";
      echo "<tr>";
$qry1="select * from product_table";  
$result1 = mysql_query($qry1);          
   if(mysql_num_rows($result1)>0)
   {
   while($row1=mysql_fetch_array($result1))
   {
  
            echo "<td width=148>";
      
      echo "<table border=1 bgcolor=#ffddee>";
      echo "<tr>";
      echo "<td width=125>" . "<img width=116 height=117 src='admin/uploads/" . $row1['image'] . "'/>" . "</td>";
      echo "</tr>";
      echo "<tr>";
      echo "<td valign=top  width=125>". $row1['product_name'] . "</td>";
      echo "</tr>";
      echo "<tr>";
      echo "<td valign=top>". $row1['description'] . "</td>";
      echo "</tr>";
      echo "<tr>";
      echo "<td valign=top class=cut>". $row1['cutout_price'] . "</td>";
      echo "</tr>";
      echo "<tr>";
      echo "<td valign=top>". $row1['price'] . "</td>";
      echo "</tr>";
      echo "</table>";
echo "</td>";
   }
  
   }
      echo "</tr>";
echo "</table>"; 
?>

 

hi rajuvs

 

thanks a lot. it worked great. it worked like i wanted.

 

one thing more i would like to ask that i 20 products in the database from where these products are coming. But i want to display only 4 in my page not all 20.

what condition will apply

 

vineet

Link to comment
Share on other sites

try this...

 

<?php
echo "<table border=1>";
      echo "<tr>";
$qry1="select * from product_table";  
$result1 = mysql_query($qry1);          
   if(mysql_num_rows($result1)>0)
   {
   while($row1=mysql_fetch_array($result1))
   {
  
            echo "<td width=148>";
      
      echo "<table border=1 bgcolor=#ffddee>";
      echo "<tr>";
      echo "<td width=125>" . "<img width=116 height=117 src='admin/uploads/" . $row1['image'] . "'/>" . "</td>";
      echo "</tr>";
      echo "<tr>";
      echo "<td valign=top  width=125>". $row1['product_name'] . "</td>";
      echo "</tr>";
      echo "<tr>";
      echo "<td valign=top>". $row1['description'] . "</td>";
      echo "</tr>";
      echo "<tr>";
      echo "<td valign=top class=cut>". $row1['cutout_price'] . "</td>";
      echo "</tr>";
      echo "<tr>";
      echo "<td valign=top>". $row1['price'] . "</td>";
      echo "</tr>";
      echo "</table>";
echo "</td>";
   }
  
   }
      echo "</tr>";
echo "</table>"; 
?>

 

hi rajuvs

 

thanks a lot. it worked great. it worked like i wanted.

 

one thing more i would like to ask that i 20 products in the database from where these products are coming. But i want to display only 4 in my page not all 20.

what condition will apply

 

vineet

 

hi ranjuvs

 

This code displays four products in a row as i needed.

if i need that after these four products, the next four products should showup in 2nd row and then next four products in 3rd row.

what do we need to change in this code

 

vineet

Link to comment
Share on other sites

try

<?php
$count = 0;
$qry1="select * from product_table";
$result1 = mysql_query($qry1);
if(mysql_num_rows($result1)>0)
{
echo "<table border=1>";
while($row1=mysql_fetch_array($result1))
{
	if ($count++  == 0) echo '<tr>';

	echo "<td width=148>";

	echo "<table border=1 bgcolor=#ffddee>";
	echo "<tr>";
	echo "<td width=125>" . "<img width=116 height=117 src='admin/uploads/" . $row1['image'] . "'/>" . "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td valign=top  width=125>". $row1['product_name'] . "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td valign=top>". $row1['description'] . "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td valign=top class=cut>". $row1['cutout_price'] . "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td valign=top>". $row1['price'] . "</td>";
	echo "</tr>";
	echo "</table>";
	echo "</td>";
	if ($count == 4){
		$count = 0;
		echo '</tr>';
	}
}
if ($count > 0) while ($count++ < 4) echo '<td>&nbsp:</td>';
echo "</tr>";
echo "</table>";
}

?>

Link to comment
Share on other sites

try

<?php
$count = 0;
$qry1="select * from product_table";
$result1 = mysql_query($qry1);
if(mysql_num_rows($result1)>0)
{
echo "<table border=1>";
while($row1=mysql_fetch_array($result1))
{
	if ($count++  == 0) echo '<tr>';

	echo "<td width=148>";

	echo "<table border=1 bgcolor=#ffddee>";
	echo "<tr>";
	echo "<td width=125>" . "<img width=116 height=117 src='admin/uploads/" . $row1['image'] . "'/>" . "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td valign=top  width=125>". $row1['product_name'] . "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td valign=top>". $row1['description'] . "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td valign=top class=cut>". $row1['cutout_price'] . "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td valign=top>". $row1['price'] . "</td>";
	echo "</tr>";
	echo "</table>";
	echo "</td>";
	if ($count == 4){
		$count = 0;
		echo '</tr>';
	}
}
if ($count > 0) while ($count++ < 4) echo '<td>&nbsp:</td>';
echo "</tr>";
echo "</table>";
}

?>

 

hi sasa

 

thanks for the reply. its works great.

 

i would like to ask that if i am right then $count is for product. And this wil display 4 products in each row till the end of number of products in database.

 

can we apply condition that only 3 rows should be displayed mean only 12 products on page (3rows x 4products). Means 3 rows containing of 4 products each. i dont want to show all the products.

 

vineet

Link to comment
Share on other sites

ups

lines

if ($count > 0) while ($count++ < 4) echo '<td>&nbsp:</td>';
   echo "</tr>";
   echo "</table>";

shoud be

if ($count > 0) while ($count++ < 4) {echo '<td>&nbsp:</td>';
   echo "</tr>";}
   echo "</table>";

add some {

Link to comment
Share on other sites

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.