Page 1 of 1

Sorting PHP Content into 3 Even HTML Columns

Posted: Wed Sep 17, 2008 8:57 am
by Aquaman
My Mysql/php db man is tied up with studying for his masters so I am on my own with little more than html knowledge. I need help with making 3 even columns from the data drawn out of my db. I believe this is a simple matter but I do not know where to put the number for columns. Here is the code I am working with:

$sql = "select distinct category,catid from suppliercategory order by category asc;";
$result = @mysql_query($sql, $connection) or die("Error #". mysql_errno() . ": " . mysql_error());
$num_results = mysql_num_rows($result);
for ($i=0; $i<$num_results; $i=++) {
if ($bgclr == "#FFFFFF") {
$bgclr="#E2E1E1";
}else{
$bgclr="#FFFFFF";
}
$row = mysql_fetch_array($result);

if ($row["catid"] == "0000000424") {
$RowWriter = $RowWriter."<tr bgcolor=\"$bgclr\"><td><a href=\"feed_search.php?id=".$row["catid"]."\" class=\"logname\">";
}else{
if($row["catid"] == "0000003500"){
$RowWriter = $RowWriter."<tr bgcolor=\"$bgclr\"><td><a href=\"med_search.php?id=".$row["catid"]."\" class=\"logname\">";
}else{
$RowWriter = $RowWriter."<tr bgcolor=\"$bgclr\"><td><a href=\"supplier_countries.php?id=".$row["catid"]."\"

class=\"logname\">";
}
}
$RowWriter = $RowWriter.$row["category"]."</a></td></tr>";
}
?>

Re: Sorting PHP Content into 3 Even HTML Columns

Posted: Wed Sep 17, 2008 1:39 pm
by Weasel5-12
i dont know if this will help alot.

But that being said, i had a situation similar to yours, and it seemed to help me alot...
have you had a look at the thread viewtopic.php?f=1&t=25105??

A very very kind person has worked through the code for us...

may or may not help... sorry if it doesn't

Re: Sorting PHP Content into 3 Even HTML Columns

Posted: Wed Sep 17, 2008 4:25 pm
by Aquaman
Thanks but that is way to much code for me to deal with. Last time I worked on changing the code the whole db went down. I hope that someone will come along with a simple solution and if not, I wait.

Cheers

Re: Sorting PHP Content into 3 Even HTML Columns

Posted: Wed Sep 17, 2008 4:38 pm
by thinsoldier
Tell your Mysql/php db man his code is fugly.
Show me an example page of what you're getting with that code and what you'd like to have instead. Then I might be able to figure out if I can help.

The only times I can remember wanting/needing even columns were cases where the data didn't need to be in <tables>

Re: Sorting PHP Content into 3 Even HTML Columns

Posted: Thu Sep 18, 2008 2:11 pm
by Aquaman
The address is: http://www.aquafind.com/supplier/supplier_search.php

The code I tried to change was:

<table border="0" class="dataDisplay">
<tr bgcolor="#FF9933">
<td><b>Categories</b></td>
</tr>
<?= $RowWriter; ?>
</table>

Re: Sorting PHP Content into 3 Even HTML Columns

Posted: Thu Sep 18, 2008 5:09 pm
by thinsoldier
"Search by Company name or from the Categories below."

You want the looong list under that heading to be made into 3 equal width columns ( and *almost* equal height)?


Easiest way I can this of (is the one I use all the time):

Code: Select all

 
<ul>
<li>Accountants</li>
<li>Adhesives</li>
<li>Adhesives - Waterproof</li>
<li>Advertising Agencies</li>
<li>Aerators - Deicing</li>
<li>Aerators - Freshwater</li>
</ul>
 
Tables suck when not used for actual 'tabular' data.
Use CSS to give the <ul> a width & set its overflow:auto or :hidden
Don't give it a height.

Make all <li> float:left. and give them a width that results in only 3 li's being able to fit on a line.
You've got a pretty wide content area, you might be able to get away with 4 columns.

Drawback of this techniqe is that instead of things being listed alphabetically going down the page, they'll be alphabetical going across
due to the way floats work.

