Categories > TinyButStrong general >

I would like a SECONDARY mysql merge block based on a while loop entry of another merge block?

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: desbest
Date: 2010-06-07
Time: 12:02

I would like a SECONDARY mysql merge block based on a while loop entry of another merge block?

This is the code that works well.
$TBS->MergeBlock("items",$conn,"SELECT * FROM items WHERE categoryid = $getcategory");

and it has a good job of looping the items with no problem.
         <tr id="itemid_[items.id;block=tr]">
                    <td height="30">
                    <!-- <img src="move.png" align="left" style="margin-right: 8px;"> -->
                    <div class="lowlight">[items.title]
                   
                   
                   
                    </div>
                   

                    </td>
                    <td height="30">
                    <div class="editable" itemid="$item[id]">[items.quantity]</div>

                    </td>
                    <td height="30">
                     <!-- <img src="pencil.png" id="edititem"width="24" height="24"> -->
        &nbsp; &nbsp; &nbsp; &nbsp;<img src="icons/folder.png" id="category_[items.categoryid]";" class="changecategory" categoryid="[items.categoryid]" itemid="[items.id]"  itemtitle="[items.title]" title="Change category" width="24" height="24">
                      &nbsp; &nbsp; &nbsp; &nbsp;  <a href="index.php?category=[var.getcategory]&deleteitem=[items.id]" title="Delete item" onclick="return confirm('Are you sure you want to delete [items.title]?')"><img src="icons/trash.png" id="deleteitem" width="24" height="24"></a>
              
                    </td>
        </tr>

This is where the problem lies.
In the table row I would like a secondary mysql merge block based on the id column that the primary (items) mysql table has.

This means that ideally I would love to do this.
$TBS->MergeBlock("fields",$conn,"SELECT * FROM items WHERE categoryid = [items.id]");

So then when I used [fields.value;block=div] inside the table row, it would pick up the a row from the fields table based on the primary mysql query. It would recognise what the id is for the merged block and perform a mysql query based on a variable that that block has.

So that's what I would like to do and that's what I call a secondary mysql merge block.
By: TomH
Date: 2010-06-07
Time: 12:57

Re: I would like a SECONDARY mysql merge block based on a while loop entry of another merge block?

Two examples on TBS site might be the way to do this
http://www.tinybutstrong.com/examples.php
See the "Sub-blocks" example which does actual subqueries
and the "Groupings" example which uses the parentgrp or headergrp techniques.

But really, just do the query something like "... ORDER BY categoryid ..." and you can do the "Groupings" technique and save doing all of the sub-queries.

By: desbest
Date: 2010-06-11
Time: 12:52

Re: I would like a SECONDARY mysql merge block based on a while loop entry of another merge block?

I'm having trouble putting this into practise using mysql.
I used the example and modified it.
This is the code I think should work but pulls no results and does not work.
===
index.php
===
<?php

require_once('../stockman-v3/tbs_class.php');
require_once('../stockman-v3/config.php');

// Create data
$TeamList[0] = array('team'=>'Eagle'  ,'total'=>'458'); //{
      $TeamList[0]['matches'][] = array('town'=>'London','score'=>'253','date'=>'1999-11-30');
      $TeamList[0]['matches'][] = array('town'=>'Paris' ,'score'=>'145','date'=>'2002-07-24');
      $TeamList[1] = array('team'=>'Goonies','total'=>'281');
      $TeamList[1]['matches'][] = array('town'=>'New-York','score'=>'365','date'=>'2001-12-25');
      $TeamList[1]['matches'][] = array('town'=>'Madrid'  ,'score'=>'521','date'=>'2004-01-14');
// }
$TeamList[2] = array('team'=>'MIB'    ,'total'=>'615'); // {
      $TeamList[2]['matches'][] = array('town'=>'Dallas'    ,'score'=>'362','date'=>'2001-01-02');
      $TeamList[2]['matches'][] = array('town'=>'Lyon'      ,'score'=>'321','date'=>'2002-11-17');
      $TeamList[2]['matches'][] = array('town'=>'Washington','score'=>'245','date'=>'2003-08-24');
// }

$TBS = new clsTinyButStrong;
$TBS->LoadTemplate('index3.html');

// Automatic subblock
$TBS->MergeBlock("item",$conn,"SELECT * FROM items");
$TBS->MergeBlock("field",$conn,"SELECT * FROM fields WHERE itemid='[%p1%]' ");
$TBS->MergeBlock('teamKEY',$TeamList);

// Subblock with a dynamic query
//$TBS->MergeBlock('match','array','TeamList[%p1%][matches]');
$TBS->MergeBlock("match",$conn,"SELECT * FROM fields WHERE id='[%p1%]' ");



$TBS->Show();

?>

