0

I am still very new to Zend and running into some issues on exporting my data to a CSV.

I found a great resource that explains the headers and download part here however I am running into issues when trying to export the actual data.

If I create a variable like $content = "test" the export works fine using the code above.

However when I duplicate my indexAction code, make some changes, and bring it into my downloadAction, I am getting issues that I believe are due to my content being returned as an Object rather than an array or string.

My Module is grabbing the SQL by using:

public function fetchAllMembers($order = null , $order_by = null, $selectwhere = null) {
    $session = new SessionContainer('logggedin_user');
    $sql = new Sql($this->adapter);
    $select = new Select();

        $select->from(array('u' => 'tbl_all_data'));
        if ($selectwhere != null){
            $select->where($selectwhere);
        }
        $select->order($order_by . ' ' . $order);

    $selectString = $sql->getSqlStringForSqlObject($select);    
    $results = $this->adapter->query($selectString, Adapter::QUERY_MODE_EXECUTE);
    $results->buffer();
    return $results;
}

and my Controller is calling that SQL by using:

 $content = $modulesTable->fetchAllMembers($order, $order_by, $where);

Any help would be greatly appreciated, and I don't need anyone to write the code for me just help with pointoing me in the right direction.

2
  • Neither you code nor the link shows how $content is assigned. Commented Nov 12, 2014 at 19:57
  • Prandeep, I just made a small edit, however you are correct. $content is what I am trying to figure out. As I mentioned, if I make $content = "test" my code works perfect and I get a CSV with the word Test. What I am trying to figure out is how to make $content = an array with all my data which comes from fetchAllMembers. Commented Nov 12, 2014 at 20:04

2 Answers 2

0

$this->adapter->query returns a Zend\Db\ResultSet object. So you need to call $results = $results->toArray(); to send an array.

Also you need to loop through the array and echo it out in your view file.

Sign up to request clarification or add additional context in comments.

Comments

0

Results, returned by adapter are ResultSet type. I guess you need to call at least

current()

method to grab some data. And they will be of array type, so, again you need to do something with them. toArray() is often used to quickly get data.

More sophisticated way to get data, is to use next() method with current():

$firstThing = $result->current();
$result->next();
$result->next();
$thirdThing = $result->current();

It's just an example, but it can be useful in some cases.

1 Comment

Thank you for your answer, and I will look into this, however the other answer was exactly the call I needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.