2015-02-11 710 views
3

在我的查询中,我有3个tinyint字段,它们通过获取给出0/1值。但是在获取所有我想要将结果编码为JSON格式的数据之后,应该得到true/false而不是0/1。 在我的代码中,我做了类型转换,它给出了正确的结果。但我应该写下所有我不想要的tinyint字段。 是否有任何方法来检查tinyint字段和转换。如何检查mysql表的boolean字段,并将0或1的值转换为true/false在php

PHP CODE: 


$result= mysql_query("select incident_main.inc_id as incident_id, 
         ward_master.ward_id as 'ward_id', 
         GROUP_CONCAT(incident_log.inclog_date) as revisit_dates , 
         inc_GISlat as lat, 
         inc_GISlon as lon, 
      inc_closed as b_is_closed, 
       inc_closeDate as closed_date, 
         incident_statuscd.inc_status_desc as status, 
         inc_date as 'incident_date', 
         inc_patientName as 'patient_name', 
         inc_patientAge as 'patient_age', 
         inc_patientGender as 'patient_gender', 
         inc_patientMobile as'patient_mobile' , 
         inc_patientAddress as 'patient_address', 
         inc_type as type, 
         inc_typeOther as 'type_other', 
         inc_diagnosis as diagnosis, 
         inc_recurrence as recurrence, 
       inc_hospitalized as b_hospitalized, 
         inc_treatment as treatment , 
         inc_treatmentOther as 'treatment_other' 
        from 
         incident_main, 
         incident_statuscd , 
         ward_master, 
         incident_log 
        where 
         inc_status=incident_statuscd.inc_status_id 
        and 
         incident_main.inc_patientWard=ward_master.ward_id 
        and incident_log.inc_id=incident_main.inc_id ") 
         or die(mysql_error); 
while($res = mysql_fetch_assoc($result)) { 
foreach($res as $key => $value) { 
    if(substr($key, 0, 2) === "b_") { 
     // assign new value 
     $res[substr($key,2,strlen($key))] = !!$value; 
     //clean up old value 
     unset($res[$key]); 
     }; 

    } 
    $data=array_filter($res); 

echo json_encode($data, true); 

请帮忙!! 在此先感谢。

+0

JS也有类型转换。所以不要这样做,如果你不想。 – Vick 2015-02-11 06:36:37

+0

可以请你更具体地解释....在哪里以及如何? – user092 2015-02-11 06:40:11

+0

[请参阅此链接](http://stackoverflow.com/questions/16313222/how-to-convert-1-to-true-or-0-to-false-upon-model-fetch) – 2015-02-11 06:48:52

回答

1

PHP中

检查TINYINT你可以做的是用B_预先考虑的变量,并使用正则表达式/ strpos/SUBSTR来筛选,以强制转换到这一点布尔没有真正的方法。这样,你只需要调整你的查询,脚本就会为你做铸件。

$result= mysql_query("select incident_main.inc_id as incident_id, 
         ward_master.ward_id as 'ward_id', 
         GROUP_CONCAT(incident_log.inclog_date) as revisit_dates , 
         inc_GISlat as lat, 
         inc_GISlon as lon, 
    --->    inc_closed as b_is_closed, 
    --->    inc_reviewed as b_is_reviewed, 
         inc_closeDate as closed_date, 
         incident_statuscd.inc_status_desc as status, 
         inc_date as 'incident_date', 
         inc_patientName as 'patient_name', 
         inc_patientAge as 'patient_age', 
         inc_patientGender as 'patient_gender', 
         inc_patientMobile as'patient_mobile' , 
         inc_patientAddress as 'patient_address', 
         inc_type as type, 
         inc_typeOther as 'type_other', 
         inc_diagnosis as diagnosis, 
         inc_recurrence as recurrence, 
    --->    inc_hospitalized as b_hospitalized, 
         inc_treatment as treatment , 
         inc_treatmentOther as 'treatment_other' 
        from 
         incident_main, 
         incident_statuscd , 
         ward_master, 
         incident_log 
        where 
         inc_status=incident_statuscd.inc_status_id 
        and 
         incident_main.inc_patientWard=ward_master.ward_id 
        and incident_log.inc_id=incident_main.inc_id ") 
         or die(mysql_error); 
while($res = mysql_fetch_assoc($result)) { 
foreach($res as $key => $value) { 
    if(substr($key, 0, 2) === "b_") { 
     // assign new value 
     $res[substr($key,2,strlen($key))] = !!$value; 
     //clean up old value 
     unset($res[$key]); 
    } 
    } 
    $data=array_filter($res,'not_null'); 
} 


function not_null($test) { 
    return !($test === null); 
} 

EDIT

加入一种方法来恢复的布尔标记键为正常的代码执行原始值。 是它的重复,但可能有助于进一步的实现/代码执行,而无需重命名所有内容b_

+0

我应该通过所有布尔变量在b_的地方吗? – user092 2015-02-11 07:44:09

+0

当你设计查询的时候,你给所有的字段提供你想要的名字,就像你在这里做的那样,只需在所有应该是你的布尔值的字段前添加一个b_。 这样你就不用担心在php代码中使用我的代码。 – Tschallacka 2015-02-11 07:54:19

+0

只要确保我的代码在while语句后运行。另外,如果你真的更喜欢你为其他代码提供的原始名称,你也可以添加编辑我现在正在制作 – Tschallacka 2015-02-11 07:56:18

-2

JS代码:

if(1) 
    alert('true'); 
if(0) 
    alert('false'); 
+0

我有一些整数领域..那么那又如何呢? – user092 2015-02-11 06:46:03

+0

为什么你继续关于JS?这个关于PHP的问题。 – BoltClock 2015-02-11 13:35:35

0

试着写另一个功能:

private function isTrue($input){ 
    if ((bool)$input){ 
      return "true"; 
    }else{ 
      return "false"; 
    }   
} 

当与0或1返回文本称为 “真” 或 “假”。然后加入呼吁要编码的每个TINYINT:

... 
$row['hospitalized'] = isTrue($row['hospitalized']); 
... 
+0

通过使用你的代码是可能的,但我应该指定所有布尔字段,任何其他方法是否有避免这个函数一次又一次的调用? – user092 2015-02-11 06:56:25

1

不幸的是,有没有办法在PHP区分TINYINTinteger。因此,您需要列出要转换的字段。这样做的缩短版本如下所示:

$a = ['a' => 1, 'b' => 100, 'c' => 0 ]; 
$bools = ['a', 'c']; // list of fields to transform 

$result = array_map(function($item) use($bools,&$a) { 
    $key=key($a); next($a); // we need a key to check it’s name 
    return in_array($key, $bools) ? (bool)$item : $item; 
}, $a); 

print_r(json_encode($result, true)); 
//⇒ {"a":true,"b":100,"c":false} 

如果并且只有key$bools阵列列出后者将投integerbool到。有关更多详细信息,请参阅array_map docs

希望它有帮助。

+0

$ item包含什么? – user092 2015-02-11 07:05:05

+0

这是一个回调参数;在遍历数组的过程中,每次调用'function'时都会包含一个数组元素。 – mudasobwa 2015-02-11 07:09:25

+0

while($ row = mysql_fetch_assoc($ result)) { \t \t $ bools = ['inc_hospitalized']; \t \t \t \t $ user [] = array_filter(($ row)); \t \t $ result1 = array_map(函数($ item)use($ bools,&$ user){ $ key = key($ user); next($ user); //我们需要一个键来检查它的名字 返回in_array($ key,$ bools)?(bool)$ item:$ item; },$ user); }这是我如何实现你的代码,但它不工作。 – user092 2015-02-11 07:17:28

相关问题