簡體   English   中英

用於表生成的外部php包含

[英]external php inclusion for table generation

我在進行項目時一直在自學PHP,因此我的代碼可能不是最優雅的,我仍在學習。 我真的很想整理一下我的代碼,並提供明智的功能來替換大量的意大利面條代碼。 我已經使測試功能可以正常工作,但是現在我想使用一個函數來生成表,但是我遇到了問題並且沒有錯誤,這使得調試變得很麻煩。 我有三個文件“ functions.php”,“ tblDesc.php”和“ index.php”,如下所示

index.php文件:

<html>
<?php
require 'header.php'
?>
 <head>
    <title>
      Asset Manager
     </title>
 </head>
 <body>
 <?php
 include_once('navBar.php');
 ?>
<?php
include_once('tblDesc.php');
?>
    </body>

的functions.php:

<?php
function initTable($query, $tblID){
    // Begin Table generation

    print "<table id='".$tblID."'> ";
    $result = $dbAssetManTest->query($query);
    $row = $result->fetch(PDO::FETCH_ASSOC);
    print "<thead>";
    print " <tr> ";
    // pulls field data for table generation
    foreach ($row as $field => $value){
        print " <th>$field</th> ";
    }
    print " </tr> ";
    print "</thead>";
    // end foreach
    //body of Table
    print "<tbody>";
    // pulls live data from DB
    $data = $dbAssetManTest->query($query);
    $data->setFetchMode(PDO::FETCH_ASSOC);
    foreach($data as $row){
        print " <tr> ";
        foreach ($row as $name=>$value){
            print " <td>$value</td> ";
        } // end field loop
        print " </tr> ";
    } // end record loop
    print "</tbody>";
    print "</table>";
}
?>

tblDesc.php

<?php
require "functions.php";

// Begin Table generation
$queryBR="select foo from bar;";
$tblIDBR='tblFooBar';

initTable($queryBR, $tblIDBR);
?>

如果我將函數initTable的內容替換為initTable($queryBR, $tblIDBR); 使用functions.php中的內容,一切正常,但是; 當我嘗試分隔傳遞給它的函數vaulues查詢和tableID時,我的頁面停止在<table id='tblBROnHand'>處加載。

因此,此方法有效,但看起來很糟糕:

<?php
// Begin Table generation
$queryBR="select foo from bar;";
$tblID='tblFooBar';
// Begin Table generation
print "<table id='".$tblID."'> ";
$result = $dbAssetManTest->query($queryBR);
$row = $result->fetch(PDO::FETCH_ASSOC);
print "<thead>";
print " <tr> ";
// pulls field data for table generation
foreach ($row as $field => $value){
    print " <th>$field</th> ";
}
print " </tr> ";
print "</thead>";
// end foreach
//body of Table
print "<tbody>";
// pulls live data from DB
$data = $dbAssetManTest->query($queryBR);
$data->setFetchMode(PDO::FETCH_ASSOC);
foreach($data as $row){
    print " <tr> ";
    foreach ($row as $name=>$value){
        print " <td>$value</td> ";
    } // end field loop
    print " </tr> ";
} // end record loop
print "</tbody>";
print "</table>";
?>

任何幫助將不勝感激!

將以下代碼添加為函數的第一行,

global $dbAssetManTest;

因為您現在已經使用了該代碼的功能,所以您更改了其中使用的變量的范圍。

換句話說,主行代碼中的變量對於函數內的代碼不再可見,即$dbAssetManTest變量

因此,將該變量作為參數傳遞給函數,然后它將在函數內部可見並可用

<?php
function initTable($dbAssetManTest, $query, $tblID){
    // Begin Table generation

    print "<table id='".$tblID."'> ";
    $result = $dbAssetManTest->query($query);
    $row = $result->fetch(PDO::FETCH_ASSOC);
    print "<thead>";
    print " <tr> ";
    // pulls field data for table generation
    foreach ($row as $field => $value){
        print " <th>$field</th> ";
    }
    print " </tr> ";
    print "</thead>";
    // end foreach
    //body of Table
    print "<tbody>";
    // pulls live data from DB
    $data = $dbAssetManTest->query($query);
    $data->setFetchMode(PDO::FETCH_ASSOC);
    foreach($data as $row){
        print " <tr> ";
        foreach ($row as $name=>$value){
            print " <td>$value</td> ";
        } // end field loop
        print " </tr> ";
    } // end record loop
    print "</tbody>";
    print "</table>";
}
?>

當您調用它時,將參數添加到所有函數中

<?php
require "functions.php";

// Begin Table generation
$queryBR="select foo from bar;";
$tblIDBR='tblFooBar';

initTable($dbAssetManTest, $queryBR, $tblIDBR);
?>

最好不要直接從函數或方法中輸出,而是構建要輸出的字符串並從函數中將其返回的更好的主意。 這樣,可以在調用后影響輸出。 在這種情況下不容易,但是作為一般規則,至少有可能

所以

<?php
function initTable($dbAssetManTest, $query, $tblID){
    // Begin Table generation

    $htm = "<table id='".$tblID."'> ";
    $result = $dbAssetManTest->query($query);
    $row = $result->fetch(PDO::FETCH_ASSOC);
    $htm .= "<thead>";
    $htm ,=  " <tr> ";
    // pulls field data for table generation
    foreach ($row as $field => $value){
        $htm .= " <th>$field</th> ";
    }
    $htm .= " </tr> ";
    $htm .= "</thead>";
    // end foreach
    //body of Table
    $htm .= "<tbody>";
    // pulls live data from DB
    $data = $dbAssetManTest->query($query);
    $data->setFetchMode(PDO::FETCH_ASSOC);
    foreach($data as $row){
        $htm .= " <tr> ";
        foreach ($row as $name=>$value){
            $htm .= " <td>$value</td> ";
        } // end field loop
        $htm .= " </tr> ";
    } // end record loop
    $htm .= "</tbody>";
    $htm .= "</table>";

    return $htm;
}
?>

當您調用它時,將參數添加到所有函數中

<?php
require "functions.php";

// Begin Table generation
$queryBR="select foo from bar;";
$tblIDBR='tblFooBar';

$htm = initTable($dbAssetManTest, $queryBR, $tblIDBR);
echo $htm;
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM