Unable to access array in View passed in from Controller


At the moment, I am having trouble using the elements of an array that I pass into view from its controller. Here is the code for the controller

class FileController extends Controller{

	public function actionUpload()



	// create new model

	$model = new UploadFile();




   		$model->file = UploadedFile::getInstance($model, 'file');

   		// if upload is successful

   		if($name = $model->upload()){

   		       // create a new row for db to store the file;

   			$db = new Files();  // this model accesses the files table in the yii2basic database


   		        // attributes

	   		$db->size = $model->getSize(); 

	   		$db->type = $model->getType();

	   		$db->realname = $name;

	   		$db->created = date('Y-m-d h:m:s');


	   		// redirect to the same page to  avoid resubmission





 	// get the files logged in database, display it in upload view

   	$files = Files::find()->indexBy('id')->all();  

	// renders view 

      return $this -> render('upload', ['model' => $model], array('filesdb' => $files));



Here’s the code for the corresponding view


		<button class = "btn btn-default btn-sm" data-toggle = "modal" data-target = ".fileUpload">

			Add Document </button>

			<div class = "modal fade fileUpload">

				<div class = "modal-dialog">

				  <?php $form = ActiveForm::begin([

					// set other attributes for the form here if needed

					'options' => ['enctype' => 'multipart/form-data']

				]); ?>

   				<div class = "modal-content">

   					<div class = "modal-header">

   						<button type = "button" class = "close" data-dismiss = "modal">

   							<span aria-hidden = "true">&times; </span><span class = "sr-only"> Close</span>


   						<h5 class = "modal-title"> Add Document </h5>


   					<div class= "modal-body">

   					    <?= $form->field($model, 'file')->fileInput() ?> 


   					<div class= "modal-footer">

   						<input id = "file-upload" type="submit" name = "submit" value = "Send">

              <label for = "file-upload" class = "custom-upload">Send</label>  

   						<button type = "button" class = "btn btn-default btn-sm" data-dismiss = "modal">





   				<?php ActiveForm::end(); ?>





    <div class="container-fluid">

       <?php echo $filesdb; ?>



When using $filesdb in view I am getting the error [color="#FF0000"]undefined variable[/color] $filesdb. I have tried multiple methods of accessing the array such as the for each loop but none seems to work. I am not sure why it would be undefined. When using a simple variable instead of an array everything works.I am able to access $model and use it.

Thanks for the help!

     // renders view 

      return $this -> render('upload', ['model' => $model, 'filesdb' => $files]);

Try this above.

Hi, thanks for the input

When I try this out, I am getting the error [color="#FF0000"]Array to string conversion[/color]. Doesn’t seem to do the trick. Anymore ideas?

The proper way to send variables to the view is what itma wrote. The error you have got is because you try to echo the array. There are many ways to display array content. For example you can use foreach loop and echo every element.

Gotcha. Thanks for the clarification! It worked.