Access Keys:
Skip to content (Access Key - 0)
Home (Access Key - 1)
All spaces... (Access Key - 3)
Log in (Access Key - 5)
Sign up (Access Key - 6)
Development

Mambo Manual is part of the documentation project for the Mambo open source content management system

Toggle Sidebar

Field Names on First Row


Now we have a field list with all the field information we need. If we want field names on the first row of our csv file all we have to do is step through the field list and create the text. Here's how:

$text = '';
$fields = array();
foreach ($this->_fieldList as $key => $field) {
    // wrap the field name in delimiters
    $fields[] = $this->fdout.$key.$this->fdout;
}
// comma separate the field names
$fields = implode($this->sep, $fields);
// add the carriage return/line feed
$text .= $fields.$this->crlf;

That's it for the field names. In my case, I left my field name delimiter empty. But I left in the code in case I need it somewhere down the road.

Extracting the Data

Next we need to extract our data and format it accordingly. We'll start with a common Mambo loadObjectList:

    $rows = $db->loadObjectList();

Now we need to step through each row to extact our data. We also need to step through each field so we can wrap it with our delimiter if needed. This is where the associated list comes in handy as we work with our field list. The code starts to get a little wordy here but it's all worth it in the long run. Here's the code:

foreach($rows as $row) {
    $values = array();
    foreach ($row as $key=>$value) {
        if ($this->_fieldList[$key]->del == '"' || $this->_fieldList[$key]->del == "'") {
            // add slashes to text fields
            $value = addslashes($value);
        }
        // wrap the value in delimiters
        $values[] = $this->_fieldList[$key]->del.$value.$this->_fieldList[$key]->del;
    }
    // comma separate the values
    $text .= implode($this->sep, $values).$this->crlf;
}

The Zip File

We now have our entire csv text ready to export. In the next section, we'll create a temporary csv file we can use to create a zip file. Then we'll compress it using the pclZip library.

Click the Next Page link below to continue your journey.

Next Page: CSV Export Class - Zip File

Toggle Sidebar
Space Navigation
Added by Lynne Pope on 30 Dec, 2007 02:46, last edited by Lynne Pope on 30 Dec, 2007 02:46

Adaptavist Theme Builder Powered by Atlassian Confluence
Free theme builder license