0

我设置了以下$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.phpand then 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 有点乏味。有什么办法可以解决这个问题,因为我有几个需要连接到数据库来提取数据的功能。有没有更好的方法来构建这种类型的东西?我对 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'];
     }
 }


}
4

2 回答 2

2

该功能course_menu()将无法识别您的$user_id,因为它超出了其范围。

使用global关键字来解决这个问题。

function course_menu()
  {
   global $user_id;
   // your remaining code .........
于 2013-10-06T09:42:26.200 回答
1

在不使用 global 的情况下绕过它的解决方案是定义并通过 ie 传递它 -define ('var', '$var') then function x($var)或者如此处所述的依赖注入如何在简单的 php 函数中使用“依赖注入”,我应该打扰吗?

于 2013-10-06T22:37:46.783 回答