===
index.html
===
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>TinyButStrong - Examples - subblocks</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="tbs_us_examples_0styles.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
.border02 {
    border: 2px solid #39C;
}
.border03 {
    border: 2px solid #396;
}

-->
</style>
</head>
<body>

<table width="400" align="center" cellpadding="5" cellspacing="0" style="border-collapse:collapse;">
  <tr>
   <td class="border02"><strong>Item name:</strong> [item.title;block=tr] ,
      <strong>Item quantity:</strong> [item.quantity]<br>
      <table border="1" align="center" cellpadding="2" cellspacing="0">
        <tr bgcolor="#CACACA">
          <td width="30"><u>Position</u></td>
          <td width="150"><u>Attribute</u></td>
          <td width="50"><u>Value</u></td>
          <td width="100"><div align="center"><u>Date Number</u></div></td>
        </tr>
        <tr bgcolor="#F0F0F0">
          <td>[field.#]</td>
          <td>[field.attribute;block=tr;p1=[item.id]]
          <br><b>[item.id]</b>
          </td>
          <td><div align="right">[field.value]</div></td>
          <td><div align="center">[field.datenumber;frm='mm-dd-yyyy']</div></td>
        </tr>
      </table>
  </td>
  </tr>
</table>
</body>
</html>

No results are shown for the field row when there are mysql results for these items.
By: desbest
Date: 2010-06-11
Time: 12:55

Re: I would like a SECONDARY mysql merge block based on a while loop entry of another merge block?

If I was to change
$TBS->MergeBlock("field",$conn,"SELECT * FROM fields WHERE itemid='[%p1%]' ");
to...
$TBS->MergeBlock("field",$conn,"SELECT * FROM fields");
It would show all the items.
By: Skrol29
Date: 2010-06-16
Time: 15:48

Re: I would like a SECONDARY mysql merge block based on a while loop entry of another merge block?

Hi Desbest,

I believe the sub-block feature should do what you need.
Can you give an example of how your wish the data to be presented after the merged, including your rows from the first query and those from the second query per id?
This will help us to show the right way to use sub-blocks in your case.
By: desbest
Date: 2010-06-17
Time: 06:59

Re: I would like a SECONDARY mysql merge block based on a while loop entry of another merge block?

Login to http://fresherplay.tk/stock/
Signup then login and then you will see a html demo of what I am doing.

Imagine a while loop which can happen because of a merge block.
This block is called [items] and it has an [items.id] variable.

I want to pass this [items.id] variable into the secondary mysql merge block so I can have a while loop inside a while loop. This secondary mysql merge block will check the fields table instead of the items table.

So it would go like this.

$TBS->MergeBlock("field",$conn,"SELECT * FROM fields WHERE itemid='[items.id]' ")
By: Skrol29
Date: 2010-06-17
Time: 11:16

Re: I would like a SECONDARY mysql merge block based on a while loop entry of another merge block?

Hi Desbest,

I've visited your html demo. I've seen simple merges but no illustration of what your expect to have.

The principle of nested loops you describe is theoretically done with TBS by the sub-block feature. Read the examples and the manual, you will see that it can be done for SQL queries using the wildcard %p1%, %p2%, ..  in the SQL, and parameters p1, p2, ... in the HTML.
By: desbest
Date: 2010-06-18
Time: 10:19

Re: I would like a SECONDARY mysql merge block based on a while loop entry of another merge block?

I need to know how to pass a variable from a primary mysql merge block such as [items.id]

and then use it in a mysql query.

I have read the manual and modifed the sub-blocks example and my failed attempt is described above.

How do I go about doing it?
By: Skrol29
Date: 2010-06-18
Time: 14:17

Re: I would like a SECONDARY mysql merge block based on a while loop entry of another merge block?

Hi Debest,

The modified example that you've posted seems good, but there is too much code and a small mistake.
Try:
<?php

require_once('../stockman-v3/tbs_class.php');
require_once('../stockman-v3/config.php');
$TBS = new clsTinyButStrong;
$TBS->LoadTemplate('index3.html');

$TBS->MergeBlock("item",$conn,"SELECT * FROM items");
$TBS->MergeBlock("field",$conn,"SELECT * FROM fields WHERE itemid=%p1%");

$TBS->Show();

?>

If you did not have any data for for the "field" rows, that was probably because you've coded "itemid='[%p1%]'" instead of "itemid=%p1%". The brackets are only when data comes from a PHP array.
If the columns itemid contains strings then you should code "itemid='%p1%'".
By: desbest
Date: 2010-06-18
Time: 15:00

Re: I would like a SECONDARY mysql merge block based on a while loop entry of another merge block?

Thanks alot for that. You are the only templating system that supports sub-blocks apart from smarty but that requires pear and it's bloated and crap like.
By: desbest
Date: 2010-06-18
Time: 15:25

IT WORKS!

I just tried what you said as in removing the brackets as it's not a php array and it works. Thanks alot.