Categories > TinyButStrong general >

Merging Blocks and Null Values

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: IndridCold
Date: 2006-12-09
Time: 16:00

Merging Blocks and Null Values

Hi!

Is there a way of having a default value set merged with a block? What I am basically doing is getting different products from my database by section and sometimes the section is empty.

#    Get products - Just to test
    $query = mysql_query("SELECT *
                          FROM products
                          WHERE section = '$section'");

    if(mysql_num_rows($query))
    {
        while($row = mysql_fetch_assoc($query))
        {
            $products[] = $row;
        }
    }

    $TBS->LoadTemplate($theme_dir.'section_list.html');
    $TBS->MergeBlock('products', $products);
    $TBS->MergeBlock('blk', $m_sections);
    $TBS->Show();

But if the record set is empty I get:

TinyButStrong Error when merging block [products] : unsupported variable type : 'NULL'.

Wouldit be better only to merge if the result is not null or is there a clever way around this ?

James
By: IndridCold
Date: 2006-12-09
Time: 16:05

Re: Merging Blocks and Null Values

Just to add a note to this, I don't want to pass my query directly to TBS - the results are coming in as arrays from functions which is why I am not sure if the MySQL merge example in the manual is applicable in this case.
By: IndridCold
Date: 2006-12-09
Time: 16:08

Re: Merging Blocks and Null Values

ALSO, I am not using tables I am using div tags like so

<div class="productSummary">
    <div class="productThumb">
    &nbsp;
    </div>
    <div class="productDesc">
    &nbsp;
    </div>
    <div class="clear">&nbsp;</div>
</div>

Which needs to be repeated for each product in the array.
By: RwD
Date: 2006-12-09
Time: 19:04

Re: Merging Blocks and Null Values

In front of the if statement testing if there are any rows in the resultset add the following:
$products = array();
The problem is that you are not supplying tbs with any data on which it can base what to do. Perhaps on the tbs side a test could be added (if ( is_array() )) or whatever, but currently that is not the case so if you just make sure the variable is an array then everything will work out ok...
By: RwD
Date: 2006-12-09
Time: 19:12

Re: Merging Blocks and Null Values

Not using tables is not a goal on it's own. Use tables when you are displaying table data.

But I am not quite sure what this has to do with the question. Tbs does not care what kind of html markup you are using to merge the data into as long as the markers are there.

ps, css cannot solve all design problems. There are things tables can do which would be near impossible with css.
By: RwD
Date: 2006-12-09
Time: 19:19

Re: Merging Blocks and Null Values

Did you actually try it before you started using the mysql_fetch_assoc function? For as far as I can see there is no reason why passing the query directly to tbs would not produce the exact same result.

Try:
// suppose your connection id = $connId;

$query = mysql_query("SELECT *
        FROM products
        WHERE section = '$section'");

$TBS->LoadTemplate($theme_dir.'section_list.html');
$TBS->MergeBlock('products', $connId, $query);
$TBS->MergeBlock('blk', $m_sections);
$TBS->Show();
By: IndridCold
Date: 2006-12-09
Time: 19:22

Re: Merging Blocks and Null Values

Hi,

Thanks for your help so far. What I cannot get my head around is, if you have nested tags, do you say:

div class="productSummary">
    <div class="productThumb">
    &nbsp;
    </div>
    <div class="productDesc">
    &nbsp;
    </div>
    <div class="clear">&nbsp;</div>
</div>

[products;block=div;nodata]
<div>
test
</div>

But this would hide all the divs above as well, right ?
By: sheepy
Date: 2006-12-11
Time: 10:10

Re: Merging Blocks and Null Values

Yes it would hide the div, but you'll still get the error if you try to merge it, since it's displayed when the merge happens not at the location it is merged.  You've to merge it with condition

/* Either this */
if ($products !== NULL) $TBS->MergeBlock('products', $products);
/* Or this */
$TBS->MergeBlock('products', ($products)?$products:array() );

I too failed to see the reason of passing data instead of query, I use lots of functions of mysql, and I send them directly to TBS.

By the way, contrary to folklore, <table> is originally proposed to do table layout, instead of presentation of table data.