使用Foreach循环查询表中的数据

时间:2016-10-26 11:20:49

标签: php wordpress foreach customization

尝试使用查询结果数据生成表。目前数据库中有三个候选者,我想在表中显示。问题是代码只显示两个候选数据,而不是全部三个。

输出样本:

click to check result

<?php 
      global $wpdb;
      $event_id = $_GET['id'];
      $candidates= $wpdb->get_results("select field_name,field_value from wpzk_cf7dbplugin_submits where field_name in ('FirstName','Lastname','Email','Organization','Designation','Address','City') and submit_time in (SELECT submit_time FROM wpzk_cf7dbplugin_submits where field_value = '$event_id')");
      if(count($candidates) >= 1)
      {   
          $header = array(
              'Firstname' => 'First Name',
              'Lastname' => 'Last Name',
              'Organization' => 'Organization',
              'Designation' => 'Designation',
              'Email' => 'Email',
              'Address' => 'Address',
              'City' => 'City',
          );

          echo "<table>";

          echo "<tr>";
          foreach ($header as $head) {
              echo sprintf("<th>%s</th>",$head);
          }
          echo "</tr>";
          $customResult = array();
          $column = 1;
          $row = 0;
          foreach($candidates as $candidate)
          {
              $customResult[$row][$candidate->field_name] = $candidate->field_value;
              if(++$column == 7){
                  $row++;
              }
          }

          foreach ($customResult as $result) {
              echo "<tr>";
              foreach ($header as $field => $value) {
                  echo sprintf("<td>%s</td>",$result[$field]);
              }
              echo "</tr>";
          }

          echo "</table>";
      }
      else
      {
        echo "No Registration For This Event";
      }
?>

&#13;
&#13;
/*Echo result of $candiates array is given below*/
   
 Array
    (
        [0] => stdClass Object
            (
                [field_name] => Firstname
                [field_value] => Shayan
            )

        [1] => stdClass Object
            (
                [field_name] => Lastname
                [field_value] => Mahmood
            )

        [2] => stdClass Object
            (
                [field_name] => Organization
                [field_value] => CyberSolutions
            )

        [3] => stdClass Object
            (
                [field_name] => Designation
                [field_value] => Software Developer
            )

        [4] => stdClass Object
            (
                [field_name] => Email
                [field_value] => shayan33@gmail.com
            )

        [5] => stdClass Object
            (
                [field_name] => Address
                [field_value] => targetarea01
            )

        [6] => stdClass Object
            (
                [field_name] => City
                [field_value] => London
            )

        [7] => stdClass Object
            (
                [field_name] => Firstname
                [field_value] => John
            )

        [8] => stdClass Object
            (
                [field_name] => Lastname
                [field_value] => Smith
            )

        [9] => stdClass Object
            (
                [field_name] => Organization
                [field_value] => Tech Solutions
            )

        [10] => stdClass Object
            (
                [field_name] => Designation
                [field_value] => Software Quality Analyst
            )

        [11] => stdClass Object
            (
                [field_name] => Email
                [field_value] => john25@gmail.com
            )

        [12] => stdClass Object
            (
                [field_name] => Address
                [field_value] => area02
            )

        [13] => stdClass Object
            (
                [field_name] => City
                [field_value] => New York
            )

        [14] => stdClass Object
            (
                [field_name] => Firstname
                [field_value] => Nelson
            )

        [15] => stdClass Object
            (
                [field_name] => Lastname
                [field_value] => Mandela
            )

        [16] => stdClass Object
            (
                [field_name] => Organization
                [field_value] => Web Solutions
            )

        [17] => stdClass Object
            (
                [field_name] => Designation
                [field_value] => Web Developer
            )

        [18] => stdClass Object
            (
                [field_name] => Email
                [field_value] => nelson@gmail.com
            )

        [19] => stdClass Object
            (
                [field_name] => Address
                [field_value] => area03
            )

        [20] => stdClass Object
            (
                [field_name] => City
                [field_value] => Berlin
            )

    )
&#13;
&#13;
&#13;

如果有人知道代码中哪些地方出错,请提供帮助

1 个答案:

答案 0 :(得分:1)

数据来自wordpress数据库,不保证像你的标题一样排序在这种情况下你需要创建一个自定义数组,它包含所有字段及其值,然后使用标题列调用类似

试试这个

 describe('Example', () => {
    it('should stub super.constructor call', () => {
      const FooStub = spy(() => sinon.createStubInstance(Foo));
      expect(FooStub).to.have.been.calledWithNew;
    });
 });

<强>更新

解决方案#2 多个结果尝试打击代码,但这可能无法正常

// created header array
$header = array(
    'Firstname' => 'First Name',
    'Lastname' => 'Last Name',
    'Organization' => 'Organization',
    'Designation' => 'Designation',
    'Email' => 'Email',
    'Address' => 'Address',
    'City' => 'City',
);

echo "<table>";

echo "<tr>";
// display headers
foreach ($header as $head) {
    echo sprintf("<th>%s</th>",$head);
}
echo "</tr>";

// creating custom Result
$customResult = array();
foreach($candidates as $candidate)
{
    $customResult[$candidate->field_name] = $candidate->field_value;
}

echo "<tr>";
// now display result like header order.
foreach ($header as $field => $value) {
    echo sprintf("<td>%s</td>",$customResult[$field]);
}
echo "</tr>";

echo "</table>";