Как вынуть анкерный элемент из эха (functions.php) и поместить его в тело (index.php)?

Как вынуть анкерный элемент из эха (functions.php) и поместить его в тело (index.php)? PS: Новичок здесь, ребята, просто пытаюсь понять о HTML, CSS, Javascript и PHP примерно 2 недели назад .. просто представьте, что я узнал до сих пор, пожалуйста, не затрудняйте понимание .. просто напишите 🙂 Спасибо ,

<!--index.php--> <!doctype html> <?php include ("functions/functions.php"); ?> <html> <head> <link rel="stylesheet" href="styles/style.css" type="text/css" /> </head> <body> <ul id="cats"> <?php getCats(); ?> </ul> </body> </html> //functions.php <?php $con = mysqli_connect("localhost","root","","justLearning"); if (mysqli_connect_errno()) { echo "The connection was not established: " . mysqli_connect_error(); } function getCats(){ global $con; $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']; echo "<a href='index.php?cat=$cat_id'>$cat_title</a>"; } } ?> 

Solutions Collecting From Web of "Как вынуть анкерный элемент из эха (functions.php) и поместить его в тело (index.php)?"

Я думаю, вы можете попробовать что-то подобное, если вы действительно хотите переместить элементы <a> в тело. Однако я думаю, что так, как вы это делали, было лучше (нет причин переместить их в тело):

 <!--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> 

И в functions.php вам просто нужно оставить объект подключения mysql:

 $con = mysqli_connect("localhost","root","","justLearning"); if (mysqli_connect_errno()) { echo "The connection was not established: " . mysqli_connect_error(); } 

Я угадываю здесь, но я считаю, что проблема в том, что includes_path установлен по умолчанию, а входящий в него файл не найден ???

 <?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> 

Вы забыли <li></li> в своем коде

Вам нужно научиться возвращать массив и использовать 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; }