vinpkl Posted October 21, 2008 Share Posted October 21, 2008 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>"; } } } Quote Link to comment https://forums.phpfreaks.com/topic/129375-split-data-in-4-columns-in-php/ Share on other sites More sharing options...
GKWelding Posted October 21, 2008 Share Posted October 21, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/129375-split-data-in-4-columns-in-php/#findComment-670700 Share on other sites More sharing options...
ranjuvs Posted October 21, 2008 Share Posted October 21, 2008 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>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/129375-split-data-in-4-columns-in-php/#findComment-670702 Share on other sites More sharing options...
vinpkl Posted October 21, 2008 Author Share Posted October 21, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/129375-split-data-in-4-columns-in-php/#findComment-670725 Share on other sites More sharing options...
ranjuvs Posted October 21, 2008 Share Posted October 21, 2008 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/129375-split-data-in-4-columns-in-php/#findComment-670729 Share on other sites More sharing options...
vinpkl Posted October 21, 2008 Author Share Posted October 21, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/129375-split-data-in-4-columns-in-php/#findComment-670739 Share on other sites More sharing options...
ranjuvs Posted October 21, 2008 Share Posted October 21, 2008 change your query $qry1="select * from product_table LIMIT 0,4"; Quote Link to comment https://forums.phpfreaks.com/topic/129375-split-data-in-4-columns-in-php/#findComment-670744 Share on other sites More sharing options...
vinpkl Posted October 21, 2008 Author Share Posted October 21, 2008 change your query $qry1="select * from product_table LIMIT 0,4"; hi rajuvs thanks a lot. everything works perfect. vineet Quote Link to comment https://forums.phpfreaks.com/topic/129375-split-data-in-4-columns-in-php/#findComment-670753 Share on other sites More sharing options...
vinpkl Posted October 23, 2008 Author Share Posted October 23, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/129375-split-data-in-4-columns-in-php/#findComment-672448 Share on other sites More sharing options...
sasa Posted October 23, 2008 Share Posted October 23, 2008 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> :</td>'; echo "</tr>"; echo "</table>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/129375-split-data-in-4-columns-in-php/#findComment-672475 Share on other sites More sharing options...
vinpkl Posted October 23, 2008 Author Share Posted October 23, 2008 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> :</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 Quote Link to comment https://forums.phpfreaks.com/topic/129375-split-data-in-4-columns-in-php/#findComment-672486 Share on other sites More sharing options...
sasa Posted October 23, 2008 Share Posted October 23, 2008 use LIMIT 12 in your query Quote Link to comment https://forums.phpfreaks.com/topic/129375-split-data-in-4-columns-in-php/#findComment-672492 Share on other sites More sharing options...
vinpkl Posted October 23, 2008 Author Share Posted October 23, 2008 use LIMIT 12 in your query Hi sas thanks. working perfect. vineet Quote Link to comment https://forums.phpfreaks.com/topic/129375-split-data-in-4-columns-in-php/#findComment-672495 Share on other sites More sharing options...
sasa Posted October 23, 2008 Share Posted October 23, 2008 ups lines if ($count > 0) while ($count++ < 4) echo '<td> :</td>'; echo "</tr>"; echo "</table>"; shoud be if ($count > 0) while ($count++ < 4) {echo '<td> :</td>'; echo "</tr>";} echo "</table>"; add some { Quote Link to comment https://forums.phpfreaks.com/topic/129375-split-data-in-4-columns-in-php/#findComment-672553 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.