PHP-foreach not working inside template

192 views
Skip to first unread message

JamesHemmingworth

unread,
Mar 4, 2017, 3:02:12 PM3/4/17
to Fat-Free Framework
Okay, after i have set up most of my CMV, i tried to work with the f3-template-language, but i find it hard to work with it, especially with foreach() (in F3: repeat).

Im loading my database-entry over the models:

$query = $this->db->exec('SELECT * FROM '.$tablename.'');
       $result = $this->fetchAll($query);
       return $result;


and retrieve it with my mainController, who loads the default-components (navbar, navigation, site-title, etc.) and set it to my template via:

$this->f3->set('navigation', \View::instance()->render('default/navigation.php','text/html'));

Variable with array:

$this->f3->set('navigationLinks', (new MainModel)->getNavigationLinks());


After that, i tried var_dumping it and i retrieve the following:

array(7) { ["id"]=> string(1) "1" ["name"]=> string(12) "BeispielLink" ["url"]=> string(9) "google.de" ["hasIcon"]=> NULL ["icon"]=> NULL ["isSubGroup"]=> NULL ["subGroup"]=> NULL }

So, the array is getting send to the template and has my desired content in it.

But, for some reason, after i apply the following foreach (PHP) inside my template:

<?php foreach($navigationLinks as $links): ?>
<?php echo $links; ?>
<?php endforeach; ?>


Which should just output something (im not selecting something from the array) i get the following:

    Invalid argument supplied for foreach()

    [lib/base.php:2151] Base->error()
    [template/default/navigation.php:5] Base->{closure}()
    [lib/base.php:2656] require('template\default\navigation.php')
    [lib/base.php:2683] View->sandbox()
    [classes/Main.php:21] View->render()
    [classes/Presentator.php:7] Main->loadModulesForPage()
    [index.php:44] Presentator->__construct()
(I removed path at the beginning)

And after i put a var_dump in Front of the foreach, the Array is NULL.

Is it that im trying to assign a value to a variable which FatFree doesnt allow or is something else playing crazy here? Im not assigning anything else to it. Or does someone know how i can do the following:

<?php foreach($navigationLinks as $links): ?>
<li><?php if($links["hasIcon"]){...}<a href="<?php echo $links["url"]?>"><?php echo $links["name"]?></a></li>
<?php endforeach; ?>

With the repeat?

With greetings.

ved

unread,
Mar 4, 2017, 5:46:05 PM3/4/17
to Fat-Free Framework
Hi,
 
$query = $this->db->exec('SELECT * FROM '.$tablename.'');
       $result = $this->fetchAll($query);
       return $result;

What's that fetchAll doing? Just using the results from $query will probably solve your problem.

 
After that, i tried var_dumping it and i retrieve the following:

array(7) { ["id"]=> string(1) "1" ["name"]=> string(12) "BeispielLink" ["url"]=> string(9) "google.de" ["hasIcon"]=> NULL ["icon"]=> NULL ["isSubGroup"]=> NULL ["subGroup"]=> NULL }

So, the array is getting send to the template and has my desired content in it.

But is that array actually correct? Shouldn't it be a multidimensional array?
 

JamesHemmingworth

unread,
Mar 4, 2017, 6:13:36 PM3/4/17
to Fat-Free Framework

Hi,
 
$query = $this->db->exec('SELECT * FROM '.$tablename.'');
       $result = $this->fetchAll($query);
       return $result;

What's that fetchAll doing? Just using the results from $query will probably solve your problem.

 
After that, i tried var_dumping it and i retrieve the following:

array(7) { ["id"]=> string(1) "1" ["name"]=> string(12) "BeispielLink" ["url"]=> string(9) "google.de" ["hasIcon"]=> NULL ["icon"]=> NULL ["isSubGroup"]=> NULL ["subGroup"]=> NULL }

So, the array is getting send to the template and has my desired content in it.
But is that array actually correct? Shouldn't it be a multidimensional array?


Huh, after correcting out the "$result = $this->fetchAll($query);" my array is getting displayed correctly... Seems like the function somehow messed up the php-foreach so that when loading the array to the foreach the function doesnt recognize it as an array.

var_dump still show the same: array(7) { ["id"]=> string(1) "1" ["name"]=> string(12) "BeispielLink" ["url"]=> string(9) "google.de" ["hasIcon"]=> NULL ["icon"]=> NULL ["isSubGroup"]=> NULL ["subGroup"]=> NULL }

Well, that solves it now. Seems like its one of these type of things where a system just doesnt wanna work with something it does not expect.

Thank you anyway.

With greetings.
Reply all
Reply to author
Forward
0 new messages