从位于不同php页面的函数内的数组获取字符串
如何从函数中获取数组字符串值,并在位于另一页的表中分别回显它们 functions.php:从位于不同php页面的函数内的数组获取字符串,php,arrays,function,variables,Php,Arrays,Function,Variables,如何从函数中获取数组字符串值,并在位于另一页的表中分别回显它们 functions.php: function get_program() { $connection = db_connect(); $username = $_SESSION['username']; $query = "SELECT * FROM utilizatori WHERE username = '$username'"; $r
function get_program()
{
$connection = db_connect();
$username = $_SESSION['username'];
$query = "SELECT * FROM utilizatori WHERE username = '$username'";
$results = mysqli_query($connection, $query);
$array = mysqli_fetch_array($results, MYSQLI_ASSOC);
return $dateOne = $array['weekOneFirst'];
return $dateTwo = $array['weekTwoFirst'];
}
<?php require_once 'functions.php'; ?>
<div>
<?php get_program(); ?>
<table>
<tr>
<th>
Week 1
</th>
<th>
Week 2
</th>
</tr>
<tr>
<td>
<?php echo $dateOne; ?> // date 1 from array string here
</td>
<td>
<?php echo $dateTwo; ?> // date 2 from array string here
</td>
</tr>
</table>
</div>
index.php:
function get_program()
{
$connection = db_connect();
$username = $_SESSION['username'];
$query = "SELECT * FROM utilizatori WHERE username = '$username'";
$results = mysqli_query($connection, $query);
$array = mysqli_fetch_array($results, MYSQLI_ASSOC);
return $dateOne = $array['weekOneFirst'];
return $dateTwo = $array['weekTwoFirst'];
}
<?php require_once 'functions.php'; ?>
<div>
<?php get_program(); ?>
<table>
<tr>
<th>
Week 1
</th>
<th>
Week 2
</th>
</tr>
<tr>
<td>
<?php echo $dateOne; ?> // date 1 from array string here
</td>
<td>
<?php echo $dateTwo; ?> // date 2 from array string here
</td>
</tr>
</table>
</div>
第一周
第2周
//此处为数组字符串中的日期1
//此处为数组字符串中的日期2
它返回错误:
注意:未定义变量:dateOne在
注意:未定义变量:dateTwo in
有其他的方法来做你正在做的事情,但这可能会为你的未来提供最好的服务,并且更加灵活: 在
get_program()
中,您只需要1return
并返回数组:
return $array;
然后在index.php
中:
<?php $result = get_program(); ?>
html here
<?php echo $result['weekOneFirst']; ?>
<?php echo $result['weekTwoFirst']; ?>
哈哈。实际上我的问题是我不知道如何以正确的方式调用函数。谢谢你的帮助。这解决了问题。我检索了更多的列,我将坚持
select*
。