Code: Select all

 
[apple  ][banana  ][cat    ][dog    ]
[egg     ][frog       ][goat  ][horse ]
 
instead of
 
[ apple    ] [ dog  ]
[ banana ] [ egg  ]
[  cat      ] [frog  ]]
 
... uhhh... paste that into notepad or something so they line up correctly to see what I mean.

Alternatively have a look at php's array_chunk function.
you can try to put the results in an array, divide the total by 3 and break the array into 3 chunks that are close to what you got from the division math. Then turn those 3 chunks into 3 separate <ul>. Make each <ul> float:left so that they're next to each other (like columns).

To avoid float collapse you should probably wrap the 3 <ul> inside a <div> and make the div overflow:auto or :hidden (like I said before).
I think for IE you'd also have to give the div a width for overflow:hidden/auto to behave properly.

Re: Sorting PHP Content into 3 Even HTML Columns

Posted: Thu Sep 18, 2008 7:33 pm
by Aquaman
I have played around with this and worked with the array chunk as you suggested but couldn't get anything to work so pleeeeaase don't waste any more of your time with this. It is obviously over my head and I will just wait. I really needed to add subcategories but that was way to complicated for my db guy to do quickly as there are already two in the db. I figured I could get by with 3 columns until he had the time to do this since I want to add over 200 mored categories.

Thank you again.

Re: Sorting PHP Content into 3 Even HTML Columns

Posted: Thu Sep 18, 2008 9:42 pm
by thinsoldier
Not guaranteeing this works. (see sig)

The end result would be 3 <ul> with the same number of category names in each one (except possibly the last one).

Wrap those 3 in a div. Float the 3 <ul> left. I didn't bother with the alternating row colors because I wanted to keep it as simple as I could for you.
You could have a tiling pattern as the background image on the <ul> to simulate alternating row colors.
Or just give all <li> a bottom border and a bit of padding to make them just as easy to read as zebra stripes.

Code: Select all

 
<?php
$sql = "select distinct category,catid from suppliercategory order by category asc;";
$result = mysql_query($sql, $connection) or die("Error #". mysql_errno() . ": " . mysql_error());
// get all the found data into an array
while($info = mysql_fetch_assoc($result))
{ $allrows[] = $info; }
 
// how many did we find?
$count = count($allrows);
 
// divide the amount found by how many columns you want
$howmanypercolumn = $count / 3;
//need to round this up to next whole number
$howmanypercolumn = ceil($howmanypercolumn);
// example: 
// 500 records divided by 3 means 166.6 records per column.
// round that up to 167
// column 1 will have 167 entries
// column 2 will have 167 entries
// column 3 will have 166 entries
 
// creates an array containing all the records split into 3 sub-arrays
$columnChunks = array_chunk($allrows, $howmanypercolumn, true);
 
foreach($columnChunks as $chunk)
{
    foreach($chunk as $row) // each row inside of a chunk
    {
        if ($row["catid"] == '0000000424') { $url = 'feed_search.php';}
        elseif($row["catid"] == '0000003500') {$url = 'med_search.php';}
        else{ $url = 'supplier_countries.php';}
        $listitems[] = sprintf('<li><a href="%s?id=%s">%s</a></li>', $url, $row['catid'], $row['category']);
    }
    // convert listitems array into just one long string of html
    // and wrap it with <ul></ul>
    echo sprintf('<ul class="column">%s</ul>', implode('',$listitems));
}
 
 
 
// I'd probably go as far as turning this all into a group of function like
echo html_ulColumnPrint(Supplier_GetAllByCategories(), 3);
?>
 

Re: Sorting PHP Content into 3 Even HTML Columns

Posted: Fri Sep 19, 2008 1:04 pm
by Aquaman
The new code did not work. It did at least leave the one column header in place but no columns. Thanks for the try. You are putting in to much time on this and am sure you have something better to do.

Re: Sorting PHP Content into 3 Even HTML Columns

Posted: Fri Sep 19, 2008 5:42 pm
by thinsoldier
You're doing it wrong.

I spent about 30 minutes on it just to prove that I was on the right track. Then I spent a metric ass-load of time and effort trying to figure out where in the hell in your html the content area was. $%&#!

