0

我是 PHP n00b。我有这个代码,但它在我的主页上。我假设像 jQuery 一样,必须有一种方法可以在用户访问的主文档上没有实际脚本的情况下显示它?

或者有没有办法让我在不重复所有内容的情况下更整洁?

        include('talentsearch.php');

        while($row = mysqli_fetch_array($result2))
        {
        echo "<div class='talent_result_wrapper' data-experience='" . $row['divTagExp'] . "' data-salary='" . $row['divTagExp'] . "'>";
        echo "<ul>";
        echo "<li><strong>Talent ID:  </strong>" . $row['CandidateID'] . "</li>";
        echo "<li><strong>Resides:  </strong>" . $row['Town'] . "</li>";
        echo "<li><strong>Salary Required:  </strong>£" . $row['SalaryMin'] . "</li>";
        echo "<li><strong>Experience:  </strong>" . $row['CandidateExperience'] . " Years </li>";
        echo "<li><strong>Industy:  </strong>" . $row['PrimarySector'] . "</li>";
        echo "<li><strong>Specialism:  </strong>" . $row['PrimarySector'] . "</li>";
        echo "</div>";
        }   

我试过把它放在这样的函数中:

function getTalent() {

}

然后像这样调用函数

<?php
include('talentsearch.php');
getTalent()
?>

但是如果我只是正常运行代码,我会得到一个错误。

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in

所以我认为我做的不对。

4

1 回答 1

0

这是你想要达到的目标吗?

talentsearch.php file

function getTalent($row) {

     return "<div class='talent_result_wrapper' data-experience='" . $row['divTagExp'] . "' data-salary='" . $row['divTagExp'] . "'>
        <ul>
          <li><strong>Talent ID:  </strong>" . $row['CandidateID'] . "</li>
          <li><strong>Resides:  </strong>" . $row['Town'] . "</li>
          <li><strong>Salary Required:  </strong>£" . $row['SalaryMin'] . "</li>
          <li><strong>Experience:  </strong>" . $row['CandidateExperience'] . " Years </li>
          <li><strong>Industy:  </strong>" . $row['PrimarySector'] . "</li>
          <li><strong>Specialism:  </strong>" . $row['PrimarySector'] . "</li>
           </div>";
    }

/////// 
main.php file     
include('talentsearch.php');

while($row = mysqli_fetch_array($result2)) {
  echo getTalent($row);
}   
于 2013-09-10T16:47:15.383 回答