我在处理项目时一直在自学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
的内容并用functions.php中的内容替换行initTable($queryBR, $tblIDBR);
,一切正常,但是;当我尝试分离传递它的函数时,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>";
?>
任何帮助都会有很大的帮助!
答案 0 :(得分:0)
将以下代码添加为函数的第一行,
global $dbAssetManTest;
答案 1 :(得分:0)
因为您现在已经创建了该代码的功能,所以您已经更改了其中使用的变量的范围。
换句话说,主线代码中的变量对函数中的代码不再可见,即$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>";
}
?>
当你调用它时,将参数添加到函数all
<?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;
}
?>
当你调用它时,将参数添加到函数all
<?php
require "functions.php";
// Begin Table generation
$queryBR="select foo from bar;";
$tblIDBR='tblFooBar';
$htm = initTable($dbAssetManTest, $queryBR, $tblIDBR);
echo $htm;
?>