动态生成html标签不起作用,并为$ this关键字提供致命错误

时间:2017-05-06 07:03:54

标签: php html

<?php
include("con_gen.php");
?>
<?php
$link = mysqli_connect("localhost","root","")  or die("failed to connect to server !!");
mysqli_select_db($link,"idcard");
$sql = "SELECT static_name FROM static_values";
      $result = mysqli_query( $sql,$this->link);
      $ch='rdbtn';
        if ($result>0) {
      $cnt=0;

        while( $fetch = mysqli_fetch_assoc( $result ) )
         {    
            $this->html[] ='
              <tr >
                    <td>
                <div class="row-fluid">

            <div class="span3 bgcolor">
              <?php echo "<label>'.$fetch["static_name"].'</label>";?>

              <select  id='.$fetch["static_name"].' name='.$fetch["static_name"].' data-live-search="true" class="selectpicker form-control" >

                <?php echo "<option>".$row. "</option>";
                 foreach ( $Data->'.$fetch["static_name"].' as $key =>$item) echo "<option value=".$key.">".$item."</option>"; ?>               
                </select> 
                                                </div>
</div>
              </td>

    </tr>'            ;        
        }


      $this->close();
      $this->close(); 
     return implode( "\r\n", $this->html );

     }


echo "<pre>".$html."</pre>";
?>

static_values表中有一些static_name我正在获取这些名称并生成一个html标记,但它不适合我,我该怎么办?

在这里,我尝试生成动态html标记,以便给出错误我应该如何解决此错误。 我尝试了很多次,但它没有解决我没有到达我出错的地方

它给了我以下错误:

  

致命错误:在第8行的C:\ xampp \ htdocs \ dyn_html.php中不在对象上下文中时使用$ this

3 个答案:

答案 0 :(得分:1)

我假设你没有在这里使用课程或功能。基于你的问题,这里是一个答案:

<?php
include("con_gen.php");

$link = mysqli_connect("localhost","root","")  or die("failed to connect to server !!");
mysqli_select_db($link,"idcard");

$sql = "SELECT static_name FROM static_values";
$result = mysqli_query( $link,$sql);
$ch='rdbtn';
$html = array();
if ($result>0) {
    $cnt=0;

    while( $fetch = mysqli_fetch_assoc( $result ) ) {    
      $html[] ='
        <tr >
              <td>
          <div class="row-fluid">

      <div class="span3 bgcolor">
        <?php echo "<label>'.$fetch["static_name"].'</label>";?>

        <select  id='.$fetch["static_name"].' name='.$fetch["static_name"].' data-live-search="true" class="selectpicker form-control" >

          <?php echo "<option>".$row. "</option>";
           foreach ( $Data->'.$fetch["static_name"].' as $key =>$item) echo "<option value=".$key.">".$item."</option>"; ?>               
          </select> 
                                          </div>
      </div>
        </td>

      </tr>';        
    }
}


echo "<pre>".implode( "\r\n", $html )."</pre>";
?>

注意:我对您的代码进行了一些更改

答案 1 :(得分:0)

更改为:

$result = mysqli_query( $link, $sql);

第一个参数是链接到数据库,第二个参数是查询。

在这里阅读: http://php.net/manual/en/mysqli.query.php

答案 2 :(得分:-1)

请尝试以下代码

$data = array();
while( $fetch = mysqli_fetch_assoc( $result ) ) { 
        $html ='
          <tr>
             <td>
                <div class="row-fluid">
                   <div class="span3 bgcolor">
                      <label>'.$fetch["static_name"].'</label>
                      <select  id='.$fetch["static_name"].' name='.$fetch["static_name"].' data-live-search="true" class="selectpicker form-control" >

                         <option>'.$row. '</option>';
                         foreach ( $Data->'.$fetch["static_name"].' as $key =>$item) {
                             $html .= '<option value="'.$key.'">'.$item.'</option>';          
                         }    
                         $html .= '</select> 
                    </div>
                 </div>
              </td>
            </tr>';
          $data[] = $html;
       }
       mysqli_close($link);
       return implode( "\r\n", $data);

我不确定你在foreach循环中使用的数组。只需使用正确的数组尝试此代码。您在html生成中也遇到了一些问题,我在此处进行了更正。