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
 
i would be happy to write a function that allows newspaper style columns if you'd like.
 
Thanks for the suggestion, jet042.

That would be wonderful, jpadie. Hope I can figure it out [blush]

Thanks,
Donna
 
here you go. completely untested so i have no idea whether it will work (it's late here)!

it does not do your category name filtering but that's easy enough to add if you want it.

this uses tables - there's no other way to achieve snaked columns without javascript, until such time as the css3 multi-column style is widely adopted by browsers. don't hold your breath...

Code:
<?php
$query = "select id, description from table order by id asc";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result, MYSQL_NUM)){
	$data[] = $row; //just select the data that you want, in the order that you want it. filter here if necessary
}
//now prescribe the format inside each cell for the data
$format = "<a href=\"somepage.php?id=%s\">%s</a>"; //use %s (or other print_f notation) for variable placeholders

echo "<table>";
$rows = new newsPaperColumns(4, $data, $format);
$rows->outputRows();
echo "</table>";


class newsPaperColumns{
	
	private $columns = array();
	
	private $numCols;
	private $numRows;
	private $data = array();
	
	public function __construct($numCols, $data, $format){
		$this->numCols = $numCols;
		$this->loadData($data);
	}
	
	private function getRows(){
		$numItems = count($this->data);
		$this->numRows = ceil($numItems/$this->numCols);
	}
	
	private function buildCells(){
		$cnt = 0;
		for($i=0; $i<$this->numRows; $i++){
			for ($j=0; $i<$this->numCols; $j++){
				if (!isset($this->data[$cnt])){
					$this->cell[$i][$j] = "<td>" . vsprintf($this->format, $this->data[$cnt]) . "</td>";
					$cnt++;
				} else {
					$this->cell[$i][$j] = "<td>&nbsp;</td>";
				}
			}
		}
	}

	private function loadData($data){
		$this->data = $data;
	}
	
	public function outputRows(){
		$table = '';
		foreach ($this->cell as $row){
			$table .= "\r\n\t<tr>";
			$table .= implode("\r\n\t\t", $row);
			$table .= "\r\n\t</tr>";
		}
		echo $table;
	}
	
}
 
jpadie said:
this uses tables - there's no other way to achieve snaked columns without javascript, until such time as the css3 multi-column style is widely adopted by browsers. don't hold your breath...
actually, i read that safari (webkit) and Mozilla browsers do support most of the necessary multi-column declarations. IE does not support any multi-column bits and bobs, even in IE 8 ( how silly. but then again IE6 and IE7 between them still represent 52% of all browser usage so we can't ignore them yet as developers. (
 
Thanks, jpadie. I had read a bit about css3, but didn't see enough browser support out there (not like I'm the expert) without a lot of javascript.

I'm just plugging away to figure out how to get the data from your generous post.

Donna
 
Donna

what do you mean by this
Donna said:
I'm just plugging away to figure out how to get the data from your generous post.

does the code not work (i did not test it) - in which case what error messages are you getting?

or is it that you cannot see how to rig it into your environment? or is it that you plain don't understand the code i posted?

with any of the above, we can help. just point us in the right direction.

you posted a link above to your current horizontal column attempt. so far as i can see, this still is not working because your css declarations are wrong. this will be because you are not specifying the number of columns properly.

this is a screen shot of your current site with the declarations shown on the left
dmcaster1.png


and here is the site with the declarations fixed. guess you can see the difference!
dmcaster2.png
 
Hi, jpadie. I did try a bit changing the widths for the horizontal style - but the advertisers aren't all under one category.

Newspaper columns is what I really want, but am having difficulty with what goes where
Code:
$date[] = $row;
figuring out what to get there. I've tried just putting one field name in the $data brackets, but get nothing for output except (when checking source) table tags. I've tried a few different ways, but can't seem to get it. I've been breaking out the dummies book but it doesn't help much. I've tried to determine the difference with using $row and where you have $rows, but didn't want to keep bothering everyone. I am trying to figure things out, just confuses a newbie like me. So in between I work on different projects I do understand and keep browsing posts.

Still no luck.
Basically I'm not understanding how to adapt to my needs.

Thanks,
Donna
 
well, first it should be $data (note the a at the end)
and second, this line should not need to be altered really.

if you want to limit the fields that get passed to the builder class then you should do so in the sql.

you then need to adapt the format string to take into account the different number of fields.

the dummies book is not going to help here. analyse each line and work out what it does and why it does it. use the manual (php.net) to inform yourself - not an abstracted book like dummies.

but from your last post, i think, again, that we've misunderstood you. is it that you want a separate newspaper column for each category of advertiser? if so does this not mean that you could end up having any number of vertical columns that would extend horizontally off the viewport? do you have an html or jpg mock up of what you are looking for? all these things are terribly simple to achieve but we (or I, for one) am guessing at what you want in the absence of precise descriptions from you.
 
