将外部SQL DB中的字段显示为数组中的值

时间:2016-09-30 作者:motorbaby

具有如下SQL数据集:

    Title   Author  Device1 Device2 Device3
    Title1  j cool  inputA  inputA  inputB
    Title2  l maker inputA  inputB  inputC
    Title3  m smith inputB  inputB  inputB
要这样显示:

    Title   Author  header1 header2 header3
    Title1  j cool  inputA: inputB:
                    Device1 Device3
                    Device2 
    Title2  l maker inputA: inputB: inputC:
                    Device1 Device2 Device3
    Title3  m smith inputB:
                    Device1
                    Device2
                    Device3
将下面的php以及表中的一些HTML进行音译wpdb:

    $sqlSelect = "SELECT * FROM titles WHERE `active` = 0 ORDER BY author, title";
    $myquery = $mysqli->query ($sqlSelect);
    if ($myquery = $mysqli->query ($sqlSelect)) {
      $result = array();
      while($row = mysqli_fetch_assoc($myquery)){
        $tmp = array();
        foreach (array(\'device1\', \'device2\', \'device3\', \'device4\') as $key) {
          if (!isset($tmp[$row[$key]])) $tmp[$row[$key]] = array();
          $tmp[$row[$key]][] = $key;
        }
        $result[$row[\'title\'] . "</td><td>" . $row[\'author\']] = $tmp;
       }

      $max = 0;
      foreach ($result as $data => $inputs) {
        $max = max($max, count($inputs));
      }
      // looping rows begins here
      foreach ($result as $data => $inputs) {
        print(\'<tr><td>\' . $data . \'</td>\');
        foreach ($inputs as $input => $devices) {
          print(\'<td>\' . $input . \':<br/>\' . implode(\'<br/>\', $devices) . \'</td>\');
        }
    //next two lines are for displaying no cells where there is no $data
        for ($i = 0; $i < $max - count($inputs); ++$i) {
          print(\'<td class="db"></td>\');
        }

        print(\'</tr>\');
尝试了以下操作:

    $mydb = new wpdb(\'user\',\'password\',\'database\',\'server\');
    $results = $mydb -> get_results("SELECT * FROM titles WHERE `active` = 1 ORDER BY author, title");
    $result = array();
    while ($result = $row) { //this doesn\'t work; and only one row without data except all devices displays without it 
        $tmp = array(); //ditto to the end
WordPress似乎不喜欢$data. 有没有办法做到这一点wpdb?

2 个回复
最合适的回答,由SO网友:adelval 整理而成

我认为没有理由为此使用wpdb,除非您的表在Wordpress数据库中。此外,如果将结果提取到一个数组中,并将此数组的转换处理与最终html输出的生成分离开来,那么代码将更加清晰(因此不容易出错)。将逻辑与表示分离。

因此,首先以标准方式将所有数据放入一个数组中:

$query = ("SELECT * FROM titles WHERE `active` = 0 ORDER BY author, title");
$results_array = array();
$result = $mysqli->query($query);
while ($row = $result->fetch_assoc()) {
  $results_array[] = $row;
}
此数组的第一行,即。$results_array[0], 例如,将是数组(\'Title\' => \'Title 1\',\'Author\' => \'j cool\', \'Device1\' => \'inputA\',...)

Edit: 或者,如果坚持使用wpdb类,请将上述代码替换为:

$mydb = new wpdb(\'user\',\'password\',\'database\',\'server\');
$results_array = $mydb->get_results("SELECT * FROM titles WHERE `active` = 0 ORDER BY author, title",ARRAY_A);
$results_array 这两种方法的内容相同。代码的其余部分不变。

End edit

您要从中获取$results_array 一个关联数组,将输入映射到设备的表与每个作者关联,如下所示:

array(\'j cool\' => array(\'inputA\' => array(\'Device1\', \'Device2\'),
                        \'inputB\' => array(\'Device3\')),
      \'l maker\' => array(\'inputA\' => array(\'Device1\'),
                        \'inputB\' => array(\'Device2\'),
                        \'inputC\' => array(\'Device3\')),
       \'m this\' => ...)
下面的代码假设您事先知道输入的所有可能值,并且这些值与作者或标题不完全匹配。

 $inputs = array(\'inputA\',\'inputB\',\'inputC\');

 $input_table = array();

 foreach ($results_array as $row) {
   $row_inputs = array();
   foreach ($inputs as $in) {
      //get all keys (devices) in $row  that have input $in as value
      $devices = array_keys($row,$in); 
      if (!empty($devices))
        $row_inputs[$in] = $devices;
   }
   $input_table[$row[\'Author\']] = $row_inputs;
 }
这将生成一个数组,其中由作者编制索引的每一行都是一个类似上述示例的数组。现在我们添加标题:

$final_table = array();
foreach ($results_array as $row) {
  $final_table[] = array(\'Title\' => $row[\'Title\'],
                       \'Author\' => $row[\'Author\'],
                       \'Inputs\' => $input_table[$row[\'Author\']]);
}
最后,以html显示生成的数组:

$html = \'\';
//$html .= \'<html><body>\';
$html .= "<table>"; //add here code for table headers too

foreach ($final_table as $row) {
  //first row for author
  $html .= \'<tr><td>\'. $row[\'Title\'] . \'</td><td>\' . $row[\'Author\'] . \'</td>\';
  foreach (array_keys($row[\'Inputs\']) as $in) {
    //output input names for this author
    $html .= \'<td>\'.$in.\'</td>\';
  }
  $html .= \'</tr><td></td><td></td>\';
  //second row for author, starting in third column
  foreach ($row[\'Inputs\'] as $in => $devices) {
    $html .= \'<td>\'.implode(" ",$devices).\'</td>\';
  }
  $html .= \'</tr>\';
}

$html .= \'</table>\';
// $html .= </body></html>
echo $html;
根据您的喜好对演示文稿进行微调。。。

SO网友:motorbaby

通过安装WP-DB驱动程序插件并跳过wpdb 总共但是,该页面只能检索页眉和页脚模板。mysqli查询似乎与WordPress codex不兼容。