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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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