创建包含while循环的函数

时间:2012-04-06 11:28:13

标签: php function while-loop

我是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);
}
?>

非常感谢, 海伦

2 个答案:

答案 0 :(得分:2)

问题在于这一行:

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

每次循环$ reportrows时,只设置为该行的html。

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

修改:用+=替换.=

答案 1 :(得分: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;
}

由于