...

View Full Version : Help splitting this output across 2 or more columns



dmac68
07-27-2008, 03:17 PM
I have the below code to pull data from my simple mySQL db. How can I get the two results split between columns? Right now it just has the category with the ad names below. Some categories will only have one name, so I guess it's a division of the total output between columns. Not a set number per columns. So, not cat 1 cat 2 with cat 3 and cat 4 underneath. Flow an even number between columns, so it goes down col 1, to col 2, to col 3 if necessary and so on.



<?php
$query = "select * from category";
$result = mysql_query($query);

while($row = mysql_fetch_array($result)) {
$categoryid = $row['id'];
$categoryname = $row['category'];
$query2 = "select * from advertisers where category_id='$categoryid' and active=1";
$result2 = mysql_query($query2);

if(mysql_num_rows($result2) > 0 ) {
echo "<h1>".$categoryname."</h1>";
while($row2 = mysql_fetch_array($result2)) {
$advertisername = $row2['name'];
echo $advertisername. "<br />";
}

} else {
echo "No advertisers found for this category.";
}
}
?>


Thanks,
Donna

BWiz
07-27-2008, 04:10 PM
I think you are looking for CSS. You can't "flow" columns using PHP - you can retrieve the data which is to be in the columns - but you can't really position them without CSS.

dmac68
07-27-2008, 09:30 PM
Actually, I saw something, I'm just not sure how to apply. Here's the code I found:



$query = "SELECT stuff, morestuff FROM mystuff ORDER BY stuff";
$result = mysql_query($query);

$num_rows = mysql_num_rows($result);


$rows = ceil($num_rows / $columns);

while($row = mysql_fetch_array($result)) {
$data[] = $row['stuff'];

//store the other field into an array
$data2[] = $row['morestuff'];
}

echo "&lt;TABLE BORDER=\"0\"&gt;\n";

for($i = 0; $i &lt; $rows; $i++) {

echo "&lt;TR&gt;\n";

for($j = 0; $j &lt; $columns; $j++) {
if(isset($data[$i + ($j * $rows)])) {
echo "&lt;TD&gt;" . $data[$i + ($j * $rows)] . "&lt;/TD&gt;\n";

//echo out the field
echo "&lt;TD&gt;" . $data2[$i + ($j * $rows)] . "&lt;/TD&gt;\n";
}
}
echo "&lt;/TR&gt;\n";
}
echo "&lt;/TABLE&gt;\n";
?>


Any direction on how I can work that with what I have?

So, it would be pretty much like this on display

Cate1 CatNumber?
Name Name
Name Name
Cate2 CatNumber?
Name Name
Cate3 CatNumber?
Name Name
Name CatNumber?
Cate4 Name
Name Name
Cate5
Name


Thanks,
Donna

BWiz
07-27-2008, 09:53 PM
Why not do exactly what you posted on the bottom of your last post? Use a switch statment, processes everything which falls under that specific case, and then move on to the next?

dmac68
07-27-2008, 10:12 PM
I'm not exactly sure what that means:o

I'm very new to this.

I tried to combine like below, but got no output



<?php require_once("includes/connection.php"); ?>

<?php
$columns = 4;
$query = "select * from category";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);

$rows = ceil($num_rows / $columns);

while($row = mysql_fetch_array($result)) {
$categoryid = $row['id'];
$categoryname = $row['category'];
$query2 = "select * from advertisers where category_id='$categoryid' and active=1";
$result2 = mysql_query($query2);

echo "&lt;TABLE BORDER=\"0\"&gt;\n";

for($i = 0; $i &lt; $rows; $i++) {

echo "&lt;TR&gt;\n";

for($j = 0; $j &lt; $columns; $j++) {
if(isset($query[$i + ($j * $rows)])) {
echo "&lt;TD&gt;" . $query[$i + ($j * $rows)] . "&lt;/TD&gt;\n";

//echo out the field
echo "&lt;TD&gt;" . $query2[$i + ($j * $rows)] . "&lt;/TD&gt;\n";
}
}
echo "&lt;/TR&gt;\n";
}
echo "&lt;/TABLE&gt;\n";
?>


Thanks,
Donna

BWiz
07-27-2008, 10:27 PM
What I'am saying, is to use a switch statment, something like this:
switch($VarThatSaysWhichRow)case 'category1': // do something break;case // and so onRearrange your code so it processes your data case by case.
EDIT:Sorry, I'm on my smartphone right now, so I can't really help you with code, just theory.

dmac68
07-27-2008, 10:29 PM
You are quick, Bwiz.

I'll try to figure out what you posted.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum