doudou130216 2014-09-03 23:06 采纳率: 0%
浏览 19
已采纳

从数据库生成的php多维数组[关闭]

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 .= "
 \t$element[css_element]:$element[element_value];";
    }

    $css .= "$selector[selector]".'{'."$rules 
".'}'."

";
}
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.

  • 写回答

1条回答 默认 最新

  • duanhuan6336 2014-09-04 11:14
    关注

    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 .= "
     \t$properties[css_el]:$properties[css_val];";
        }
            $css .= "$selector[selector]".'{'."$rules 
    ".'}'."
    
    ";
        }
    
        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; 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 three.js添加后处理以后模型锯齿化严重
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常