我有
设置的以下变量$user_id
//Check if user is logged in
session_start();
if (!isset ($_SESSION['user_id']))
{
header("location:login.php");
}
elseif(isset ($_SESSION['user_id']))
{
$user_id = $_SESSION['user_id'];
}
然后在同一个函数文件中我有以下内容:
function course_menu()
{
$sqlSubscription = "SELECT * FROM subscriptions WHERE `user_id` = '".$user_id."'";
$subscriptionResult = mysql_query($sqlSubscription);
while ($rows = mysql_fetch_assoc($subscriptionResult))
{
$user_id = $rows['user_id'];
$course_id = $rows['course_id'];
$course_title = $rows['course_title'];
if ($data_id == $rows['course_id'])
{
echo
'<li>
<a href="chapters.php?course_id=',$course_id,'&course_name=',$course_title,'" class="active">',$course_title,' </a>
</li>';
}
else
{
echo
'<li><a href="chapters.php?course_id=',$course_id,'&course_name=',$course_title,'">',$course_title,' </a></li>';
}
}
}
问题是我每次尝试运行该函数时都会收到未定义的变量user_id。我可以echo $user_id
在另一个页面上使用index.php
然后require_once function.php
说echo $user_id
,但由于某种原因,该功能本身无法访问它?
我认为这可能是因为它超出了它的范围 - 但如果是这样的话,我不完全确定如何应对它。
我的问题是,如何让函数能够使用变量$ user_id?
修改
所以我开始做
$user_id = $_SESSION['user_id'];
global $conn;
$sqlSubscription = "SELECT * FROM subscriptions WHERE `user_id` = '".$user_id."'";
$subscriptionResult = $conn->query($sqlSubscription);
while ($rows = mysqli_fetch_assoc($subscriptionResult))
{
$user_id = $rows['user_id'];
$course_id = $rows['course_id'];
$course_title = $rows['course_title'];
if ($data_id == $rows['course_id'])
{
echo
'<li>
<a href="chapters.php?course_id=',$course_id,'&course_name=',$course_title,'" class="active">',$course_title,' </a>
</li>';
}
else
{
echo
'<li><a href="chapters.php?course_id=',$course_id,'&course_name=',$course_title,'">',$course_title,' </a></li>';
}
}
似乎工作正常,但每次使用函数添加新连接或手动设置$ user_id都有点乏味。有没有办法解决这个问题,因为我有几个函数需要连接到db来提取数据。有没有更好的方法来构建这种类型的东西?我对OOP不是很熟悉,但如果我能得到一些方向,我可以尝试一下,这是我使用的另一个功能(并且至少有另外一个需要数据库连接的5-6)
function render_dashboard()
{
$user_id = $_SESSION['user_id'];
global $conn;
//Following brings up the number of subscription days left on the user dashboard
$sqlDate = "SELECT * FROM subscriptions WHERE `user_id` = '".$user_id."'" ;
$date = $conn->query($sqlDate);
while ($daterows = mysqli_fetch_assoc($date))
{
$course_registered = $daterows['course_title'];
$date_time = $daterows['end_date'];
$calculate_remaining = ((strtotime("$date_time")) - time())/86400;
$round_remaining = round("$calculate_remaining", 0, PHP_ROUND_HALF_UP);
// Here we assign the right term to the amount of time remaining I.E DAY/DAYS/EXPIRED
if($round_remaining > 1)
{
$remaining = $course_registered." ".$round_remaining." "."Days Remaining";
$subscriptionStatus = 2;
echo '<p>',$remaining,'</p>';
}
elseif ($round_remaining == 1)
{
$remaining = $course_registered." ".$round_remaining." "."Day Remaining";
$subscriptionStatus = 1;
echo '<p>',$remaining,'</p>';
}
elseif ($round_remaining <= 0)
{
$remaining = $course_registered." "."Expired"." ".$date_time;
$subscriptionStatus = 0;
echo '<p>',$remaining,'</p>';
}
}
//Check for most recent viewed video
$sqlVideo = "SELECT `last_video` FROM users WHERE `user_id` = '".$user_id."'" ;
$videoResult = $conn->query($sqlVideo);
if ($videoRows = mysqli_fetch_assoc($videoResult))
{
$last_video = $videoRows['last_video'];
$videoLink = "SELECT `chapter_id` FROM chapters WHERE `chapter_title` = '".$last_video."'";
if ($chapteridResult = mysql_fetch_assoc(mysql_query($videoLink)));
{
$chapter_id = $chapteridResult['chapter_id'];
}
$videoLink = "SELECT `course_id` FROM chapters WHERE `chapter_title` = '".$last_video."'";
if ($courseResult = mysql_fetch_assoc(mysql_query($videoLink)));
{
$course_id = $courseResult['course_id'];
}
}
}
答案 0 :(得分:2)
course_menu()
功能无法识别您的 $user_id
,因为它不在其范围内。
使用 global
关键字来解决此问题。
function course_menu()
{
global $user_id;
// your remaining code .........
答案 1 :(得分:1)
在不使用全局的情况下绕过它的解决方案是DEFINE并将其传递给ie define ('var', '$var')
then function x($var)
或依赖注入,如此处所述How can I use "Dependency Injection" in simple php functions, and should I bother?