Reputation: 49
i'm trying to call getMensClothing() function from function.php to header.php
$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
<?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 ??
Upvotes: 2
Views: 286
Reputation: 917
What happens is that you use echo
with parameters, that are evaluated to be printed.
You use concatenation with your arguments to 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.
During the evaluation of getMensClothing()
you print some data.
Consequently, the data printed in your function getMensClothing()
gets printed before the end of the call to echo
statement in header.php
.
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.
Upvotes: 0
Reputation: 356
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
Upvotes: 0
Reputation: 170
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
Upvotes: 4