Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I split this evenly between 4 columns 2

Status
Not open for further replies.

dmacster

Technical User
Jan 28, 2005
670
US
Way newbie here - I can connect to the mySQL db I have and output by category and any active names, but can I split that between 4 table columns without having the column split unevenly away from category? I mean so the if there are 4 columns evenly divided, there isn't an advertiser name in column two split from the category name. Kind of like a keep with next paragraph option in InDesign or some other print pagination program.

Here's my code to date

Code:
	<?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 "Advertiser is ".$advertisername. "<br />";
   		   }

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

If anything isn't clear, I'll be happy to try to explain. It's a bit fuzzy for me at this point.[blush]

Thanks,
Donna
 
Sorry, I can't edit my post. Here's what I've tried, but I get no output with this.

Code:
<?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
 
why not just use css?
if you get no output at all, it is often because your query is failing or returning zero rows. although in your case there are a bunch of coding errors that may also be culprits.

Code:
for($i = 0; $i &lt; $rows; $i++) {
i'm not sure whether you are overzealously converting the symbols to html codes or whether this is just a typo. anyway, in functions you need to use the actual symbol, not the html code equivalent.

the other weird bit is that you're treating $query as an array inside the loop. i'd guess you mean to refer to $row as $query appears to be the text of the first query.

and i also notice that you're not doing anything with $result2.

and you can't refer to individual rows within the resultset using array notation if you have not previously stored the resultset in an array. most people use a while loop to address rows or, if you must manipulate the cursor yourself, you can use mysql_result()
 
Wow - I have a lot to look at. Thanks for pointing that out.

I've been trying little bits and then adding to them, so I'm sure that's where I've come up with the extra result2.

I was trying to use a tutorial I found at

but obviously I've missed a lot.

I'm pretty frustrated now, but keep trying to slug on.

I've had such a variance in advice in dealing with this that I'm more than confused. I'll see if I can interpret your post in to what I'm trying to achieve and get this all going.

Thanks,
Donna
 
it's not too difficult. probably the explanation is more complex than the reality.

there are two methods of creating columns, one is the two sets of loops handling rows and then columns in a table. The other way is to use floated block level elements and css widths. For non-tabular data I prefer css as it degrades nicely and it's much easier.

here is a code block for the css method

Code:
<?php

$columns = 4;
$sql = "Select id, imgName, imgDescription from imageTable";

//perform the query
$result = mysql_query($sql) or die(mysql_error());

//prepare the css
$column_css = (100/$columns) - 1;
$css = <<<HTML
<style>
.contentHolder {width:85%; margin: 0 auto; text-align:center;}
.innerHolder {width: $column_css; margin-right: 1px; float:left;}
.clear {line-height: 0.5px; visibility: hidden; clear:both;}
}
</style>

HTML;
//prepare the output
$output = <<<HTML
<div id="contentHolder">

HTML;
//iterate the recordset
while ($row = mysql_fetch_assoc($result)){
	$output .= <<<HTML
	<div class="innerHolder">
		<div class="imageHolder">
			<img 	src="images/{$row['imgName']}" 
					alt="{$row['imgDescription']}"
					title="{$row['imgDescription']}" />
		</div>
		<div class="captionHolder">
			{$row['imgDescription']}
		</div>
	</div>
	
HTML;
}

//finish the output
$output .= <<<HTML
	<div class="clear">&nbsp;</div><!--- clear the floats for box model issues --->
</div> <!--- finish the contentHolder div --->

HTML;
?>
 
Thanks, jpadie.

I'll look at this today when I get home from work. I prefer the css method, but was going to be using tables to fit into the webmaster's design on this project.

Donna
 
here's some code for the double loop version using tables.

Code:
<?php
$numColumns = 4;
$sql = "select id, imgName, imgTitle from images";

//run sql
$result = mysql_query($sql) or die(mysql_error());
$count = mysql_num_rows($result);

//start the table output
if ($count > 0){
	echo "<table>\r\n";
} else {
	die ('no records found');
}
//start the outer loop
while ($row = mysql_fetch_assoc($result)){
	echo "\t<tr>\r\n";
	
	//start the inner loop
	for ($i=0; $i<$numColumns; $i++){
		if ($row !== false){
			echo <<<HTML
		<td>
			<div>
				<img src="images/{$row['imgName']}" title="{$row['imgTitle']}" alt="{$row['imgTitle']}"/>
			</div>
			<div>
				{$row['imgTitle']}
			</div>
		</td>
		
HTML;
		} else {
			//you need to do this to ensure we don't have a broken set of columns at then end
			echo "\t\t<td>\r\n&nbsp;\r\n\t\t</td>\r\n";
		}
		//now advance the row cursor again
		$row = mysql_fetch_assoc($result);
	}
	
	echo "\t</tr>\r\n";
}
//close the table
echo "</table>\r\n";
?>

 
You are wonderful. As soon as I can get out of here (day job), I'll look forward to going through this. Thanks for your patience.
[sunshine]

Donna
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top