Jump to content

Two Colum Output Help


TRI0N

Recommended Posts

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

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>

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.

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>";



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>';

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.