TRI0N Posted July 24, 2010 Share Posted July 24, 2010 Tried searching for this but can't search for "2 Colum Output" so created this to be compatible with future searches. Anyways my problem I will make this as an example from what I what to do. I need to select all records in a database (25 lets say) and need to output them in matter to be formated by CSS elements. 1) Select All From Database to count records. 2) Divide Records into two arrays. 3) Set Right and Left Colum <DIV> 4) Echo the results out to format with <li></li> CSS Styles. I need to be able to echo these out into 2 <DIV> classes for right and left. The output I'm looking for is: -1 -14 -2 -15 -3 -16 -4 -17 -5 -18 -6 -19 -7 -20 -8 -21 -9 -22 -10 -23 -11 -24 -12 -25 -13 Making it so that the larger half is in colum 1 if total records is not divided evenly. Any help on this would be great. Link to comment https://forums.phpfreaks.com/topic/208730-two-colum-output-help/ Share on other sites More sharing options...
litebearer Posted July 24, 2010 Share Posted July 24, 2010 rough idea (psuedo) Align divs as you chose... $some_array = info from db $total_elements = count($some_array); $div_1_count = ceil($total_elements/2); $div_2_count = $total_elements - $div_1_count; $i=0; ?> <div> ?> while($i<$div_1_count) { echo $total_elements[$i] . "<br>"; $i ++; } ?> </div> <div> <?PHP $i = $div_1_count; while($i<$div_2_count) { echo $total_elements[$i] . "<br>"; $i ++; } ?> </div> Link to comment https://forums.phpfreaks.com/topic/208730-two-colum-output-help/#findComment-1090507 Share on other sites More sharing options...
TRI0N Posted July 24, 2010 Author Share Posted July 24, 2010 Okay the results from the above is that it's not outputing anything though it appears to be attemting to without errors. Maybe I'm going about wrong with my Fetch Venues Query. Here is my code: <div class="venue-holder"> <div class="column"> <?php // Database Connection mysql_connect("$dbhost2","$dbuser2","$dbpasswd2") ; // Database Selection mysql_select_db("$dbname2") ; // Fetch Venues $results2 = mysql_query("select distinct * from venues WHERE (id_no != '') ORDER BY venue ASC") ; $total_elements = count($results2); $div_1_count = ceil($total_elements/2); $div_2_count = $total_elements - $div_1_count; $i=0; ?> <div class="left-box"> <ul> <?php while($i<$div_1_count) { echo '<li>'. $total_elements[$i] . '</li>'; $i ++; } ?> </ul> </div> <div class="right-box"> <ul> <?php $i = $div_1_count; while($i<$div_2_count) { echo '<li>'. $total_elements[$i] . '</li>'; $i ++; } ?> </ul> </div> <?php // Close Database Connection mysql_close(); ?> </div> </div> Again any help would be great. Link to comment https://forums.phpfreaks.com/topic/208730-two-colum-output-help/#findComment-1090699 Share on other sites More sharing options...
TRI0N Posted July 24, 2010 Author Share Posted July 24, 2010 Actually after looking at this I'm not pulling out anything to be displayed as each <DIV> is being built. Not sure where or how I should add a "while($row = mysql_fetch_row($result2)) {$venue = $row[1];}". Link to comment https://forums.phpfreaks.com/topic/208730-two-colum-output-help/#findComment-1090702 Share on other sites More sharing options...
litebearer Posted July 24, 2010 Share Posted July 24, 2010 first step is to check and make sure variables have values that you are expecting .... ie simply echo out the values without worrying about the layout. Link to comment https://forums.phpfreaks.com/topic/208730-two-colum-output-help/#findComment-1090703 Share on other sites More sharing options...
litebearer Posted July 24, 2010 Share Posted July 24, 2010 also just noted, $total_elements is NOT an array, it is simply supposed to be the number of rows your query got. Link to comment https://forums.phpfreaks.com/topic/208730-two-colum-output-help/#findComment-1090706 Share on other sites More sharing options...
TRI0N Posted July 24, 2010 Author Share Posted July 24, 2010 Okay if I just toss in a "echo $total_elements;" in there I get a results of 1 showing.. There is 78 Venues in that database so something not right... Link to comment https://forums.phpfreaks.com/topic/208730-two-colum-output-help/#findComment-1090707 Share on other sites More sharing options...
litebearer Posted July 24, 2010 Share Posted July 24, 2010 what values are in id_no? (the values in the db) Link to comment https://forums.phpfreaks.com/topic/208730-two-colum-output-help/#findComment-1090719 Share on other sites More sharing options...
TRI0N Posted July 24, 2010 Author Share Posted July 24, 2010 1-78 Auto Increasement Field. Link to comment https://forums.phpfreaks.com/topic/208730-two-colum-output-help/#findComment-1090723 Share on other sites More sharing options...
litebearer Posted July 24, 2010 Share Posted July 24, 2010 try this and see what happens... $results2 = mysql_query("select distinct * from venues ORDER BY venue ASC") ; while($row = mysql_fetch_array($result2)) { echo $row['id_no'] . " " . $row['venue'] . "<br>"; } Link to comment https://forums.phpfreaks.com/topic/208730-two-colum-output-help/#findComment-1090725 Share on other sites More sharing options...
litebearer Posted July 24, 2010 Share Posted July 24, 2010 being as id_no is an auto increment, it wil NEVER be empty, therefore checking if it is empty is a waste of time. Link to comment https://forums.phpfreaks.com/topic/208730-two-colum-output-help/#findComment-1090726 Share on other sites More sharing options...
TRI0N Posted July 24, 2010 Author Share Posted July 24, 2010 I get a list of all the venues with their ID and Venus Name. There was just a slight error so it took me longer to respond. The Fetch had result2 not results2 in your example to use. Link to comment https://forums.phpfreaks.com/topic/208730-two-colum-output-help/#findComment-1090732 Share on other sites More sharing options...
litebearer Posted July 24, 2010 Share Posted July 24, 2010 Ok. next question. What is your estimate of the maximum number of records that may be returned? ie will you potenitally have hundreds or will it be a more manageable number? Link to comment https://forums.phpfreaks.com/topic/208730-two-colum-output-help/#findComment-1090735 Share on other sites More sharing options...
TRI0N Posted July 24, 2010 Author Share Posted July 24, 2010 Under a hundred is more realistic. Link to comment https://forums.phpfreaks.com/topic/208730-two-colum-output-help/#findComment-1090738 Share on other sites More sharing options...
litebearer Posted July 24, 2010 Share Posted July 24, 2010 you will need to tweak the div styling but... $results2 = mysql_query("select distinct * from venues ORDER BY venue ASC") ; $i=0; while($row = mysql_fetch_array($results2)) { $temp_array[$i] = $row['venue']; $i ++; } $total_elements = count($temp_array); $div1 = ceil($total_elements/2); $div2 = $total_elements - $div1; echo "<div>"; $i = 0; while($i<$div1) { echo $temp_array[$i] . "<br>"; $i ++; } echo "</div>"; $i = $div1; echo "<div>"; while($i<$div2) { echo $temp_array[$i] . "<br>"; $i ++; } echo "</div>"; Link to comment https://forums.phpfreaks.com/topic/208730-two-colum-output-help/#findComment-1090753 Share on other sites More sharing options...
TRI0N Posted July 24, 2010 Author Share Posted July 24, 2010 Okay the Left Box is working.. The right box however has no output.. Here is the code I formated: $results2 = mysql_query("select distinct * from venues ORDER BY venue ASC") ; $i=0; while($row = mysql_fetch_array($results2)) { $temp_array[$i] = $row['venue']; $i ++; } $total_elements = count($temp_array); $div1 = ceil($total_elements/2); $div2 = $total_elements - $div1; echo '<div class="left-box"><ul>'; $i = 0; while($i<$div1) { echo '<li>'. $temp_array[$i] . '</li>'; $i ++; } echo '</ul></div>'; $i = $div1; echo '<div class="right-box"><ul>'; while($i<$div2) { echo '<li>'. $temp_array[$i] . '</li>'; $i ++; } echo '</ul></div>'; Link to comment https://forums.phpfreaks.com/topic/208730-two-colum-output-help/#findComment-1090761 Share on other sites More sharing options...
litebearer Posted July 24, 2010 Share Posted July 24, 2010 is it displaying the correct number of elements in the left div? Link to comment https://forums.phpfreaks.com/topic/208730-two-colum-output-help/#findComment-1090767 Share on other sites More sharing options...
TRI0N Posted July 24, 2010 Author Share Posted July 24, 2010 Correct... I think it lies in the $i counter after the first div. Link to comment https://forums.phpfreaks.com/topic/208730-two-colum-output-help/#findComment-1090769 Share on other sites More sharing options...
litebearer Posted July 24, 2010 Share Posted July 24, 2010 check the values of $div1, $div2 and $i Link to comment https://forums.phpfreaks.com/topic/208730-two-colum-output-help/#findComment-1090770 Share on other sites More sharing options...
TRI0N Posted July 24, 2010 Author Share Posted July 24, 2010 Before Right DIV the totals are: $i and div1 = 39 $div2 = 39 After Right DIV: $i = 39 Link to comment https://forums.phpfreaks.com/topic/208730-two-colum-output-help/#findComment-1090773 Share on other sites More sharing options...
litebearer Posted July 24, 2010 Share Posted July 24, 2010 see what value $total_elements contains Link to comment https://forums.phpfreaks.com/topic/208730-two-colum-output-help/#findComment-1090777 Share on other sites More sharing options...
litebearer Posted July 24, 2010 Share Posted July 24, 2010 Duh!!! ok I see what it needs... for div 2 it should be... while($i<$total_elements) Link to comment https://forums.phpfreaks.com/topic/208730-two-colum-output-help/#findComment-1090778 Share on other sites More sharing options...
TRI0N Posted July 25, 2010 Author Share Posted July 25, 2010 Sucess!! It was in the 3rd while statement. I also removed $i = div1; at the start of the second DIV since $i is already a running total. Here is the final code that does the job. <?php // Database Connection mysql_connect("$dbhost2","$dbuser2","$dbpasswd2") ; // Database Selection mysql_select_db("$dbname2") ; // Fetch Venues $results2 = mysql_query("select distinct * from venues ORDER BY venue ASC") ; $i=0; while($row = mysql_fetch_array($results2)) { $temp_array[$i] = $row['venue']; $i ++; } $total_elements = count($temp_array); $div1 = ceil($total_elements/2); $div2 = $total_elements - $div1; echo '<div class="left-box"><ul>'; $i = 0; while($i<$div1) { echo '<li>'. $temp_array[$i] . '</li>'; $i ++; } echo '</ul></div>'; echo '<div class="right-box"><ul>'; while($i<$total_elements) { echo '<li>'. $temp_array[$i] . '</li>'; $i ++; } echo '</ul></div>'; // Close Database Connection mysql_close(); ?> Thanks a ton for all your help in getting this working.. Excellent! Link to comment https://forums.phpfreaks.com/topic/208730-two-colum-output-help/#findComment-1090780 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.