Using tinybutstrong templating system, how do I have a (secondary) mysql query (sub-block), inside a
Posted
by desbest
on Stack Overflow
See other posts from Stack Overflow
or by desbest
Published on 2010-06-14T12:19:37Z
Indexed on
2010/06/14
12:22 UTC
Read the original article
Hit count: 211
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"> -->
<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">
<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.
This is what I attempted by using the sub-blocks example and modifying it.
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>
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();
?>
please note what i am trying to do
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.
© Stack Overflow or respective owner