PDA

View Full Version : Columnizing


The-Ensemble
06-02-2008, 10:29 PM
I've NO idea what I am doing wrong here.

$counter = 0;

$result = mysql_query("query");

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

$columncount = '3';
$counter++;

$categoryname = $category['catname'];
$catid = $category['catid'];
$catdesc = $category['catdesc'];

eval('$cats .= "' . fetch_template('template') . '";');

if ($counter == $columncount)
{
echo '</tr><tr>';
$counter = 0;
}

}


It resets the counter to zero yet won't print </tr><tr> I've tried dump it with ?> <?php tags and that did nothing, I don't get what I've done wrong for it to not work. In theory it should all be working fine, but its not. :confused:

NOTE: I cut out the template name and query because they aren't really relevant they both work perfectly and this is a snippet of the overal code the rest of it isn't relevant to this part (or even executed yet) hence why I haven't posted it.

Opserty
06-02-2008, 10:40 PM
There is a closing curly brace (}) missing from your while() statement. Also you don't need the single quotes around the 3 in $columncount = '3';. Run
var_dump($cats); and throw in an echo statement into your while and see if you get any output. Also run var_dump() on $counter to see if it is changing.

Maybe check that you are getting results from the database first.

The-Ensemble
06-02-2008, 10:45 PM
The query is working fine, its returning all 4 records from the database with errors and the counter is working fine as I've put $counter in the template and its clearly stating 1-3 then resets to 1 without adding the </tr><tr> on 3.

See attached picture. Its returning the names, the descriptions and the counter.

MoT3rror
06-02-2008, 11:13 PM
Well if you are using templates you can't echo or print anything but what you can do is something like this.

$counter = 0;

$result = mysql_query("query");

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

$columncount = 3;
$counter++;

$categoryname = $category['catname'];
$catid = $category['catid'];
$catdesc = $category['catdesc'];

eval('$cats .= "' . fetch_template('template') . '";');

if ($counter == $columncount)
{
$cats .= '</tr><tr>';
$counter = 0;
}

}

The-Ensemble
06-02-2008, 11:28 PM
Oh wow, that works. Thanks!

Question: what does the . in .= do?

Dismounted
06-03-2008, 04:17 AM
Concatenate :)

Google it.

Opserty
06-03-2008, 09:25 AM
Always check the HTML source of the output when you're using PHP and things do go to plan. Sometimes the browser hides incorrectly placed code so you don't always see the exact output. It probably would have shown you your error. ;)

The-Ensemble
06-03-2008, 12:56 PM
Always check the HTML source of the output when you're using PHP and things do go to plan. Sometimes the browser hides incorrectly placed code so you don't always see the exact output. It probably would have shown you your error. ;)
It didn't output the </tr><tr> at all I put <!-- cat --> at the top and <!-- / cat --> at the bottom of the template to check if it was being outputted but in the wrong place. And It wasn't. But its working nicely now so thats all that matters. :up: