Author Topic: [Loops] one-dimensional arrays  (Read 1100 times)

0 Members and 1 Guest are viewing this topic.

Offline ClausVB

  • Administrator
  • Hero Member
  • *****
  • Posts: 566
    • Homepage: clausvb.de
[Loops] one-dimensional arrays
on: Sun, 16. June 2002 - 14:37:00
As far as I know, one-dimensional arrays cannot be integrated in loops.

A loop has to be:
Array (
[0] => Array (
[colname] => "FirstColumn"
[colname] => "SecColumn"
...
))

I know how it works now, but wouldn't it make sense to program a routine, which
can output a one-dimensional array directly?

like

$col = ("First", "Second", "Third");
into
<td><tmpl_var name='colName'></td>

I see the problem a user could not set his own variable name anymore, but you could use a dummy like "<tmpl_var name='array'>".

This would have a major advantage:
$row = mysql_fetch_array($result);
You could parse $row without further thinking.

--------

Congratulations on your work. I used SMARTY and FastTemplates, but imho your template-class is the best one and the best documented I have seen.

Best regards,
Claus

_________________
"Work is a drug that looks like medicine."
Tennesse Williams

PowerHelpDesk - HelpDesk Software, Ticket-Tracking, Support

[size=8][ This Message was edited by: ClausVB on 2002-06-17 09:41 ][/size]

releasedj

  • Guest
[Loops] one-dimensional arrays
Reply #1 on: Sun, 16. June 2002 - 20:40:00
This is a good suggestion.

There are a couple of problem I see with it like how the variables and the loop names are referenced in the templates, but I think I'll be able to figure out a way around it.

When I've envisioned the changes, I'll post back to the forum for feedback.

Thanks for your comments.

Kelvin

releasedj

  • Guest
[Loops] one-dimensional arrays
Reply #2 on: Tue, 18. June 2002 - 23:19:00
What would you think of this solution.

[!-- BBCode Start --][TABLE BORDER=0 ALIGN=CENTER WIDTH=85%][TR][TD][size=8]Code:[/size]
[/TD][/TR][TR][TD]


<?php

    include_once "../vlibTemplate.php";

    $tmpl = new vlibTemplate('./test.html');



    $q = "select username, firstname, lastname from users";

    $cnx = mysql_connect('localhost', 'user', 'password');

    $result = mysql_db_query('vlibdb', $q);



    // this is the new stuff

    $tmpl->newLoop('myloop'); // tells us the name of the new loop



    while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

        $tmpl->addRow($row); // this would add a row to the previously named loop

    }



    $tmpl->pparse();

?>

[/TD][/TR][TR][TD]
[/TD][/TR][/TABLE][!-- BBCode End --]



So this is an idea of how one could pass in a single dimensional array to a loop, making it easier to understand and program. The array could be an associative array like this:



[!-- BBCode Start --][TABLE BORDER=0 ALIGN=CENTER WIDTH=85%][TR][TD][size=8]Code:[/size]
[/TD][/TR][TR][TD]


array(

    'username' => 'pj',

    'firstname' => 'Peter',

    'lastname' => 'Johnson'

);

[/TD][/TR][TR][TD]
[/TD][/TR][/TABLE][!-- BBCode End --]



Nested loops would be to complicated using this structure, but then they're an advanced feature anyway.



Tell me what you think!



Kelvin

Offline ClausVB

  • Administrator
  • Hero Member
  • *****
  • Posts: 566
    • Homepage: clausvb.de
[Loops] one-dimensional arrays
Reply #3 on: Wed, 19. June 2002 - 12:31:00
Since

$row =
Array (
[0] => Nordheimer, Ute [name] => Nordheimer, Ute
[1] => Berlin [ort] => Berlin
[2] => Deutschland [land] => Deutschland )

Array (
[0] => Smith, Daniel [name] => Smith, Daniel
[1] => Salt Lake City [ort] => Salt Lake City
...

This should work perfectly, for mysql_fetch_array and ifx_fetch_row.
Both deliver associative arrays and so it should work fine with DB-output.

I wouldn't worry about nested loops, because you would not need them anymore for DB-outputs. I think this is a major concern, when using templates.

If you have a n-dimensional array you could use nested-loops again, but I never encountered one in my whole life.

Please tell me when I can test the function (and write docu on it :smile:).

Greetings
Claus
_________________
"Work is a drug that looks like medicine."
Tennesse Williams

PowerHelpDesk - HelpDesk Software, Ticket-Tracking, Support

[size=8][ This Message was edited by: ClausVB on 2002-06-19 12:36 ][/size]

releasedj

  • Guest
[Loops] one-dimensional arrays
Reply #4 on: Thu, 20. June 2002 - 11:27:00
This new functionality has been added, and a new version of vLIB (3.0.1) has been released with this functionality.



Take a look at the docs, but you'll probably get a better example using the new libTemplate_basicloop.php file in the examples section.



Regards,



Kelvin

Offline ClausVB

  • Administrator
  • Hero Member
  • *****
  • Posts: 566
    • Homepage: clausvb.de
[Loops] one-dimensional arrays
Reply #5 on: Thu, 20. June 2002 - 18:05:00
Thank your very much!

I will test it tomorrow.

CU
Claus