The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Multidimensional array

Eric's Avatar

Eric

27 Jan, 2012 04:54 PM

I posted a question here about multidimensional arrays, but don't see that it registered to the forum. I wanted to make sure I was not wasting anyone's time with my question. I was able to get "$app->view()->setData()" to accept a multidimensional array by constructing it in "straight" PHP, then assign it to a "view" variable via the Slim ->setData method:

$i = 0; foreach( $pdo->query( $sql ) as $row ) {

$query_results[$i] = array(
    'partnumber' => $row['PartNumber'],
    'manufacturer' => $row['Manufacturer'],
    'description' => $row['Description']
);
$i += 1;

} $app->view()->setData( array( 'results' => $query_results ));

This does work (I can access in my view as "$results[X]['partnumber']" for example.) Since I am posting, is this the correct method for doing this, or is it more of a workable hack and not the best Slim way?

Since I am asking, ->setData appears to overwrite values. I have confirmed that it clears any previously set "view" variables, even if you are using a new variable name. Is this because these variables persist, and by clearing all the "view" variables on a ->setData it clears everything out (good for memory issues)? I have tested ->appendData and that just adds the new variable to what is available. I guess I am just wondering if I understand correctly the value of ->setData, if what I am thinking is why it does what it does. Is there any other way to clear all the "view" variables without setting a new one? Just curious.

Thanks!
Eric

  1. Support Staff 1 Posted by Josh Lockhart on 01 Feb, 2012 01:43 PM

    Josh Lockhart's Avatar

    Hi Eric,

    Good questions. Currently, you have setData() and appendData(). The setData() method accepts one or two arguments. If one argument, that argument must be an associative array and that array will overwrite any existing information; I do not recommend this in most scenarios. If you pass two arguments, the first is the key and the second is the new value (effectively setting/creating a single variable in the View).

    The appendData() method accepts an associative array and appends/merges data into the View's existing data.

    There is no way to clean a View's variables other than setData(array()).

    Usually, however, you pass the data into the View upon render like this:

    $app->render('template-name.html, array('foo' => 'bar'));
    

    The second argument will be assigned to the View automatically and made accessible to your template.

  2. 2 Posted by Eric on 01 Feb, 2012 04:03 PM

    Eric's Avatar

    Thanks Josh. I am new to this, and cannot even really call myself a PHP programmer (though learning), so sometimes not sure if I am doing things right. I'll go ahead and close this thread.

  3. 3 Posted by Eric` on 01 Feb, 2012 04:04 PM

    Eric`'s Avatar

    Nevermind, looks like I can't. Thanks though for answering my question.

  4. Josh Lockhart closed this discussion on 25 Mar, 2012 03:15 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac