I have a section of code where I call a set of results with a query and then email the results as a CSV. One column of my results return either a 1 or a 0. I would like it so that when the CSV is created it write a YES for the results with a 1 and No for the results returned as 0. This is only for one column so I couldn't use a str_replace as this would affect all the data (I'm not great at PHP so I may be wrong about that).
I have tried a few thing and none seem to work, below is the section of code a one method I tried:
while ($row = mysqli_fetch_array($result)) {
if ($row->Optin_Marketing == "1") {
$mark = 'YES';
}
else {
$mark = 'NO';
}
$row->Optin_Marketing = $mark;
for ($i = 0; $i < $columns; $i++) {
$row = str_replace('"', '', $row);
$row = str_replace('en ', '', $row);
// clean up the data; strip slashes; replace double quotes with two single quotes
$data_rows .= $file["csv_contain"] . preg_replace('/'.$file["csv_contain"].'/', $file["csv_contain"].$file["csv_contain"], stripslashes($row[$i])) . $file["csv_contain"];
$data_rows .= ($i < $columns-1) ? $file["csv_separate"] : '';
}
$data_rows .= $this->csv_end_row; // add data row to CSV file
}
Any help on this would be gratefully received.
Thanks
My query:
$emailCSV->setQuery('SELECT
orderheader.ordernumber As Order_Number
, ccilog.orderid AS Order_ID
, ccilog.userid AS User_ID
, orderheader.billingcustomeremailaddress AS Customer_Email
, orderheader.billingcontactfirstname AS First_Name
, orderheader.billingcontactlastname AS Last_Name
, orderheader.billingcustomername AS Company
, orderheader.billingcustomeraddress1 AS Address_1
, orderheader.billingcustomeraddress2 AS Address_2
, orderheader.billingcustomeraddress3 AS Address_3
, orderheader.billingcustomercity AS City
, orderheader.billingcustomercounty AS County
, orderheader.billingcustomerpostcode AS Postcode
, users.sendmarketinginfo AS Optin_Marketing
FROM orderheader
LEFT JOIN users ON orderheader.userid=users.id
LEFT JOIN ccilog ON orderheader.id=ccilog.orderid
LEFT JOIN orderitems ON ccilog.orderid=orderitems.orderid
WHERE ccilog.formattedpaymentdate >= "'.$start.'" AND ccilog.formattedpaymentdate <= "'.$end.'"
');
You could do this in your mysql query:
SELECT u.fieldname, CASE WHEN u.OPTION IS 1 THEN 'Yes' ELSE 'No' END AS 'yes/no'
FROM tblName u;
Or you could do it as nickb has suggested.
EDIT:
To fit it into your query, you would replace this:
users.sendmarketinginfo AS
With this:
CASE users.sendmarketinginfo WHEN 1 THEN 'Yes' ELSE 'No' END AS 'Optin_Marketing'
Compare the integer value, like so:
while( $row = mysqli_fetch_array( $result))
{
$row['Optin_Marketing'] = ( intval( $row['Optin_Marketing']) == 1) ? "YES" : "NO";
echo $row['Optin_Marketing']; // Must be YES or NO here
}
The above condenses the entire check and change into one line using the ternary operator . You were accessing the array as if it were an object (using ->
instead of ['']
).
Also, how can this work, since $row
is an array, and str_replace
expects a string? Not sure what you're trying to do, but you might need something like array_map .
$row = str_replace('"', '', $row);
$row = str_replace('en ', '', $row);
Well, I didn't found a mysqli_fetch_array function on PHP, so, I'll just assume you meant mysql_fetch_array , which judging by the name should do pretty much the same. You have two issues in your code, the first one is with the definition of fetch array:
mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both
Can be fixed like this:
while ($row = mysql_fetch_object($result)) {
$marketingOption = $row["Optin_Marketing"];
If you call fetch_array, you should be getting an array, not an object, so act accordingly. You could just call mysql_fetch_object and do it like this:
while ($row = mysql_fetch_object($result)) {
The second is well pointed by nickb, so I'll just skip it. Hope I can help!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.