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!

No output for multiple columns 4

Status
Not open for further replies.

dmacster

Technical User
Jan 28, 2005
670
US
I've tried to add in some code from jpadie to the output I have that did work, but I get no output at all now. Can someone point out what I've screwed up, please?

Here 'tis
Code:
<?php require_once("includes/connection.php"); ?>

  <?php 
  $numColumns = 4;
  
   //CREATE THE SQL QUERY TO PULL ALL VISIBLE LISTINGS
   
   $sql = "select * from advertisers a left join category c USING(category_id) ";
   $sql .= "WHERE a.visible = 1 order by c.category_name asc, a.advertiser_name asc ";

   $result = mysql_query($sql);
   $count = mysql_num_rows($result);
   
   $currentCategory = "";
   
   //start output
if ($count > 0){
    echo "\r\n";
} else {
    die ('no records found');
}

   while($row = mysql_fetch_array($result)){
   
      //PRINT OUT THE CATEGORY NAME EVERY TIME IT CHANGES
          //start the inner loop
    for ($i=0; $i<$numColumns; $i++){
        if ($currentCategory == "" || $currentCategory != $row['category_id']){
         echo "<div class='categoryName' style='font-weight:bold;margin-top:8px;'>";
         echo "<a href='category.php?cid=".$row['category_id']."'>" .stripslashes($row['category_name'])."</a></div>";
         $currentCategory = $row['category_id'];
      }
	  else {
            //prevent broken end columns
            echo "\r\n";
        }
      
      //PRINT OUT THE ADVERTISER
      echo "<div class='advertiserName'>";
      echo "<a href='category.php?cid=".$row['category_id']."#advertiser_".$row['advertiser_id']."'>";
      echo stripslashes($row['advertiser_name']);
      echo "</a></div>";
   }
   
?>

Thanks,
Donna
 
You didn't close your while loop. Add a } to the end of the code an try again.

For future reference, you can check the syntax of your PHP code from the command line by opening up a terminal (or cmd.exe on Windows) and typing this at the prompt:

Code:
$ php -l name_of_your_script.php

That is a lower-case "L", by the way. You'll also want to be either in the directory where the script is stored (if PHP is set up as an environment variable on Windows) or in the directory where the PHP executable resides (in which case, use the absolute path to your script). This is called "lint" and it will either tell you you have a sytax error and about where to look for it or it will say you don't have any.
 
Thanks - that gets me output, but everything is just repeated four times -

So, the advertiser named Auto Gallery just looks like
Auto Gallery
Auto Gallery
Auto Gallery
Auto Gallery

and everything is still a single column.

I can get a single column as shown here

with the following
Code:
<?php 
  
   //CREATE THE SQL QUERY TO PULL ALL VISIBLE LISTINGS
   
   $sql = "select * from advertisers a left join category c USING(category_id) ";
   $sql .= "WHERE a.visible = 1 order by c.category_name asc, a.advertiser_name asc ";

   $result = mysql_query($sql);
   
   $currentCategory = "";
   
   while($row = mysql_fetch_array($result)){
   
      //PRINT OUT THE CATEGORY NAME EVERY TIME IT CHANGES
      if($currentCategory == "" || $currentCategory != $row['category_id']){
         echo "<div class='categoryName' style='font-weight:bold;margin-top:8px;'>";
         echo "<a href='category.php?cid=".$row['category_id']."'>" .stripslashes($row['category_name'])."</a></div>";
         $currentCategory = $row['category_id'];
      }
      
      //PRINT OUT THE ADVERTISER
      echo "<div class='advertiserName'>";
      echo "<a href='category.php?cid=".$row['category_id']."#advertiser_".$row['advertiser_id']."'>";
      echo stripslashes($row['advertiser_name']);
      echo "</a></div>";
   }
   
?>

But I can't make the output split across multiple columns. Can you point me further?

Thanks,
Donna
 
you have not advanced the cursor inside the for loop. so all you are doing is repeating the for loop four times on the same row.

try this code instead

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

$numColumns = 4;

//CREATE THE SQL QUERY TO PULL ALL VISIBLE LISTINGS

$sql = "select * from advertisers a left join category c USING(category_id) ";
$sql .= "WHERE a.visible = 1 order by c.category_name asc, a.advertiser_name asc ";

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

$currentCategory = "";

//start output
if ($count > 0){
echo "\r\n";
} else {
die ('no records found');
}

while($row = mysql_fetch_assoc($result)){

	 //PRINT OUT THE CATEGORY NAME EVERY TIME IT CHANGES
	//start the inner loop
	for ($i=0; $i<$numColumns; $i++){
		if ($currentCategory == "" || $currentCategory != $row['category_id'] || $row !== false){
			 echo "<div class='categoryName' style='font-weight:bold;margin-top:8px;'>";
			 echo "<a href='category.php?cid=".$row['category_id']."'>" .stripslashes($row['category_name'])."</a></div>";
			 $currentCategory = $row['category_id'];
		} else {
			//prevent broken end columns
			echo "\r\n";
		} //end of if
		
		//PRINT OUT THE ADVERTISER
		echo "<div class='advertiserName'>";
		echo "<a href='category.php?cid=".$row['category_id']."#advertiser_".$row['advertiser_id']."'>";
		echo stripslashes($row['advertiser_name']);
		echo "</a></div>";
		
		//advance the cursor
		$row = mysql_fetch_assoc($result);
	}
} // end the while loop
?>
 
Thanks, jpadie.

That gets single column of listings, but now the category is repeated above every entry. So, where it was

Restaurants
Half Full, Striper Bites and Kindle
Jake's Seafood
The Plaza Pub

Now it's
Restaurants
Half Full, Striper Bites and Kindle

Restaurants
Jake's Seafood

Restaurants
The Plaza Pub

I guess I have to look at implementing a table in there somewhere.

Donna
 
Your problem is here:
Code:
if ($currentCategory == "" || $currentCategory != $row['category_id'] [red]||[/red] $row !== false){
Try changing the line to:
Code:
if ( [red]([/red]$currentCategory == "" || $currentCategory != $row['category_id'][red]) &&[/red] $row !== false){
By saying OR the row doesn't equal false it is always running as the row is never equal to false until it runs out of rows.
 
Thanks, Borvik. That gets me back to just one category above advertisers, but still no multiple columns.

Donna
 
Try using css to float the class advertiserName or add a column class. You are just kicking them out in plain divs which are correctly displayed in the browser. How do you expect them to be in multiple columns?

e.g.
Code:
.advertiserName {
	width: 21%;
	float: left;
}

Greg
People demand freedom of speech as a compensation for the freedom of thought which they seldom use. Kierkegaard
 
I was trying to adapt from this post
But obviously have missed something to make the multiple columns.

Too new at this to have an understanding of the output side with php. Sorry if I am being extra dumb.

I'll try looking at your suggestion with a style from my css when I get home.

Thanks,
Donna
 
sure.

have another look at the code i posted. at the top you will find some style declarations. note that you are also mixing up the table and css style of columns in your code above.
i've not really spent a lot of time on this, but this mashup should work for you

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

$columns = 4;

//CREATE THE SQL QUERY TO PULL ALL VISIBLE LISTINGS
$sql = "select * from advertisers a left join category c USING(category_id) ";
$sql .= "WHERE a.visible = 1 order by c.category_name asc, a.advertiser_name asc ";

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

//prepare the css
$column_css = (100/$columns) - 2;
$css = <<<HTML
<style type="text/css">
.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;}
.categoryName {font-weight:bold;margin-top:8px;}
}
</style>

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

HTML;
//iterate the recordset
while ($row = mysql_fetch_assoc($result)){
	
	array_walk($row, 'stripslashes'); //you REALLY should not need this....
	$categoryName ='';
	if ( $currentCategory != $row['category_id']) {
		$categoryName = <<<HTML
	<div class='categoryName'>
		<a href="category.php?cid={$row['category_id']}">
			{$row['category_name']}
		</a>
	</div>

HTML;
		$currentCategory = $row['category_id'];
    }
    
    $output .= <<<HTML
    <div class="innerHolder">
		$categoryName
        <div class="itemHolder">
            <a href="category.php?cid={$row['category_id']}#advertiser_{$row['advertiser_id']}">
        		{$row['advertiser_name']}
        	</a>
        </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;
?>
 
Hmm - no output, but I'm unclear on something (well, at least one thing...) What does this mean?
Code:
<<<HTML

and then the end
HTML;

I've never seen that before (other that in your post(s).

Thanks,
Donna
 
Oh, and I don't see any
Code:
echo
anywhere. That the only way I've been learning to output stuff for the viewer.

Thanks,
Donna
 
you (obviously) need to echo $output at the end of the script!!

the <<<HTML is called the heredoc syntax. have a look in the manual - heredoc is really useful
 
Thanks, jpadie. I'll look into that. I've never heard of it, but will look right now.

Donna
 
I'm sorry to be so dense and questioning - I really could not have found the heredoc reference without knowing it's name.

Okay - it outputs on the actual server, but just not on my local. I may be confusing this, but I removed the } that was right before the ending of the style definitions.

Still one big column (see here
but I'm guessing since there's nothing set for style for itemHolder, I'm just getting an empty or nonstyled div wrapper. Would this be why just a single column?

This stuff was definitely not covered in the videos at lynda or in the dummies book.

Thanks,
Donna
 
You need to output (echo) $css variable as well. For the example you can just output it before the $output but on the actual page, you should include that in an external stylesheet that is called within the head of the document or at least output the styles within the head of a properly structured html document.

___________________________________________________________
[small]Do something about world cancer today: Comprehensive cancer control information at PACT[/small]
 
i don't think that you will be able to put it in the main css file as you need the columnar calculation to get the innerHolder width right (currently this is not working in your script). alternatively you could hard code it. essentially you want a wrapper div that is the width you want for the columns' container and then just less than 25% for each column (assuming 4). you need to be 'just less' in order to take account of margins and padding. for absolute control you can, of course, express the widths in absolute numbers of pixels.
 
Hey, jpadie. I believe you - I pretty much believe everyone right now. I'll be working on this stuff tomorrow to try to get the containers correct, and not to break the advertiser name away from the category.

I'd love to try to make it go down then over, but horizontal will work for me.

Thanks for everyone's patience. I do appreciate the assistance.

Donna
 
When I have a page that I want to display in an exact way, I typically mock it up with just HTML, CSS, and dummy data first. Then when I look at that code, I can usually identify places where my script can loop to create individual lines, especially with tables. Don't forget that you can nest loops, so if you have headers that will change infrequently that's one loop and the data for each header is another loop inside it.

These can get pretty complicated very fast, but it looks like you will be able to figure it out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top