0

我是 PHP 新手,并试图通过创建一个包含我所有工作的数据库来学习它(我是一名自由设计师)。我已经创建了下面的代码来生成一个表格来显示描述包含徽标的所有作业,该徽标可以正常工作并生成几行......

<?php
$logojobs = mysql_query("SELECT *  FROM hlgd_projects WHERE description LIKE '%logo%'", $connection);
?>

<a href="index.php?report=logo">View logo jobs</a>

<?php
if ($report == 'logo') {         
echo "<h1>Logo jobs</h1>";
echo "<table id='report'>
    <tr>
     <th></th>
     <th>Status</th>
     <th>Lead</th>
     <th>Start</th>
     <th colspan='2'>Codes</th>
     <th>Client</th>
     <th>Description</th>
     <th>Fee</th>
     <th>Contact</th>
     <th colspan='2'>Invoice</th>
     <th>Paid</th>
    </tr>";
 while ($logojob = mysql_fetch_array($logojobs)) { 
    echo "<tr>
        <td class='id'>" . $logojob['id'] . "</td>
        <td>" . $logojob['status_id'] . "</td>
        <td>" . $logojob['lead_id'] . "</td>
        <td>" . $logojob['date_start'] . "</td>
        <td>" . $logojob['code_lead'] . "</td>
        <td>" . $logojob['code_hlgd'] . "</td>
        <td>" . $logojob['client'] . "</td>
        <td>" . $logojob['description'] . "</td>
        <td>&pound;" . $logojob['fee'] . "</td>
        <td>" . $logojob['contact'] . "</td>
        <td>" . $logojob['invoice'] . "</td>
    <td>" . $logojob['date_inv'] . "</td>
        <td>" . $logojob['date_paid'] . "</td>      
    </tr>";             
 }  
 echo "</table>";        
}
?>

但是,我希望能够为许多不同的事物生成报告,因此希望创建一个函数来生成表格并每次传递相关参数。我已经这样做了,但它只生成第一行,所以我认为它忽略了一段时间?谁能告诉我哪里出了问题或者如何更好地将上面的代码放入一个函数中?

<?php
function report_bytype($title,$job_set,$job_name) {
  $reporthead = "<h1>$title</h1>
    <table id='report'>
    <tr>
     <th></th>
     <th>Status</th>
     <th>Lead</th>
     <th>Start</th>
     <th colspan='2'>Codes</th>
     <th>Client</th>
     <th>Description</th>
     <th>Fee</th>
     <th>Contact</th>
     <th colspan='2'>Invoice</th>
     <th>Paid</th>
    </tr>";
  while ($job_name = mysql_fetch_array($job_set)) { 
    $reportrows = "
        <tr>
        <td class='id'>" . $job_name['id'] . "</td>
        <td>" . $job_name['status_id'] . "</td>
        <td>" . $job_name['lead_id'] . "</td>
        <td>" . $job_name['date_start'] . "</td>
        <td>" . $job_name['code_lead'] . "</td>
        <td>" . $job_name['code_hlgd'] . "</td>
        <td>" . $job_name['client'] . "</td>
        <td>" . $job_name['description'] . "</td>
        <td>&pound;" . $job_name['fee'] . "</td>
        <td>" . $job_name['contact'] . "</td>
        <td>" . $job_name['invoice'] . "</td>
        <td>" . $job_name['date_inv'] . "</td>
        <td>" . $job_name['date_paid'] . "</td>     
    </tr>";             
  }                             
  $reportfoot = "</table>"; 
  $reporttable = $reporthead . $reportrows . $reportfoot;
  echo $reporttable;
  return $reporttable; 
}
?>

<?php
if($report == 'logo') {
report_bytype("logo",$logojobs,$logojob);
}
if($report == 'stationery') {
report_bytype("stationery",$stationeryjobs,$stationeryjob);
}
?>

非常感谢,海伦

4

2 回答 2

2

问题是这一行:

while ($job_name = mysql_fetch_array($job_set)) { 
    $reportrows = "lots of html"
}

每次循环 $reportrows 都设置为该行的 html。

使用$reportrows .= "some html";它将每一行添加到 $reportrows 而不是用该行替换 $reportrows。

编辑:替换+=.=.

于 2012-04-06T11:33:47.547 回答
2

我可以看到为什么你有错误你不是concatenating你的结果

请参阅http://php.net/manual/en/language.operators.string.php以获得更详细的解释

代替

$reportrows = "

$reportrows .= "

完整脚本

function report_bytype($title, $job_set, $job_name) {
    $reporthead = "<h1>$title</h1>
    <table id='report'>
    <tr>
    <th></th>
    <th>Status</th>
    <th>Lead</th>
    <th>Start</th>
    <th colspan='2'>Codes</th>
    <th>Client</th>
    <th>Description</th>
    <th>Fee</th>
    <th>Contact</th>
    <th colspan='2'>Invoice</th>
    <th>Paid</th>
    </tr>";

    $reportrows = "";
    while ( $job_name = mysql_fetch_array ( $job_set ) ) {
        $reportrows .= "
    <tr>
    <td class='id'>" . $job_name ['id'] . "</td>
    <td>" . $job_name ['status_id'] . "</td>
    <td>" . $job_name ['lead_id'] . "</td>
    <td>" . $job_name ['date_start'] . "</td>
    <td>" . $job_name ['code_lead'] . "</td>
    <td>" . $job_name ['code_hlgd'] . "</td>
    <td>" . $job_name ['client'] . "</td>
    <td>" . $job_name ['description'] . "</td>
    <td>&pound;" . $job_name ['fee'] . "</td>
    <td>" . $job_name ['contact'] . "</td>
    <td>" . $job_name ['invoice'] . "</td>
    <td>" . $job_name ['date_inv'] . "</td>
    <td>" . $job_name ['date_paid'] . "</td>
    </tr>";
    }
    $reportfoot = "</table>";
    $reporttable = $reporthead . $reportrows . $reportfoot;
    echo $reporttable;
    return $reporttable;
}

谢谢

于 2012-04-06T11:35:51.337 回答