从数据库生成的PHP多维数组 [英] php multidimension array generated from database
问题描述
我正在研究一个代码,我从这篇文章中获得了大部分的功能:
https:// stackoverflow。 com / a / 19582946/1316372
但我试图从Mysql数据库获取数据如下:
//我的2分贝查询
$ query = array();
$ properties_array = array();
$ selector_query = tep_db_query(SELECT * FROM bts_selectors);
while($ selector = tep_db_fetch_array($ selector_query)){
$ query [] = array('id'=>(int)$ selector ['id'],
'selector '=> $ selector ['selector']
);
$ properties_query = tep_db_query(SELECT * FROM bts_properties WHERE selector_id ='。(int)$ selector ['id']。');
while($ properties_result = tep_db_fetch_array($ properties_query)){
$ properties_array [] = array('id'=>(int)$ properties_result ['id'],
'selector_id '=>(int)$ properties_result ['selector_id'],
'css_element'=> $ properties_result ['css_el'],
'element_value'=> $ properties_result ['css_val']
);
//这是一个工作的静态数组
$ probs_good = array(
1 => array(
array('id'= > 1,'selector_id'=> 1,'css_element'=>'border','element_value'=>'3px solid'),
array('id'=> 2,'selector_id '=> 1,'css_element'=>'padding','element_value'=>'10px')
)
);
///这里的输出应该是生成的
$ css ='';
foreach($ query as $ selector){
// $ properties = $ probs_good [$ selector ['id']]; //这是工作的静态数组
$ properties = $ properties_array [$ selector ['id']];
$ rules ='';
foreach($ properties as $ element){
$ rules。=\\\
\ t $ element [css_element]:$ element [element_value];;
$ b $ css。=$ selector [selector]。
}
echo< pre>;
echo$ css;
echo< / pre>;
我意识到我通过 foreach
要求数组应该和 $ probs_good
具有相同的结构,但是说实话,我每次都尝试失败。
在帖子中我指的是有一个引用的查询。
我也做了一些尝试。
现在,我认为只显示干净/简单的代码并解释我所要达到的目标会更好。
好的,在做了研究之后,我发现了解决方案(很容易,但是从来没有遇到.....)
$ selectors_array = array();
$ selector_query = tep_db_query(SELECT * FROM bts_selectors);
while($ selector = tep_db_fetch_array($ selector_query)){
$ selectors_array [] = array('id'=> $ selector ['id'],
'selector'=> ; $ selector ['selector']
);
}
$ css ='';
foreach($ selectors_array as $ selector){
$ rules ='';
$ properties_query = tep_db_query(SELECT * FROM bts_properties WHERE selector_id ='。$ selector ['id']。');
while($ properties = tep_db_fetch_array($ properties_query)){
$ rules。=\\\
\ t $ properties [css_el]:$ properties [css_val];;
}
$ css。=$选择器[选择器]。{。$ rules \\\
。}。\\\
\\\
;
}
echo< pre>;
echo$ css;
echo< / pre>;
生成并希望输出:
.thisone {
border:1px solid;
padding:10px;
}
#thatone {
border:1px solid;
}
.body {
width:40px;
height:40px;
}
I am working on a code where i got most of the functionality from this post: https://stackoverflow.com/a/19582946/1316372
But i tried to fetch the data from a Mysql database as following:
//my 2 db queries
$query = array();
$properties_array = array();
$selector_query = tep_db_query("SELECT * FROM bts_selectors");
while ($selector = tep_db_fetch_array($selector_query)) {
$query[] = array('id' => (int)$selector['id'],
'selector' => $selector['selector']
);
}
$properties_query = tep_db_query("SELECT * FROM bts_properties WHERE selector_id= '".(int)$selector['id']."'");
while ($properties_result = tep_db_fetch_array($properties_query)) {
$properties_array[] = array('id' => (int)$properties_result['id'],
'selector_id' => (int)$properties_result['selector_id'],
'css_element' => $properties_result['css_el'],
'element_value' => $properties_result['css_val']
);
}
//this is a working static array
$probs_good =array(
1 => array(
array('id' => 1, 'selector_id' => 1, 'css_element' => 'border', 'element_value' => '3px solid'),
array('id' => 2, 'selector_id' => 1, 'css_element' => 'padding', 'element_value' => '10px')
)
);
///here the output should be generated
$css = '';
foreach($query as $selector){
//$properties = $probs_good[$selector['id']]; //this the working static array
$properties = $properties_array[$selector['id']];
$rules = '';
foreach($properties as $element){
$rules .= "\n \t$element[css_element]:$element[element_value];";
}
$css .= "$selector[selector]".'{'."$rules \n".'}'."\n\n";
}
echo "<pre>";
echo "$css";
echo "</pre>";
I am aware that the way i go over the foreach
requires that the array should have the same structure as the $probs_good
, but honestly i fail with each try.
In the post i refer to there is a quoted query made.
I made some attempts to comply to that also.
For now i thought it would be better to just show clean/simple code and explain what i tried to achieve.
Ok, after doing the research i found the solution (SO EASY, BUT WHEN NEVER HAD TO.....)
$selectors_array = array();
$selector_query = tep_db_query("SELECT * FROM bts_selectors");
while ($selector = tep_db_fetch_array($selector_query)) {
$selectors_array[] = array('id' => $selector['id'],
'selector' => $selector['selector']
);
}
$css = '';
foreach ( $selectors_array as $selector ) {
$rules = '';
$properties_query = tep_db_query("SELECT * FROM bts_properties WHERE selector_id = '" . $selector['id'] . "' ");
while ($properties = tep_db_fetch_array($properties_query)) {
$rules .= "\n \t$properties[css_el]:$properties[css_val];";
}
$css .= "$selector[selector]".'{'."$rules \n".'}'."\n\n";
}
echo "<pre>";
echo "$css";
echo "</pre>";
Generated and wanted output:
.thisone{
border:1px solid;
padding:10px;
}
#thatone{
border:1px solid;
}
.body{
width:40px;
height:40px;
}
这篇关于从数据库生成的PHP多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!