0

I'm trying to understand how to fix this error.

Warning: prev() expects parameter 1 to be array, string given in

Its in the if statement below. Is this happening since the first value doesn't have a previous and I need to deal with that condition? Weirdly this worked in regular .php but not in the framework I have it in now.

I'm trying to generate an XML file based on a result set returned for a query. (I'm open to better ideas)

    $export.= '<Campaigns>';
    while ($line = mysql_fetch_assoc($result) ) {
        //echo '<Email Timestamp="' . $line['EmailTimeStamp'] . '" ';
        $export.=  '<Campaign Info="' . $line['EmailTrackingNumber'] . '" EmailId="' .$line['EmailId'] . '">';
        $export.=  '<Emails>';
                if (prev($line['EmailTrackingNumber']) == current($line['EmailTrackingNumber'])) {
                    $export.=  '<Email Timestamp="' . $line['EmailTimeStamp'] . '" ';
                    $export.=  'City="' . $line['City'] . '" ';
                    $export.=  'Zip="' . $line['Zip'] . '"';
                }
        $export.=  '</Emails></Campaign>';
        }
    $export.= '</Campaigns></EmailTrackingData>';
    //echo $export;
    file_put_contents('DateOfFile-export.xml', $export);
1
  • 3
    It's happening because you're passing a string to prev(), and it is expecting an array. In this case, if I understand your code correctly, you're trying to output things based on whether variables in this database row have changed - you just need to save the variables from the previous loop iteration somewhere and compare them with the current variables instead.
    – andrewsi
    Nov 7, 2013 at 14:18

1 Answer 1

0

This

prev($line['EmailTrackingNumber'])

is not an array but a string. This

prev($line)

makes more sense. It returns the array entry which is before the current entry of $line.

But I think you would like to compare the last record with the current record. But that does not work like this. You can only access the columns of the current record. You have to temporarly save your last record.

$export.= '<Campaigns>';

$lastLine = null;

while ($line =  mysql_fetch_assoc($result)) {
    //echo '<Email Timestamp="' . $line['EmailTimeStamp'] . '" ';
    $export.=  '<Campaign Info="' . $line['EmailTrackingNumber'] . '" EmailId="' .$line['EmailId'] . '">';
    $export.=  '<Emails>';

    if ($lastLine['EmailTrackingNumber'] == $line['EmailTrackingNumber']) {
        $export.=  '<Email Timestamp="' . $line['EmailTimeStamp'] . '" ';
        $export.=  'City="' . $line['City'] . '" ';
        $export.=  'Zip="' . $line['Zip'] . '"';
    }

    $export.=  '</Emails></Campaign>';
    $lastLine = $line;
}
$export.= '</Campaigns></EmailTrackingData>';
//echo $export;
file_put_contents('DateOfFile-export.xml', $export);

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.