How To Take Out The Anchor Element From Echo (functions.php) And Place It To Body (index.php)?
How to take out the anchor element from echo (functions.php) and place it to body (index.php)? PS: Newbie here guys, just trying to understand about HTML, CSS, Javascript and PHP a
Solution 1:
You forgot <li></li>
in your code
You need to learn to return an array and to use foreach():
<!doctype html>
<?php include_once("functions/functions.php"); ?>
<html>
<head>
<link rel="stylesheet" href="styles/style.css" type="text/css" />
</head>
<body>
<ul id="cats">
<?
$cats = getCats();
foreach($cats as $cat){
echo "<li><a href='index.php?cat=$cat[cat_id]'>$cat[cat_title]</a></li>";
}
?>
<ul>
</body>
</html>
function getCats(){
global $con;
$get_cats = "select * from categories";
$run_cats = mysqli_query($con, $get_cats);
while ($row_cats=mysqli_fetch_array($run_cats)){
$cats[]=$row_cats;
}
return $cats;
}
Solution 2:
I guess you can try something like this, if you really want to move the < a > elements to the body.However I think that the way you did it was better (there is no reason to move them to the body):
<!--index.php-->
<!doctype html>
<?php include_once("functions/functions.php"); ?>
<html>
<head>
<link rel="stylesheet" href="styles/style.css" type="text/css" />
</head>
<body>
<ul id="cats">
<?php
$get_cats = "select * from categories";
$run_cats = mysqli_query($con, $get_cats);
while ($row_cats=mysqli_fetch_array($run_cats))
{
$cat_id = $row_cats['cat_id'];
$cat_title = $row_cats['cat_title'];
?>
<a href='index.php?cat=<?php echo $cat_id;?>'><?php echo $cat_title;?></a>
<?php
}
?>
</ul>
</body>
And in functions.php you just need to leave the mysql connection object:
$con = mysqli_connect("localhost","root","","justLearning");
if (mysqli_connect_errno())
{
echo "The connection was not established: " . mysqli_connect_error();
}
Solution 3:
I'm guessing here but I reckon the problem is that the includes_path
is set to the default and the included file isn't being found???
<?php
set_include_path( $_SERVER['DOCUMENT_ROOT'] . '/functions/' );
include_once( "functions.php" );
?>
<!--index.php-->
<!doctype html>
<html>
<head>
<title>There must be a title to be considered VALID html</title>
<link rel="stylesheet" href="styles/style.css" type="text/css" />
</head>
<body>
<ul id="cats">
<?php
getCats();
?>
</ul>
</body>
</html>
Post a Comment for "How To Take Out The Anchor Element From Echo (functions.php) And Place It To Body (index.php)?"