Fluoresce Posted February 27, 2009 Share Posted February 27, 2009 The code below selects my categories, counts how many products are available for each category, separates the categories into groups, and echoes the groups, each with a heading, like this: Apparel All Apparel/Clothing (11) Women's Apparel (7) Men's Apparel (2) Children's Apparel (2) Automotive All Automotive (13) Auto Insurance (7) Auto Parts (6) Babies and Kids All Babies and Kids (9) Games and Toys (3) Baby Gear (6) Altogether, there are 13 such category groups which run all the way down my page. Can anyone tell me, please, how I can echo these results into two table columns? Any suggestions will be greatly appreciated. <?php $conn = mysql_connect('localhost','root') or trigger_error("SQL", E_USER_ERROR); mysql_select_db('heru_ctyi', $conn) or trigger_error("SQL", E_USER_ERROR); $query = "SELECT tcat.catheading, tcat.cat, tcat.category, COUNT(tcpncat.id) AS cpn_count FROM tcat LEFT JOIN tcpncat ON tcat.catid = tcpncat.catid GROUP BY tcat.catid ORDER BY tcat.cattype ASC"; $result = mysql_query($query, $conn) or trigger_error("SQL", E_USER_ERROR); $last_heading = ''; while ($row = mysql_fetch_assoc($result)) { if ($row['catheading'] != $last_heading) { if ($last_heading) echo "</p>"; $last_heading = $row['catheading']; echo "<h3>{$last_heading}</h3><p>"; } else echo "<br />"; echo "<a href=\"" . $row['category'] . "\">" . $row['cat'] . "</a> (" . $row['cpn_count'] . ")"; } echo "</p>"; ?> Link to comment https://forums.phpfreaks.com/topic/147111-solved-how-do-you-split-results-into-two-columns/ Share on other sites More sharing options...
Q695 Posted February 27, 2009 Share Posted February 27, 2009 put a table writer into the loop. Link to comment https://forums.phpfreaks.com/topic/147111-solved-how-do-you-split-results-into-two-columns/#findComment-772378 Share on other sites More sharing options...
Fluoresce Posted February 27, 2009 Author Share Posted February 27, 2009 Q695, please be a bit more specific. I'm a newbie. Into which loop? And what exactly is a table writer? Link to comment https://forums.phpfreaks.com/topic/147111-solved-how-do-you-split-results-into-two-columns/#findComment-772382 Share on other sites More sharing options...
haku Posted February 27, 2009 Share Posted February 27, 2009 put a table writer into the loop. If you can't give usable advice, don't reply!!! OP: It depends on how you want the categories split up (i.e. which categories in which column), and how you want the categories to appear. But one way you could do it would be to do this: $div1 = '<div id="div1">'; $div2 = '<div id="div2">'; $i = 2; while($row = mysql_fetch_assoc($result)) { if($i % 2 == 0) { $div1 .= _____; // output one category's worth of info here } else { $div2 .= _____; // output one category's worth of info here } $i++; } $div1 .= '</div>'; $div2 .= '</div>'; echo $div1; echo $div2; You will also need to add this to your CSS: #div1, #div2 { float:left; width: 50%; } This code is going to create two divs that have the category info in them, alternating back and forth between categories. It will then output the divs to the page, and the CSS will put them into two columns. Link to comment https://forums.phpfreaks.com/topic/147111-solved-how-do-you-split-results-into-two-columns/#findComment-772397 Share on other sites More sharing options...
Q695 Posted February 27, 2009 Share Posted February 27, 2009 There are different methods to create tables, so I was leaving the table design up to the designer, since I still use TR, and TD to develop my dynamic tables, which is a little harder to do. Link to comment https://forums.phpfreaks.com/topic/147111-solved-how-do-you-split-results-into-two-columns/#findComment-772418 Share on other sites More sharing options...
haku Posted February 27, 2009 Share Posted February 27, 2009 I still use TR, and TD to develop my dynamic tables, which is a little harder to do. Harder to do than what? Tables have to have TR and TD, or they are invalid code, and you aren't doing it right. Are you trying to say that doing it right is harder than doing it wrong? Link to comment https://forums.phpfreaks.com/topic/147111-solved-how-do-you-split-results-into-two-columns/#findComment-772420 Share on other sites More sharing options...
Q695 Posted February 27, 2009 Share Posted February 27, 2009 TR and TD is harder than a div tag, if you have the div tag knowledge at hand. Link to comment https://forums.phpfreaks.com/topic/147111-solved-how-do-you-split-results-into-two-columns/#findComment-772921 Share on other sites More sharing options...
Fluoresce Posted February 28, 2009 Author Share Posted February 28, 2009 Haku, thanks for your help. Link to comment https://forums.phpfreaks.com/topic/147111-solved-how-do-you-split-results-into-two-columns/#findComment-772975 Share on other sites More sharing options...
haku Posted February 28, 2009 Share Posted February 28, 2009 No problem. TR and TD is harder than a div tag, if you have the div tag knowledge at hand. So why would you use old outdated methods of positioning things (tables), if the new modern method (CSS) is easier? Link to comment https://forums.phpfreaks.com/topic/147111-solved-how-do-you-split-results-into-two-columns/#findComment-773020 Share on other sites More sharing options...
Q695 Posted February 28, 2009 Share Posted February 28, 2009 I don't know how to do the new method. Link to comment https://forums.phpfreaks.com/topic/147111-solved-how-do-you-split-results-into-two-columns/#findComment-773438 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.