简体   繁体   English

我正在尝试在回声中调用PHP函数

[英]I'm trying to call a PHP function inside a echo

i'm trying to call getMensClothing() function from function.php to header.php 我正在尝试从function.php到header.php调用getMensClo​​thing()函数

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "khaki";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 


function getMensClothing(){
global $conn;

$sql = "SELECT * FROM men_clothing";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
    echo "<li><a href='#' class='hvr-grow'>". $row['men_clo_items']."      </a></li>";
    }
} 
}

header.php file looks like this header.php文件如下所示

<?php include 'functions.php'; ?>
<?php
     echo' 
          <div class="col-sm-2"><br>
                <a href="men\'s_clothing.php"><p> <b>Men\'s Clothing</b></p></a>
                <ul>
                '.getMensClothing().'
                </ul>
           </div>'
?>

function is called but the items aren't displayed where it has to everything is show at the top of the page . 函数会被调用,但是项目不会显示在页面的顶部。 How to display the items inside the div ?? 如何显示div内的项目?

Use below Code 使用以下代码

$html = "";
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
    $html .= "<li><a href='#' class='hvr-grow'>". $row['men_clo_items']."</a></li>";
    }
} 
return $html;

into your function.php file 到您的function.php文件中

create a class like 创建一个像

class support{

//inside goes codes and functions

}

while calling into another file. 同时调用另一个文件。

include it above file then create 将其包括在文件上方,然后创建

$a=new support()
$a->functionname();

this should do the trick 这应该可以解决问题

What happens is that you use echo with parameters, that are evaluated to be printed. 发生的情况是您将echo与参数一起使用,这些参数被评估为可以打印。
You use concatenation with your arguments to echo . 您可以将串联与自变量结合使用以echo
You have one parameter contructed with the concatenation of three arguments. 您有一个参数与三个参数的串联构成。 The result of this concatenation is printed. 串联的结果被打印出来。 One of these arguments is the returned value of the getMensClothing() function. 这些参数之一是getMensClothing()函数的返回值。

During the evaluation of getMensClothing() you print some data. 在评估getMensClothing()您将打印一些数据。
Consequently, the data printed in your function getMensClothing() gets printed before the end of the call to echo statement in header.php . 因此,在函数getMensClothing()打印的数据在header.phpecho语句的调用结束之前被打印。

As other people pointed out, you should reconsider your technique as your code could be more easy to use if you separate the job of retrieving and constructing your data and the job of displaying it. 正如其他人指出的那样,您应该重新考虑技术,因为如果将检索和构造数据的工作与显示数据的工作分开,则代码将更易于使用。 Have a look to MVC for instance. 例如看一下MVC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM