Rendering AJAX result in a view

This question probably has been asked and answered hundred of times but after analysing answers found in the forum I’m not even a bit closer to the answer (incorrectly searching?). That’s why I’m asking.

To make this short. I have a DB query in action that returns querying result in $rows array. This action is called via AJAX:


$sql = 'SELECT * FROM TABLE(LHTML.GetWynList('.$id.'))';


$connection = Yii::app()->db;

$command = $connection->createCommand($sql);

$dataReader = $command->query();

$rows = $dataReader->readAll();


print_r($rows); //Works fine

echo($this->renderPartial('_wynik_main', array('rows'=>$rows), TRUE)); //Does not work


app()->end();

If I use first approach (printing array with print_r) everything is fine -> AJAX result is inserted into proper part of main view.

But if I use second approach (attempt to put array into renderPartial and then return it), AJAX action returns empty string (or at least nothing is inserted to DIV that is supposed to hold AJAX request result).

I’ve tried both:


echo($this->renderPartial('_wynik_main', array('rows'=>$rows), TRUE));

and:


$this->renderPartial('_wynik_main', array('rows'=>$rows));

What am I missing?

Show your _wynik_main file, I think an error must be there. I created two views and all works fine to me:


public function actionAjaxAction()

{

	$files = array(4, 5, 6);

	$this->renderPartial('aaa', array('files' => $files));

}


// aaa

<?php

	echo $this->renderPartial('bbb', array('files' => $files), true);

?>


// bbb

<?php

	echo '123'; print_r($files);

?>


// output

123Array

(

    [0] => 4

    [1] => 5

    [2] => 6

)



I would first of all check what’s actually returned from the Ajax call. Firebug Console is a suitable tool.

/Tommy

Thanks for your effort in testing this for me, frantic! :]

The problem is that this is a very early stage of developing this part of my project (and my first attempt to use renderPartial in AJAX requests) therefore my view file, you’re asking for contains nothing more than just:


<pre>print_r($rows);</pre>

So there shouldn’t be any problem here.

Actually I was moving this line between controller and view to test that it works like a charm in first version, while it doesn’t in second one.

Thanks! Seems like a deeper problem, while I was assuming that I made some very obvious mistake.

I didn’t think about testing it with Firebug, because I assumed (maybe mistakenly) that, if it works with print_r then the problem is in Yii code itself, not in AJAX request. I’ll test it at my work (currently have no access to this project) and write here, if I get to any conclusions.

Problem solved, it was something, what we call a Czech mistake in Polish - a very simple, but yet hard to notice error. My view file is:


<pre><?php print_r($rows) ?></pre>

I was playing with surrounding it with echo for some minor tests and for that reason I had to add second parameter to print_r - TRUE - to force result to be returned instead of being printed. I then remove echo but forgot to remove that TRUE, resulting in function returning its result out into space.