简体   繁体   English

PHP查询将结果1更改为YES

[英]PHP query Changing a result of 1 to YES

I have a section of code where I call a set of results with a query and then email the results as a CSV. 我有一段代码,其中我用查询调用一组结果,然后将结果以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). 我的结果的一列返回1或0。我想要这样,以便在创建CSV时,它为结果写YES,结果为1,而对于返回为0的结果为No。这仅适用于一列,因此我不能使用str_replace,因为这会影响所有数据(我不太擅长PHP,所以我可能是错的)。

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: 您可以在mysql查询中执行此操作:

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. 或者您可以按照nickb的建议进行操作。

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? 另外,由于$row是一个数组,而str_replace需要一个字符串,这将如何工作? Not sure what you're trying to do, but you might need something like array_map . 不确定您要做什么,但是您可能需要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. 好吧,我没有在PHP上找到mysqli_fetch_array函数,因此,我假设您的意思是mysql_fetch_array ,按名称判断应该做的差不多。 You have two issues in your code, the first one is with the definition of fetch array: 您的代码中有两个问题,第一个是fetch array的定义:

mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both mysql_fetch_array —提取结果行为关联数组,数字数组或两者

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. 如果调用fetch_array,则应该获取数组,而不是对象,因此应采取相应措施。 You could just call mysql_fetch_object and do it like this: 您可以只调用mysql_fetch_object并按以下方式进行操作:

while ($row = mysql_fetch_object($result)) { 

The second is well pointed by nickb, so I'll just skip it. 尼克(nickb)很好地指出了第二点,因此我将跳过它。 Hope I can help! 希望我能帮忙!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM