Jump to content

**SOLVED** multi-column output from a database with Divs


devofash

Recommended Posts

hi, need a little bit of help. Am trying to display data from mysql and split it into 2 columns (equal height) BUT by using divs. I've found quite a few scripts that do this but with TABLES and I dont want to use tables, was wondering if anyone has managed to do this with divs. Am posting a code below which uses tables was hoping that someone could modify it and use divs instead of table. Thank you in advance to anyone for helping.

[code]<?php
//set the number of columns
$columns = 2;

mysql_connect('localhost','','');
mysql_select_db('test');
$query = "SELECT stuff FROM mystuff ORDER BY stuff";
$result = mysql_query($query);

//we add this line because we need to know the number of rows
$num_rows = mysql_num_rows($result);
echo "<TABLE BORDER=\"0\">\n";

//changed this to a for loop so we can use the number of rows
for($i = 0; $i < $num_rows; $i++) {
    $row = mysql_fetch_array($result);
    if($i % $columns == 0) {
        //if there is no remainder, we want to start a new row
        echo "<TR>\n";
    }
    echo "<TD>" . $row['stuff'] . "</TD>\n";
    if(($i % $columns) == ($columns - 1) || ($i + 1) == $num_rows) {
        //if there is a remainder of 1, end the row
        //or if there is nothing left in our result set, end the row
        echo "</TR>\n";
    }
}
echo "</TABLE>\n";
?> [/code]
try
[code]
<html>
<head>
<meta name="generator" content="PhpED Version 4.5 (Build 4513)">
<title>Sample using DIV</title>
<meta name="author" content="Barand">
<link rel="shortcut icon"  href="">
<style>
    DIV {
        float: left;
        width: 200px;
        font-size: 0.8em;       
    }
    BR {
        clear : both;
    }
</style>
</head>
<body>
<?php
include '../test/db.php';
$sql = "SELECT stuff FROM tablename
        ORDER BY stuff";
$res = mysql_query($sql) or die(mysql_error());
$k = 0;
while (list($x) = mysql_fetch_row($res)) {
    echo "<DIV>$x</DIV>";
    if (++$k % 2 == 0) echo '<br />';
}
echo '<br />'
?>
</body>
</html>
[/code]
thanks alot barand it works like a charm.

got another similar question, therefore i'm not making another post. How do I split contents of one record  into 2 divs e.g. if I had a column called "contents" in the database which is 200 words and I want to display that record into two divs 100 words on left and 100 on right.  how would i go about doing this ?
try
[code]
<?php
$sql = "SELECT content FROM table";
$res = mysql_query($sql) or die(mysql_error());

while (list($x) = mysql_fetch_row($res)) {
    $tmparray = explode (' ', $x);                      // array of words
    $k = ceil(count($tmparray)/2);                      // count words, say 200
    $txtarray = array_chunk($tmparray,$k);              // split into 2 arrays of 100 words each
    echo "<DIV>" . join(' ', $txtarray[0]) . "</DIV>";  // join words and output
    echo "<DIV>" . join(' ', $txtarray[1]) . "</DIV>";  // join words and output
    echo '<br />';
}

?>[/code]

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.