Categories > TinyButStrong general >

Help with ondata= and mysql query

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: BobC
Date: 2005-12-29
Time: 02:09

Help with ondata= and mysql query

I would like to take a value of a database field named "Status" and use its value to determine the value of what is placed in the table.  Below is the html code and php code snippets..  Can this be done.  I know there is a parameter to use the "if" condition in the html code, but I want to stay away from that if I can and actually keep all the code in the php file.

//html code

<td><div align="right">[blk1.Status;block=tr;ondata=change_status;.]</div></td>


//Partial PHP code

$TBS->MergeBlock('blk1',$cnx_id,'SELECT Level1Name, Level2Name, Level3Name, Level4Name, Level5Name, Level6Name,Level7Name, Level8Name, Status, id FROM Organizations');

//and the function

function change_status($BlockName,&$CurrRec,$RecNum) {
    if ($Status='Disable') $Status='Changed Disable';
    if ($Status = 'Active') $Status ='Changed Active';
//the hope here is to change the value that comes from the mysql //database from either Disable or Active to something else when it is //displayed in the htmle table
By: Skrol29
Date: 2005-12-29
Time: 14:41

Re: Help with ondata= and mysql query

Hello,

Yes, this is possible. Values of current record are available througth the local variable $CurrRec. Thus, your code should be:
function change_status($BlockName,&$CurrRec,$RecNum) {
  if ($CurrRec['Status']=='Disable') $CurrRec['Status'] = 'Changed Disable';
  if ($CurrRec['Status']=='Active') $CurrRec['Status'] = 'Changed Active';
}
By: BobC
Date: 2005-12-29
Time: 18:27

Re: Help with ondata= and mysql query

I still can't make this work even with the changes you proposed.. my full code is as follows

html - basically from TBS example page.  I have an if statement there on alternate rows as I had that working but would like to push it to the php side as I previously mentioned.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>TinyButStrong</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">
</head>
<body>
<p align="center" class="title-page">Example of MySQL Data Merge</p>
<table border="1" align="center" cellpadding="2" cellspacing="0">
  <tr bgcolor="#CACACA">
    <td width="50"><strong>Position</strong></td>
    <td width="100"><strong>Level1Name</strong></td>
    <td width="100"><strong>Level2Name</strong></td>
    <td width="100"><strong>Level3Name</strong></td>
    <td width="100"><strong>Level4Name</strong></td>
    <td width="100"><strong>Level5Name</strong></td>
    <td width="100"><strong>Level6Name</strong></td>
    <td width="100"><strong>Level7Name</strong></td>
    <td width="100"><strong>Level8Name</strong></td>
    <td width="80"><strong>Status</strong></td>
    <td width="80"><strong>ID</strong></td>
  </tr>

  <tr bgcolor="#F0F0F0">
    <td>[blk1.#]</td>
    <td><div align="right">[blk1.Level1Name;block=tr;.]</div></td>
    <td><div align="right">[blk1.Level2Name;block=tr;.]</div></td>
    <td><div align="right">[blk1.Level3Name;block=tr;.]</div></td>
    <td><div align="right">[blk1.Level4Name;block=tr;.]</div></td>
    <td><div align="right">[blk1.Level5Name;block=tr;.]</div></td>
    <td><div align="right">[blk1.Level6Name;block=tr;.]</div></td>
    <td><div align="right">[blk1.Level7Name;block=tr;.]</div></td>
    <td><div align="right">[blk1.Level8Name;block=tr;.]</div></td>
    <td><div align="right">[blk1.Status;block=tr;ondata=change_status;.]</div></td>
    <td><div align="right">[blk1.id;block=tr;.]</div></td>
  </tr>

  <tr bgcolor="#E6E6E6">
    <td>[blk1.#]</td>
    <td><div align="right">[blk1.Level1Name;block=tr;.]</div></td>
    <td><div align="right">[blk1.Level2Name;block=tr;.]</div></td>
    <td><div align="right">[blk1.Level3Name;block=tr;.]</div></td>
    <td><div align="right">[blk1.Level4Name;block=tr;.]</div></td>
    <td><div align="right">[blk1.Level5Name;block=tr;.]</div></td>
    <td><div align="right">[blk1.Level6Name;block=tr;.]</div></td>
    <td><div align="right">[blk1.Level7Name;block=tr;.]</div></td>
    <td><div align="right">[blk1.Level8Name;block=tr;.]</div></td>
    <td><div align="right">[blk1.Status;block=tr;if [val]='Active';then <input type="radio" checked="checked" value="Active" name="[blk1.id]" id="Active"/>Active<br/> <input type="radio" value="Disable" name="[blk1.id]" id="Disable"/>Disable<br/>;else <input type="radio" value="Active" name="[blk1.id]" id="Active"/>Active<br/> <input type="radio" value="Disable" checked="checked" name="[blk1.id]" id="Disable"/>Disable<br/>;.]</div></td>
    <td><div align="right">[blk1.id;block=tr;.]</div></td>
  </tr>

  <tr bgcolor="#E6E6E6">
    <td colspan="4" bgcolor="#FFCFB9">[blk1;block=tr;nodata]There is no data. </td>
  </tr>
</table>
<p align="center">There are [blk1.#] displayed lines.</p>
</body>
</html>


And here is the php code... less the connection strings.. I do make connections without a problem and generate a page, adding your code doesn't change the value of "Status", it just leaves it as when it is drawn from the mysql database.


include_once('../tbs/tbs_class.php5') ;

//Connexion to the database
$cnx_id = mysql_connect('server', 'user', 'password' ) ;
mysql_select_db('database',$cnx_id) ;

$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate('organizations.html') ;
$TBS->MergeBlock('blk1',$cnx_id,'SELECT Level1Name, Level2Name, Level3Name, Level4Name, Level5Name, Level6Name,Level7Name, Level8Name, Status, id FROM Organizations');
mysql_close($cnx_id);
$TBS->Show();

function change_status($BlockName,&$CurrRec,$RecNum) {
  if ($CurrRec['Status']=='Disable') $CurrRec['Status'] = 'Changed Disable';
  if ($CurrRec['Status']=='Active') $CurrRec['Status'] = 'Changed Active';
}

?>


I can due this in pure php by putting the result table into an array and then using the array values indivdually to merge.. but that seems to be a waist if it can be done as simple as above.. I just can't get it to work and don't know what the probelm can be... output remains "Active" and is not changed to "Changed Active" as it should with your code corrections.
By: Skrol29
Date: 2005-12-29
Time: 19:42

Re: Help with ondata= and mysql query

Hello BobC,

You've defined too much "block=tr" in you template. Only one is needed for each section. In the main <tr>, delete all parameters "block=tr" but the one with "ondata=change_status".

What happens to you is that the first "block=tr" met for the section is considered, block parameters placed in the next tags are ignored. So your parameter "ondata=change_status" is ignored.
By: BobC
Date: 2005-12-29
Time: 20:13

Re: Help with ondata= and mysql query

And it works!! Thank you... I read about those in prior posts, but did not make the connection to my problem.... Wonderful!  Thanks Again!