[PHP]Smarty probleem met loopen

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

  • PSU_freak
  • Registratie: December 2005
  • Laatst online: 20:28
Ik ben een paar dagen aan het testen met smarty. Maar ik krijg het steeds niet voor elkaar om variabelen toe te wijzen in een for loop.

Ik heb in de PHP code verschillende variabelen die toegewezen moeten worden.
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
require '../Smarty/Smarty.class.php';
$smarty = new Smarty;
$smarty->template_dir = '../TPL';
$smarty->compile_dir = '../Smarty_user_data/templates_c/';
$smarty->config_dir = '../Smarty_user_data/configs/';
$smarty->cache_dir = '../Smarty_user_data/cache/';

//MySQL connect gedeelte
//
for($j=$begin+1; $info = mysql_fetch_object($query); $j++)
{
     $test = $info->var + 1;
     $test2 = $info->var2 -3;
     $test3 = $info->var3-5;
}
$smarty->display('layout.tpl');
?>


en nu wil ik in mijn template zoiets hebben:
code:
1
2
3
{section name=tabel loop=$test}
{$test}, {$test2}, {test3}
{/section}

In het bovenste gedeelte word dus elke keer variabel $test overnieuw gemaakt als ik dit stuk code gebruik:
code:
1
$smarty->assign('test',$test);


Ik heb de search functie gebruikt en daar vond ik ook een interessant topic maar dat is net een beetje anders dan wat ik wil (hier worden de variabelen niet meer geedit voordat ze toegewezen worden) ik krijg het niet voor elkaar om dit om te schrijven

Acties:
  • 0 Henk 'm!

Anoniem: 88197

Maak hier een array van? D.w.z. maak van test een array, in de for-loop in php de array vullen en vervolgens deze array toewijzen aan smarty. Daar kun je de array afdrukken :)

Acties:
  • 0 Henk 'm!

Anoniem: 111538

Bekijk eens de manual van smarty: http://smarty.php.net/man...uage.function.section.php
Daar staat het in, als voorbeeld:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php

$id = array(1001,1002,1003);
$smarty->assign('custid',$id);

$fullnames = array('John Smith','Jack Jones','Jane Munson');
$smarty->assign('name',$fullnames);

$addr = array('253 N 45th', '417 Mulberry ln', '5605 apple st');
$smarty->assign('address',$addr);

$types = array(
           array( 'home phone', 'cell phone', 'e-mail'),
           array( 'home phone', 'web'),
           array( 'cell phone')
         );
$smarty->assign('contact_type', $types);

$info = array(
           array('555-555-5555', '666-555-5555', 'john@myexample.com'),
           array( '123-456-4', 'www.example.com'),
           array( '0457878')
        );
$smarty->assign('contact_info', $info);

?>  
{*
  sections can be nested as deep as you like. With nested sections,
  you can access complex data structures, such as multi-dimensional
  arrays. In this example, $contact_type[customer] is an array of
  contact types for the current customer.
*}
{section name=customer loop=$custid}
<hr>
  id: {$custid[customer]}<br />
  name: {$name[customer]}<br />
  address: {$address[customer]}<br />
  {section name=contact loop=$contact_type[customer]}
    {$contact_type[customer][contact]}: {$contact_info[customer][contact]}<br />
  {/section}
{/section}

Acties:
  • 0 Henk 'm!

  • MueR
  • Registratie: Januari 2004
  • Laatst online: 16-06 13:10

MueR

Admin Tweakers Discord

is niet lief

Ik zal je smarty code ff omzetten naar php, dan zie je de fout. $test, $test2 en $test3 zijn arrays
code:
1
2
3
{section name=tabel loop=$test}
{$test}, {$test2}, {test3}
{/section}

PHP:
1
print ($test . ", " . $test2 . ", " . $test3);

Je wil dus doen:
PHP:
1
2
3
print ($test[0] . ", " . $test2[0] . ", " . $test3[0]);
print ($test[1] . ", " . $test2[1] . ", " . $test3[1]);
// Enz...


Je vergeet de index erbij te zetten.

Anyone who gets in between me and my morning coffee should be insecure.


Acties:
  • 0 Henk 'm!

  • Skaah
  • Registratie: Juni 2001
  • Laatst online: 06-06 09:54
Let op de blokhaken. Dat betekent: toevoegen aan array.
PHP:
1
2
3
4
5
6
7
$test = $test2 = $test3 = array(); // variabelen array maken
for($j=$begin+1; $info = mysql_fetch_object($query); $j++)
{
     $test[] = $info->var + 1;
     $test2[] = $info->var2 -3;
     $test3[] = $info->var3-5;
}

In Smarty doe je het vervolgens zo (met {section}):
HTML:
1
2
3
{section name=tabel loop=$test}
{$test[tabel]}, {$test2[tabel]}, {test3[tabel]}
{/section}

[ Voor 17% gewijzigd door Skaah op 06-01-2006 12:00 ]