Live Code: http://thinsoldier.com/wip/php/aquafind/



COMMENTS:
Your html on that site is absolutely horrible!
A text-book examples of why you shouldn't use <table> for page layout!!!!!!!

All the pages I looked at had FAR TOO MUCH html, far too much content, and the ratio of html vs actual content was rediculous.
I wouldn't be surprised if that hurts your search engine friendliness greatly.

These are far too many categories to list on a single page. I suggest you look into both sub-categories AND pagination.

Some of them, their name is so long that they are forced to wrap onto 2 or 3 lines when sqeezed into the width of a column. Thus making the column with the most long names always much much longer than the others, preventing an "even" appearance. The only fix for this was to force the lines not to wrap using css white-space: nowrap.

But the side effect of that is that you can no longer see the whole category name.
To compensate for that I duplicated the category name as the title attribute of the link.

Code: Select all

 
<?php
error_reporting(E_ALL);
 
// extracted the data from your html
// pretend this actually queries the database
// and returns all the results
include('database.php');
 
// which I then turn into a single array containing everything
$allrows = $db;
 
// how many did we find?
$count = count($allrows);
 
// divide the amount found by how many columns you want
$howmanypercolumn = $count / 3;
//need to round this up to next whole number
$howmanypercolumn = ceil($howmanypercolumn);
// example:
// 500 records divided by 3 means 166.6 records per column.
// round that up to 167
// column 1 will have 167 entries
// column 2 will have 167 entries
// column 3 will have 166 entries
 
// creates an array containing all the records split into 3 sub-arrays
$columnChunks = array_chunk($allrows, $howmanypercolumn, true);
 
foreach($columnChunks as $chunk)
{
    foreach($chunk as $row) // each row inside of a chunk
    {
        if ($row["catid"] == '0000000424') { $url = 'feed_search.php';}
        elseif($row["catid"] == '0000003500') {$url = 'med_search.php';}
        else{ $url = 'supplier_countries.php';}
        $listitems[] = sprintf("\r<li>\r".'<a href="%s?id=%s" title="%s">%s</a>'."\r</li>", $url, $row['catid'], $row['category'], $row['category']);
    }
    // convert listitems array into just one long string of html
    // and wrap it with <ul></ul>
    $content[] = sprintf('<ul class="column">%s</ul>'."\n\n", implode('',$listitems));
    
    unset($listitems); // or else every column will be combined with the previous column
}
 
$content = implode('', $content);
include('template.php');
?>
 

A sample of what's in the database.php file

Code: Select all

 
$db[] = array('catid'=>0000000001 , 'category'=>'Accountants');
$db[] = array('catid'=>0000000275 , 'category'=>'Adhesives');
$db[] = array('catid'=>0000000430 , 'category'=>'Adhesives - Waterproof');
$db[] = array('catid'=>0000000002 , 'category'=>'Advertising Agencies');
$db[] = array('catid'=>0000000431 , 'category'=>'Aerators - Deicing');
$db[] = array('catid'=>0000000432 , 'category'=>'Aerators - Freshwater');
$db[] = array('catid'=>0000000433 , 'category'=>'Aerators - Paddle Wheel');
$db[] = array('catid'=>0000000434 , 'category'=>'Aerators - Saltwater');
$db[] = array('catid'=>0000000435 , 'category'=>'Aerators - Wind Driven');
$db[] = array('catid'=>0000000003 , 'category'=>'Aerators Equipment and Supplies');
 

The css for the 3 <ul> (only tested in firefox & safari)

Code: Select all

 
#pooHead {
font-size: 35px;
font-family: "trebuchet ms";
padding: 10px;
text-align: center;
letter-spacing: 25px;
}
 
ul.column {
font-family: verdana;
font-size: 10;
white-space: nowrap;
list-style-type: none;
margin:0 13px 0 0;
padding:0;
width: 160px;
overflow:hidden;
float:left;
}
 
ul.column li {
padding: 3px;
margin-bottom: 5px;
background: #eee;
}
 
 

Re: Sorting PHP Content into 3 Even HTML Columns

Posted: Fri Sep 19, 2008 7:56 pm
by Aquaman
Sorry but no go. This is going to get rewritten completely. Thank you