2017-10-12 143 views
-1

我正在从正在工作的数据库获取输出。在数组下方显示正确的输出。爆炸功能无法正常工作

$get_elements = array(
     'student_elements' => $row->student_elements, 
     'address_elements' => $row->address_elements, 
     'marketing_elements' => $row->marketing_elements, 
     'office_use_elements' => $row->office_use_elements, 
); 

入门输出

Array 
(
    [student_elements] => firstname,lastname,mobileno,age,gender 
    [address_elements] => building,sector,city 
    [marketing_elements] => 
    [office_use_elements] => counsellername,mobile,email 
) 

现在我通过数组值在爆炸功能

$result_elements=explode(',',$get_elements); 

得到错误

Severity: Warning 
Message: explode() expects parameter 2 to be string, array given 

我想通过$result_elements中的foreach

$results = []; 
foreach ($result_elements as $value) { 
    echo $sql_elements_get="SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name='".$value."'"; 
    $fetch_query = $this->db->query($sql_elements_get); 
    foreach ($fetch_query->result() as $r){ 
     $results[] = $r; 
    } 
} 
return $results;// I 

我想每次运行查询以获取每个来自爆炸函数的值的输出。

你能帮我解决吗?

+1

您正在将一个数组传递给'explode'。你不能爆炸一个数组。你只能爆炸一个字符串。错误信息告诉你很多。你想'爆炸'这个:'$ row-> student_elements',而不是'$ get_elements'。 – deceze

+0

@deceze,对,但我传递$ row-> student_elements,然后我得到输出只有名字,姓氏,移动,年龄,性别 –

回答

1

它看起来像你试图让所有的字符串的所有独特元素的列表。正如其他人所说,你不能explode一个数组。一种方式,你可以通过这个循环来将implode排列在一起,然后explode它。

<?php 
$get_elements = array (
    'student_elements'  => 'firstname,lastname,mobileno,age,gender', 
    'address_elements'  => 'building,sector,city', 
    'marketing_elements'  => NULL, 
    'office_use_elements'  => 'counsellername,mobile,email' 
); 

// Combine all strings 
$get_elements = implode(',',$get_elements); 

// DEBUGGING ONLY 
echo $get_elements; 

// Split the elements 
$get_elements = explode(',', $get_elements); 

// DEBUGGING ONLY 
echo '<pre>'.var_export($get_elements, TRUE).'</pre>'; 

// YOUR CODE 
$results = []; 
foreach ($get_elements as $value) { 
    // Ensure it's not empty 
    if($value != ''){ 
     echo $sql_elements_get="SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name='".$value."'"; 
     $fetch_query = $this->db->query($sql_elements_get); 
     foreach ($fetch_query->result() as $r){ 
      $results[] = $r; 
     } 
    } 
} 
return $results; 

?> 

A Codepad链接代码输出(删除数据库调用)。

0

爆炸函数需要第二个参数作为字符串而不是数组。你必须进一步深入你的阵列获取字符串

$get_elements = array(
    'student_elements' => $row->student_elements, 
    'address_elements' => $row->address_elements, 
    'marketing_elements' => $row->marketing_elements, 
    'office_use_elements' => $row->office_use_elements, 
); 
for($get_elements as $key=>$value){ 
    $value = explode(',', $value); 
    //run your query here 
} 
+0

我得到了一个错误,语法错误,意想不到'(T_AS),期待' ;” –

+0

它的语法错误,你写了'for'循环而不是'foreach' –

1

不要运行这么多的查询;只运行一个。最佳做法是始终尽可能降低查询次数。由于您不区分结果元素的不同值,因此仅使用IN的一个查询将会很有效。 *注意,在用逗号加入之前,我会过滤掉所有空串,然后用单引号将所有值包裹起来。


要强调的WHERE子句中IN的重要性,这里的区别是:

没有IN(假设你过滤掉空元素)你让11个调用数据库:

SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name='firstname' 
SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name='lastname' 
SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name='mobileno' 
SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name='age' 
SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name='gender' 
SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name='building' 
SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name='sector' 
SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name='city' 
SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name='counsellername' 
SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name='mobile' 
SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name='email' 

随着IN你做1个调用数据库:

SELECT fields_name, fields_type FROM `tbl_form_builder_fields` WHERE fields_name IN ('firstname','lastname','mobileno','age','gender','building','sector','city','counsellername','mobile','email')