Categories > TinyButStrong general >

block loop and php conditional depending on the value inside loop

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: hozyali
Date: 2010-08-03
Time: 22:51

block loop and php conditional depending on the value inside loop

Hi,

I am trying to achieve this.

sqlquery
then the while loop {

if (fieldval = 1) {
$ImageFolder = 'Directory1';
} else {
$ImageFolder = 'Directory2';
}


so now the above code I already have in php works fine, but I need to implement this in TBS, as it uses MergeBlocks. So I really have no idea how to accomplish this.
The block loops in TBS, but it has to go back to PHP code between the loop and pass a parameter to find the image directory name and many other values depending on the ID from the loop.

If possible, please also advise PHP code changes above.

Thanks
Ali
By: hozyali
Date: 2010-08-03
Time: 23:00

Re: block loop and php conditional depending on the value inside loop

just to add to my above question,

I have to call function depending on the value of the field between the loop in template.

Please advise.

thanks
By: Skrol29
Date: 2010-08-03
Time: 23:58

Re: block loop and php conditional depending on the value inside loop

Hi Hozyali,

They are several way to do this.

- You can use parameters "if/then/else" in a the TBS field

- you can use parameter "ondata" which goes with a block definition, and will call a custom function. You can code a "ondata" function that will add a new column in the recordset for each record before is is merged in the template. See parameter "ondata" in the manual for more details.

- you can use parameter "onformat" whcih will call a custom function, such a fnuction can edit the value a the field. See parameter "onformat" in the manual for more details.
By: hozyali
Date: 2010-08-04
Time: 00:01

Re: block loop and php conditional depending on the value inside loop

Thanks for your help.

I saw the manual for onformat, but I am newbie so don't get it exactly.

Can you give me an example to call a function with a couple of parameters for the php function?

thanks a lot.
By: Skrol29
Date: 2010-08-04
Time: 01:44

Re: block loop and php conditional depending on the value inside loop

The manual gives an example.
And there is an example for "ondata" with the online examples, see section "event function".
By: hozyali
Date: 2010-08-04
Time: 10:07

Re: block loop and php conditional depending on the value inside loop

ok. I think I have got it worked partially.

Here is my template code

[BlkEvents.artist_id;onformat=TBS_ShowDetails]

The above function takes artist_id as the ID to execute the SQL query.  Below is the actual PHP function
function TBS_ShowDetails($id){
$GLOBALS['initiate_the_function'];
$Query = "select * from ".$GLOBALS['pre_tbl']."shows WHERE art_id = '$id' ";
$rs =  mysql_query($Query) or die(mysql_error());
return mysql_fetch_assoc($rs);
}

You can see that the above function now returns an array of recordset which I use to pull data from that row related to the given artist_id.

As this old function didn't work, I used MergeBlock also in this function to return a block, but I get this error on the browser

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


Can you please advise how can we execute a simple php function and get its field values in the TBS template back?

thanks
By: hozyali
Date: 2010-08-05
Time: 20:52

Re: block loop and php conditional depending on the value inside loop

can you be so kind to reply to my last post? its the only thing stopping me from moving on with my project.

Really appreciate your help.

thanks
By: Skrol29
Date: 2010-08-05
Time: 21:22

Re: block loop and php conditional depending on the value inside loop

hi Hozyali,

It's a bit hard to help you because you're not only a TBS newbie, but a PHP newbie above all. We've all been so.

Your function TBS_ShowDetails() that you've posted does not return a recordset, but a single record. That what mysql_fetch_assoc() does. You're supposed to close a recorset when you do not need it anymore, but it is not done. This is not a technical error but is not clean. It would be better if you use mysql_free_result($rs) before to leave your function.
Another strange thing is the line with $GLOBALS['initiate_the_function']. This is only a variable an you call it like it was a function. I'm afraid this line does nothing.

Now about TBS and your template.

The error you've got is because you've called MergeBlock() with the wrong arguments.
But in order to help you, I need to understand what you expect to have. Do you what to display all the information for only one artist id that you've got ?  Or is it to display the information for several artists?
Can you give an example of what result you want to have?

By: hozyali
Date: 2010-08-05
Time: 21:36

Re: block loop and php conditional depending on the value inside loop

hi Skrol29,

Many thanks for trying to help.

The $GLOBALS['initiate_the_function'] variable is called to call the global variables from another file before it initiates the function.

It is called as a function because we needed to secure a few variables from open display in our code.

I can use free_results as its not a problem, however see my scenario what I need to do.

From start of the whole page,

there is a query runs and gets a loop, using MergeBlock, I get this fine.
Next step I do is the use block to do the loop in the TBS template.
After the loop is initiated, from each record, we get an artist_id.

This artist_id has its full record in another table, so in every loop, we need to call the PHP function to get the full details of this artist_id from its associated mysql table.

To help you better understand what is it, its a events listing page. All the events have artist_id in them and the artist data is coming from another table. so during the entire loop, I have to go to the PHP function every time to get the artist details from its own table.

Just so you know, this is my existing PHP application in which I need to integrate your TBS system. I would really appreciate if you can help with me with a few beginning procedure of TBS, and I will be fine to move on lonely.

Thanks again for your help.
By: Skrol29
Date: 2010-08-05
Time: 21:53

Re: block loop and php conditional depending on the value inside loop

> The $GLOBALS['initiate_the_function'] variable is called to call
> the global variables from another file before it initiates the function.

!!?? I juts don't understand. Calling a global variable does nothing at all.

>To help you better understand what is it, its a events listing page. All the
> events have artist_id in them and the artist data is coming from another
> table. so during the entire loop, I have to go to the PHP function every
> time to get the artist details from its own table

This is possible using parameter "ondata". I can explain how, but first of all, why don't you simply add the artist information in the main block with the main SQL query?

Like this:
PHP:
$sql = "SELECT event_id, event_title, artist_name, artist_coutry FROM t_event AS e LEFT JOIN t_artist AS a ON (e.artist_id=a.artist_id)"
$TBS->MergeBlock('BlkEvents', 'mysql', $sql);

HTML:
<tr>
  <td> [BlkEvents.event_title;block=tr] </td>
  <td> [BlkEvents.artist_name] from [BlkEvents.artist_country] </td>
</tr>
By: Skrol29
Date: 2010-08-05
Time: 21:54

Re: block loop and php conditional depending on the value inside loop

This is the fastest way because there is only one query call for the server.
By: hozyali
Date: 2010-08-05
Time: 21:59

Re: block loop and php conditional depending on the value inside loop

thanks a lot.

Can you explain the ondata as you mentioned you will explain it how?

Also yes we can do, but in some cases, there are same field names in both tables which we will be calling. so will it be a problem, or how do we call them?

For eg; there is a field called country in both tables and I need to display both country fields in different places in the same page, how will we call them?
By: Skrol29
Date: 2010-08-05
Time: 22:20

Re: block loop and php conditional depending on the value inside loop

> For eg; there is a field called country in both tables and
> I need to display both country fields in different places in the same
> page, how will we call them?

Use aliases in your SQL. Aliases are given using the reserved word "AS". In an SQL query, you can give aliases to tables in order to use a shorter name, and put table aliases before the column name to explicitly indicate what column you want. And you can give aliases to columns in the SELECT clause in order to rename them.

Th the following example, I use both column aliases and table aliases.
$sql = "SELECT e.id, e.title, e.country, a.name, a.country AS artist_country
FROM t_event AS e
LEFT JOIN t_artist AS a ON (e.artist_id=a.artist_id)";
$TBS->MergeBlock('BlkEvents', 'mysql', $sql);

The prefix of the columns, which are corresponding to table aliases, will not be return by MySQL. The column names of the query below will be :
id , title , country , name , artist_country

And the corresponding HTML could be:
<tr>
  <td> [BlkEvents.title;block=tr] </td>
  <td> [BlkEvents.name] from [BlkEvents.artist_country] </td>
</tr>

I think this is the better way for you. The "ondata" solution will produce as many SQL queries as they are events. 100 events => 100 SQL queries. This will be slow down your page.
By: hozyali
Date: 2010-08-05
Time: 22:24

Re: block loop and php conditional depending on the value inside loop

Hi,

I know about alias in the query and I do use them but in this case, this is a problem

There are 3 tables I have
events
artists
shows

The events table has 2 connections with the other 2 tables.

artist_id is the field connect with the other two tables, however we choose which table is it from ev_type field.
ev_type field tells us if the artist_id is in artists table or shows table.

So using the LEFT JOIN, I am not sure how do I check which table we have to go in and find the record, can you advise in this case?

Also, I would need to know the way to get the data out of the function because there are more than 100 functions I already have in my existing application and I will need still need to use them.

So kind of you.

thanks
Ali
By: Skrol29
Date: 2010-08-06
Time: 01:41

Re: block loop and php conditional depending on the value inside loop

Hi,

You could create an SQL query with two LEFT JOIN on both the Show table and the Artist table. Thus you will have both information for each event, and you could choose the columns to display depending to [ev_type].

Nevertheless, here is the solution with ondata.

The parameter "ondata" makes TBS to call your custom function for each record of the recordset to merge. Within this function, you can arrange all the record as you wish before it is merged. In the example below, a new column 'show' is added, this column contain an array with the Show information of the event. But please note that you could enhance this function to get more information, and change it depending to column [ev_type].

The ondata custom function, at the PHP side:
function f_blkevent_ondata($BlockName,&$CurrRec,$RecNum) {

  // get the show record for the current artist
  $Query = "SELECT date, description FROM ".$GLOBALS['pre_tbl']."shows WHERE art_id=".$CurrRec['artist_id'];
  $rs =  mysql_query($Query);
  $rec = mysql_fetch_assoc($rs);
  $rs = mysql_free_result($rs);

  // add the show record in a new column named 'show'
  $CurrRec['show'] = $rec;
}

Example of template that could display the information saved in the new column [show]:
<tr>
  <td> [BlkEvents.title;block=tr;ondata=f_blkevent_ondata] </td>
  <td> date: [BlkEvents.show.date] </td>
  <td> description: [BlkEvents.show.description] </td>
</tr>
By: Anonymous
Date: 2010-08-06
Time: 08:53

Re: block loop and php conditional depending on the value inside loop

thanks a lot. that was really the only thing I needed.

I understand that my own php code is not very optimized but it now gives me a signal to move on.

I really appreciate your help with the sql joins. and I will surely implement them.

Thanks again
By: hozyali
Date: 2010-08-06
Time: 21:29

Re: block loop and php conditional depending on the value inside loop

Hi,

This question is off topic, but I thought you have very good knowledge and may be able to help me with this too.

as you above mentioned, I can do 2 left joins, can you either give me tutorial link or any example so I can join multiple tables in my query?

I have about 6 tables to join in one statement. if you can just give me an example of 2, that would be great and I will handle the rest.

thanks a lot
By: hozyali
Date: 2010-08-06
Time: 21:39

Re: block loop and php conditional depending on the value inside loop

nevermind, I got it working.

thanks for your help