Sorry, I meant $data

I'll ditch the book and try to use the manual.

No, I don't want a separate column for each category, just say four columns with the info divided among them.

Like
cat1 item cat6 cat8
item cat4 item item
item item cat7 item
cat2 item item cat9
item cat5 item item
cat3 item item item
item item item

Or however it ends up. I believe it will be 4 columns in the end - they keep changing depending on their design, but here's an example at 2 cols - just whatever it ends up being

Hope that is more clear.

Thanks,
Donna
 
much clearer. i will post back later (off to see Dark Knight...)
 
try this please Donna

Code:
<?php
$sql = "select * from advertisers a left join category c USING(category_id) 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());
while ($row = mysql_fetch_assoc($result)){
    $data[] = $row; //this puts all the rows into a an array of associative arrays
}
$rows = new newsPaperColumns(4, $data);

echo "<table>\r\n";
$rows->outputRows();
echo "</table>\r\n";


class newsPaperColumns{
    
    private $columns = array();
    
    private $numCols;
    private $numRows;
    private $data = array();
    
    public function __construct($numCols, $data){
        $this->numCols = $numCols;
        $this->data = $data;
    }
    
    private function getRows(){
        $numItems = count($this->data);
        $this->numRows = ceil($numItems/$this->numCols);
    }
    
    private function buildCells(){
        $cnt = 0;
        for($i=0; $i<$this->numRows; $i++){
            for ($j=0; $i<$this->numCols; $j++){
                if (!isset($this->data[$cnt])){
                	if ($this->curCategory !== $this->data[$cnt]['category_id']){
                		$cell = <<<HTML
		<td>
			<div class="categoryName">
				$this->data[$cnt]['category_name']
			</div>
			<div class="advertiser">
				<a href="category.php?cid={$this->data[$cnt]['category_id']}#advertiser_{$this->data[$cnt]['advertiser_id']}">
	                {$this->data[$cnt]['advertiser_name']}
	            </a>
			</div>
		</td>
	
HTML;
                	} else {
                		$cell = <<<HTML
		<td>
			<div class="advertiser">
				<a href="category.php?cid={$this->data[$cnt]['category_id']}#advertiser_{$this->data[$cnt]['advertiser_id']}">
	                {$this->data[$cnt]['advertiser_name']}
	            </a>
			</div>
		</td>
		
HTML;
                	}
                    $cnt++;
                } else {
                    $cell = "<td>&nbsp;</td>";
                }
				$this->cell[$i][$j] = $cell;
            }
        }
    }
    
    public function outputRows(){
        $table = '';
        foreach ($this->cell as $row){
            $table .= "\r\n\t<tr>";
            $table .= implode("\r\n\t\t", $row);
            $table .= "\r\n\t</tr>";
        }
        echo $table;
    }
}
 
Hi, jpadie -

I'll try this evening as soon as I get home from work.

Hope you enjoyed the movie.

Thanks,
Donna
 
Donna

my apologies, I omitted a call to an internal method in the above class.

please change the outputRows() method to the following
Code:
    public function outputRows(){
   [red]$this->getRows();
        $this->buildCells();[/red]
        $table = '';
        foreach ($this->cell as $row){
            $table .= "\r\n\t<tr>";
            $table .= implode("\r\n\t\t", $row);
            $table .= "\r\n\t</tr>";
        }
        echo $table;
    }

without these two lines the class won't actually output anything and you'll be left with just table tags.

the film was great. i saw it in French but will checkout the english version as soon as i have a sec.
 
Hi - I was looking at it last evening and was still just getting
Code:
<table></table>

