2013-04-24 75 views
0

我有一个脚本,从一个mysql结果产生一个表,我的目标是使列显示完全动态基于一个变量,其中包含我想显示的列的名称。我已经制作表格标题这种方式使用此代码:如果列名称匹配变量只打印行列

$search_field_names=explode(',', $search_field_names)  
for($i=0, $count=count($search_field_names);$i<$count;$i++) { 
     echo ('<th>'.$search_field_names[$i].'</th>'); 
    } 

其中$ search_field_names是一个逗号separatated值的字符串(例如像fristname,姓氏,USER_ID等)。

打一个比方,假设上面的字符串包含我要显示这样的列名7:

$search_field_names=('firstname,lastname,dob,company,position,user_id,date_added') 

第一代码片段将随后产生的表与7名标题。

然后出现棘手的部分:存储所有记录的数据库表共有22列。通过完成与第一个片段打印记录完全相同的内容,结果将会打印该表格的前7列,这不一定是我想要的。

这里是我得到的记录(这abviously自带的脚本上面的片断后):

$query=mysqli_query($mysqli, "SELECT * FROM table WHERE column_name1='$search_query' AND column_name2='$search_query' etc...); 
while($row=mysqli_fetch_assoc($query)) { 
echo('<tr>'); 
for($i=0, $count=count($search_field_names);$i<$count;$i++) { 
     echo ('<td>'.$row[$i].'</td>'); 
    } 
echo('</tr>'); 

所以,我想实现的是只取在数据库中,这correspons列到变量$ search_field_names中设置的名称。

+1

为什么不;吨你改变你的选择查询并只获取​​$ Search_field_names变量中可用的列? – Haider 2013-04-24 11:49:20

+0

因为我的网页的每个用户都有数据存储在数据库的不同列中,所以查询会搜索所有列。但是,正如你提到的那样,是否有可能在搜索查询中为()循环做类似的操作,所以它只搜索存储在变量中的列?如果这是可能的这可能会解决我的问题。 – Vegard 2013-04-24 11:58:16

回答

1

两种解决方案,由海德尔建议,请使用$ search_field_names变量,以适应​​您的查询:

​​

或者,因为你在做一个mysqli_fetch_assoc,这应该工作:

$query=mysqli_query($mysqli, "SELECT * FROM table"); 
while($row=mysqli_fetch_assoc($query)) { 
    echo('<tr>'); 
    for($i=0, $count=count($search_field_names);$i<$count;$i++) { 
     // Using header name as the key to the column 
     echo ('<td>'.$row[ $search_field_names[$i] ].'</td>'); 
    } 
    echo('</tr>'); 
} 

编辑:到目前为止,我们只看巧妙循环$ search_field_names来生成结果表。您在最后的评论中询问了构建查询约束条件时是否可以使用相同的技巧。一个问题是,你的似乎要直接在您的查询中使用$ _POST。请不要这样做,这是SQL injections的来源。另一个问题是,我们永远不会聪明。如果所有字段的类型都是相同的,我们可以这样做,但如果其中一个搜索字段是日期或整数,那么编写和调试将会非常困难。

所以,如果你真的要巧用你的查询,你可以做这样的事情(假设所有搜索字段字符串值):

<?php 
// Assuming you have a $post_array like so, where inputs have the same names as the column names in the database 
// $post_array = array('username' => 'T', 'url' => 'http'); 

// Build constraints of the form column_name LIKE 'value%' 
$constraints = array(); 
foreach ($post_array as $key => $value) { 
    $constraints[] = "$key LIKE ?"; 
    $post_array[$key] = $value.'%'; 
} 

// Build a query selecting the columns from $post_array, with corresponding constraints 
// Note : replace " AND " by " OR " if you want the query to match ANY parameter. 
$query_text = "SELECT ".implode(", ", array_keys($post_array))." FROM table WHERE ".implode(" AND ", $constraints); 

// Prepare this query, we don't want SQL injections! 
$query = $mysqli->prepare($query_text); 

$bind = array(); 
foreach ($post_array as $key => $value) { 
    // We need to use our bind variables outside the scope of this loop, so we create them in $GLOBALS 
    $GLOBALS[$key] = $value; 
    // Array of references to our bind variables. 
    $bind[] = &$$key; 
} 
// Binding the parameters : we need as many 's' as there are inputs 
$bindtypes = str_repeat('s', count($post_array)) 
call_user_func_array(array($query, 'bind_param'), array_merge(array($bindtypes), $bind)); 
// Binding the retrieved columns 
echo call_user_func_array(array($query, 'bind_result'), $bind); 
$query->execute(); 

// Header row 
echo '<tr>'; 
foreach ($post_array as $key => $value) { 
    echo '<th>'.$key.'</th>'; 
} 
echo '</tr>'; 

// Result rows 
while ($query->fetch()) { 
    echo('<tr>'); 
    foreach ($post_array as $key => $value) { 
     echo '<td>', $$key, '</td>'; 
    } 
    echo('</tr>'); 
} 
?> 
+0

是的,这是一个可能的解决方案,但是我忘了在我的原始问题中包含查询是SELECT .. FROM table WHERE column_name ='$ search_query'。 – Vegard 2013-04-24 12:26:39

+0

我不明白这个查询。 '$ search_query'变量中有什么? 'column_name'是什么意思? – 2013-04-24 13:08:56

+0

$ search_query是来自输入字段的POST变量 column_name只是一个示例。一个真实世界的例子是: SELECT .. FROM table WHERE firstname LIKE'$ search_query%'和lastname LIKE'$ search_query $'等。 – Vegard 2013-04-25 06:09:52