/    Sign up×
Community /Pin to ProfileBookmark

display results in column

Hi,
Is there any better way, to display results in column, let says, 3 columns in a rows.

[code=php]
<html>
<table border=”1″ CELLSPACING=”0″ CELLPADDING=”10″ align=”center”>

<?php
$db = @mysql_connect(“localhost”, “root”, “”);
if( ! ($db = @mysql_connect(“localhost”, “root”, “”)) ) {
} else {
mysql_select_db(“database”,$db) or die(“Select DB Error: “.mysql_error());
}

//set 3 to 4 of you want 4 columns. Set it to 5 if you want 5, etc
$numcols = 3; // how many columns to display
$numcolsprinted = 0; // no of columns so far

// get the results to be displayed
$query = “SELECT * FROM cc”;
$mysql_result = mysql_query($query, $db);

// get each row
while($myrow = mysql_fetch_row($mysql_result))
{

//get data – eg, reading fields 0 and 1
$tn = $myrow[0];
$in= $myrow[1];

if ($numcolsprinted == $numcols) {
print “</tr>n<tr>n”;
$numcolsprinted = 0;
}

// output row from database
echo “<td>$in $tn</td>n”;

// bump up row counter
$numcolsprinted++;

} // end while loop

$colstobalance = $numcols – $numcolsprinted;
for ($i=1; $i<=$colstobalance; $i++) {

}
print “<TD>&nbsp;</TD>n”;
?>

</table>
</html>

[/code]

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 04.2006 — I usually do something like:
[code=php]
$result = mysql_query($sql) or die("$sql - ".mysql_error());
echo "<table>n<tr>";
$count = 0;
$numCols = 3;
while($row = mysql_fetch_assoc($result))
{
if($count != 0 and $count % $numCols == 0)
{
echo "</tr>n<tr>";
}
echo "<td>{$row['name']}</td>";
$count++;
}
echo "</tr>n</table>n";
?>
[/code]
Copy linkTweet thisAlerts:
@beginnerzauthorNov 04.2006 — thanks. But i have a new question, if let say, i wanted to display A by column, then inside column A, there are 1 2 3 4, that is also needed to display under A category.

[code=php]
$result = mysql_query($sql) or die("$sql - ".mysql_error());
echo "<table>n<tr>";
$count = 0;
$numCols = 3;
while($row = mysql_fetch_assoc($result))
{
if($count != 0 and $count % $numCols == 0)
{
echo "</tr>n<tr>";
}
$sql2 = "select * from profile where foreign_key = ".$row['pkey'];
$result2 = mysql_query($sql2) or die("$sql2 - ".mysql_error());
$data[] = "<td>$row['name']";
while($row2 = mysql_fetch_assoc($result2)){
$data[] = "<BR>".$row2['table2_info'];
}
echo "</td>";
$count++;
}
echo "</tr>n</table>n";
?>
[/code]
Copy linkTweet thisAlerts:
@bokehNov 04.2006 — Why are you using a table at all? Is it really tabular data you are dealing with? The problem with a table is if you have one sentence in one cell and five sentences in an adjacent cell your layout won't look like columns in the sense of a newspaper. There will be an obvious rowed nature to the presentation. If you want true columns (rather than a design that looks as though it was built from Lego blocks) I would use three <div> elements for the columns and <p> elements for the vertical spacing.
Copy linkTweet thisAlerts:
@bokehNov 04.2006 — [code=php]
$result = mysql_query($sql) or die("$sql - ".mysql_error());
echo "<table>n<tr>";
$count = 0;
$numCols = 3;
while($row = mysql_fetch_assoc($result))
{
if($count != 0 and $count % $numCols == 0)
{
echo "</tr>n<tr>";
}
echo "<td>{$row['name']}</td>";
$count++;
}
echo "</tr>n</table>n";
?>
[/code]
[/QUOTE]
Sorry, I'm very bored today but to the point: This code needs a method to deal with remainders so that on the last row of the table the cell count corresponds with the column count.

For example:[code=php]echo str_repeat(" <td></td>n", ($numCols - ($count % $numCols)) % $numCols));[/code]
Copy linkTweet thisAlerts:
@beginnerzauthorNov 05.2006 — i need it, so that it could display all the categories from my database which will produce something like this http://www.lelong.com.my/Auc/List/BrowseAll.asp
Copy linkTweet thisAlerts:
@bokehNov 05.2006 — produce something like this http://www.lelong.com.my/Auc/List/BrowseAll.asp[/QUOTE]According to [URL=http://validator.w3.org/check?verbose=1&uri=http%3A%2F%2Fwww.lelong.com.my%2FAuc%2FList%2FBrowseAll.asp]the validator[/URL] that page only has 1042 validation errors. Anyway looking at the data you should be using a list not a table.
×

Success!

Help @beginnerz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...