I updated from your post above this morning (it's about 6:30 here) and now I only get the opening table tag.

I don't know where the rest went - not that there was much [ponder]

Donna
 
Oh - is there supposed to be $cell named in the class properties somewhere?

Thanks,
Donna
 
can you provide some dummy data in sql insert format? i suspect that there is a syntax error somewhere.
 
thanks. that helped debug things. although i should have spotted the errors earlier!

try this code for size

Code:
<?php

$sql = "select * from advertisers a left join category c USING(category_id) 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());
while ($row = mysql_fetch_assoc($result)){
    $data[] = $row; //this puts all the rows into a an array of associative arrays
}
$rows = new newsPaperColumns(4, $data);
echo <<<CSS
<style type="text/css">
	body {font-family:verdana; font-size:0.9em;}
	.categoryName {background-color: grey;}
	.advertiser {background-color: lightblue;}
	td {padding-top: 5px; padding-left: 5px;}
	td {background-color: lightblue;}
	table, tr, td {border-collapse:collapse;}
</style>

CSS;
$rows->outputTable();

class newsPaperColumns{
    
    private $columns = array();
    
    private $numCols;
    private $numRows;
    private $data = array();
    
    public function __construct($numCols, $data){
        $this->numCols = $numCols;
        $this->data = $data;
    }
    
    private function getRows(){
        $numItems = count($this->data);
        $this->numRows = ceil($numItems/$this->numCols);
    }
    
    private function buildCells(){
        $cnt = 0;
		$curCategory = '';
		for ($col=0; $col < $this->numCols; $col++){
			for($row=0; $row < $this->numRows; $row++){
				$cell = '';
				if (isset($this->data[$cnt])){
					$cell .= "\t\t<td>\r\n";
					if ($curCategory != $this->data[$cnt]['category_id']){
						$cell .= <<<HTML
            <div class="categoryName">
                {$this->data[$cnt]['category_name']}
            </div>

HTML;
						$curCategory = $this->data[$cnt]['category_id'];
					} //end category if
					$cell .= <<<HTML
            <div class="advertiser">
                <a href="category.php?cid={$this->data[$cnt]['category_id']}#advertiser_{$this->data[$cnt]['advertiser_id']}">
                    {$this->data[$cnt]['advertiser_name']}
                </a>
            </div>
        </td>

HTML;
				} else {
					//create a stub cell
					$cell = "<td>&nbsp;</td>";
				}
				
				
				$this->cell[$row][$col] = $cell; //"<td>Col $col<br/>Row $row<br/>Counter:$cnt</td>";	
				$cnt++;
			}
		}
		//echo "<pre>".print_r ($this->cell, true) ."</pre>";
    }
    
    public function outputTable(){
    	$this->getRows(); 
		$this->buildCells();
	
        $table = '<table>';
        foreach ($this->cell as $row){
            $table .= "\r\n\t<tr>";
            $table .= implode("\r\n\t\t", $row);
            $table .= "\r\n\t</tr>";
        }
		$table .= "</table>";
		echo $table;
    }
}

i've also thought of a way of doing this with pure css if you are interested.
 
here's a css method
Code:
<?php

$sql = "select * from advertisers a left join category c USING(category_id) 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());
while ($row = mysql_fetch_assoc($result)){
    $data[] = $row; //this puts all the rows into a an array of associative arrays
}
$rows = new newsPaperColumnsCSS(4, $data);
echo <<<CSS
<style type="text/css">
	body {font-family:verdana; font-size:0.9em;}
	.categoryName {background-color: grey;}
	.advertiser {background-color: lightblue;}
	.wrapper {width: 850px; min-width:850px; overflow:auto; background-color:lightblue; margin:0 auto; border:thin grey solid;}
</style>

CSS;

$rows->outputCols();

class newsPaperColumnsCSS{
    
    private $columns = array();
    
    private $numCols;
    private $numRows;
    private $data = array();
    
    public function __construct($numCols, $data){
        $this->numCols = $numCols;
        $this->data = $data;
    }
    
    private function getRows(){
        $numItems = count($this->data);
        $this->numRows = ceil($numItems/$this->numCols);
    }
    
    private function buildColumns(){
        $cnt = 0;
		$curCategory = '';
		$this->output = '';
		while ($cnt < count($this->data)):
			$this->output .= <<<HTML

	<div class="column">

HTML;
			for($row=0; $row < $this->numRows; $row++ ):
				if (!isset($this->data[$cnt])) break(0);
				$item = <<<HTML
				
		<div class="itemHolder">

HTML;
				if ($curCategory != $this->data[$cnt]['category_id']):
						$item .= <<<HTML
						
            <div class="categoryName">
                {$this->data[$cnt]['category_name']}
            </div>

HTML;
					$curCategory = $this->data[$cnt]['category_id'];
				endif;
				
				$item .= <<<HTML
				
            <div class="advertiser">
                <a href="category.php?cid={$this->data[$cnt]['category_id']}#advertiser_{$this->data[$cnt]['advertiser_id']}">
                    {$this->data[$cnt]['advertiser_name']}
                </a>
            </div>
		</div><!--end of itemHolder -->

HTML;
				$cnt++;
				$this->output .= $item;
					
			endfor;
			$this->output .= "</div>";
		endwhile;
    }
    
    public function outputCols(){
    	$this->getRows(); 
		$this->buildColumns();
		echo "<div class=\"wrapper\">";
		echo $this->output;
		echo "</div>";
    }
}
 
Beautiful - Thanks, jpadie!!! [2thumbsup]

I'm going to study these to see how it all works.

Sorry to have such a long and painful thread.

Thanks again to all for the assistance.

Donna
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top