anybody tried to display a little table in a field? using foreach or i++?

Hello, i have control form, i’m using few fields as label… fields are set in events onLoad e.g.:

{field1}= $var1
{field2}= $var2
{field3}= “textHere”

Well, that works ok for single value text or number…

I want to display a table with about 10 rows and 4 columns, don’t know how to use foreach here

my code


sc_lookup(rs, "SELECT name, class, number, section FROM students_table");

{field4} = "<table><tr><th>Name</th><th>Class</th><th>Number</th><th>Section</th></tr><tr><td>".{rs[0][0]}."</td><td>".{rs[0][1]}."</td><td>".{rs[0][2]}."</td><td>".{rs[0][3]}."</td></tr></table>";

That works okay…

But i want to display all 10 rows without adding them one by one !! something like using foreach or $i++ but couldn’t figure it out

There must be a way to display this array nicely even without table!? just brain stops grrrrrrrrrrrr

anybody tried it?

If you want to display the table with all results from your select, you should loop the recordset.
Something like this (didn’t test it so likely it will be full of errors)


$tbl_header = "<tr><th>Name</th><th>Class</th><th>Number</th><th>Section</th></tr>";

$tbl_data = "";

$sql = "SELECT name, class, number, section FROM students_table";
sc_select(rs, $sql);
if (!(false == {rs} || {rs}->EOF))
{
    {rs}->MoveFirst();
    do
    {
        $tbl_data .= "<tr><td>" . {rs}->Fields('name')    . "</td>"
                       . "<td>" . {rs}->Fields('class')   . "</td>"
                       . "<td>" . {rs}->Fields('number')  . "</td>"
                       . "<td>" . {rs}->Fields('section') . "</td></tr>";
        {rs}->MoveNext();
    } while (!({rs}->EOF));
}

{field4} = "<table> $tbl_header $tbl_data </table>";

1 Like

thanks robydago, it works but only shows one row of the table!!

any hint?

Are you sure your query returns more than one record?

edit - Also, make sure you have a dot+equal here:


do
    {
        $tbl_data [B].=[/B] "<..........

Ooops you are right! there was where clause that limits my query result to one row :frowning:

Thanks dude, it works as expected.

Another related Q, can I format this table? it looks primitive? any chance to make it look like the grid colors? or at least align it nicely?

Mike sorry but I’m not good at that.

thanks roby you were so helpful already

Just wanted to the css tags that we can use for such occasion, ofcourse we can format using html in the event itself, but thought might be better way and include more modern table style same time

cheers dude