assign values from action

hi

I have an action which is called by jquery and show xml




class ListAction extends CAction {

    public function run() {

        $criteria=new CDbCriteria;

        $criteria->select= $fields;

        $criteria->limit= $limit;

        $criteria->condition = $condition;

        $result= $table->model()->findAll($criteria);


        header("Content-type: text/xml;charset=utf-8");

        $s = "<?xml version='1.0' encoding='utf-8'?>";

        $s .= "<rows>";

        $s .= "<page>".$page."</page>";

        $s .= "<total>".$total_pages."</total>";

        $s .= "<records>".$count."</records>";

        $fields = explode(",", $fields);

        $data = array();

        foreach($result as $n=> $row):

            $s .= "<row id='". $row->$fieldID."'>";

            foreach($fields as $field):

                $field = trim($field);

                $data[$n][$field] = $row->$field; 

                $s .= "<cell>". $row->$field."</cell>";

                endforeach;

            $s .= "</row>";

        endforeach;

        $s .= "</rows>";

        header("Content-Type: application/xml");

        header("Content-Length: " . strlen($s));

        echo $s;

   }

}



so I want to get the $data array values to use ir after the jquery call.

any idea how to do this?

y tried something like this but don’t work

[color="#8B0000"]Yii::app()->params[‘datapdf’] = $data;[/color]

Why don’t you cache it?

Just a suggestion, but have you ever considered SimpleXML for XML AJAX responses? It’s w-a-y easier to read the code and nullifies errors since it does all the closures for you.

Here’s a sample:




$xml = new SimpleXMLElement("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

$result = $xml->addChild('result', $rs->RecordCount());


foreach($rs as $row) {

    $prospect = $xml->addChild('prospect');

    $prospect->addChild('id', $row['id']);

    $prospect->addChild('fn', $row['firstName']));

    $prospect->addChild('ln', $row['lastName']));

}

echo $xml->asXML();



But, back to your issue.

What do you mean by, 'I want to get the $data array values to use ir after the jquery call"?

I presume ‘ir’ is a typo for it – otherwise we’re all in trouble – but what is it that you want $data to use? And where do you want to use it?

Okay, I’ll make a guess that you want to preserve $data between round trips. That being the case, why not add it to the session data?

I don’t know your app, but Yii::app()->params[‘datapdf’] is global, so available everywhere, which, in general, with response data, is not what you want. In any case "globals are evil"™, so best avoid that.

sorry, is a typo, I meant ‘it’.

my app is using jqgrid extension, so jqgrid call an action with jquery to get the values from database and prints the xml result as you see in the code.

but once it shows the grid result I want to export it to pdf format.

So I was thinking to create an array at the same time is creating the xml and assign it to Yii::app()->params[‘datapdf’], once click on the save button I could export to pdf.

I know globals are evil but i could not figure out something better to get the $data, I was thinking also to convert the xml to an array, but seems a lot, there must be a simplest way to do this.

that’s what I though best, maybe you have a better idea to do this?

If I were you, I would only store this data if I had a very high click-trough rate (like, above 50%). So if anyone is interested in PDF version, I’d simply regenerate that data for him.

Otherwise, the output should go to session.

And the road to programming hell is also paved with global variables. :lol:

yes now is clearer for me.

this sounds better, but still would be nice to find a way to get both xml and array data.

I am going to follow your advice.

thanks.

You don’t actually need to create $data, which a duplicate of the xml in a different form. You can simply store you XML and use that when you need the data a second time. If you either use SimpleXML or DOM XML (if the former is too simple for your needs), then it’s easy enough.




$xml = new SimpleXMLElement($s);

foreach ($xml->row as $row) {

   # do stuff with $row

}



It also depends in the size of $s. If it’s small, then it’ll be easier to simply re-query the db, unless the data is a moving target. If it’s large, then caching the data seems like the best best. But I’d cache to db or file in that case.

hey auxbuss

thank you for your advice, I am going to try this also.

cheers!