0

我目前正在使用 jQuery Mobile 和 MVC 框架开发移动应用程序。它用作任务/作业管理器,允许用户输入班级信息(姓名、讲师、描述等)和每个班级的作业信息(标题、注释、到期日期等)

我已经使用会话变量创建了一个登录页面,并且可以成功登录。登录后,我有一个默认操作 =“list_assignments”,它采用所有类名并将它们转换为列表视图的分隔符,其中包含每个类下的分配。

我的问题是,登录时我会看到所有用户的每个分配。

我在 MySQL 中有 3 个表,

  • 作业(assign_id[PKey and AutoInc]、assign_title、note、dueDate、user_id、class_id)
  • 类(class_id[Pkey & AI]、class_name、class_instructor、class_description、user_id)
  • 用户(user_id[Pkey & AI]、用户名、密码)

assignment_tracker/index.php(控制器)

if ($action == 'list_assignments') {

    // Get the current user ID
   @ $user_id = $_SESSION['user_id'];
    if (!isset($user_id)) {
        $user_id = 1;
    }
    // Get the current class ID
   @ $class_id = $_GET['class_id'];
    if (!isset($class_id)) {
        $class_id = 1;
    }

    // Get assignment and class data
    $class_name = get_class_name($class_id);
    $classes = get_classes();

    // needs to be looped for each class within assignment_list.php
    $assignments = get_assignments_by_class($class_id);

    // Display the assignment list, or build "the view"
    include('view/assignment_list.php');


}

assignment_tracker/view/assignment_list.php

    <?php include ('header.php'); ?>

    <?php print $_SESSION['user_id']; ?>

    <div data-role="content" id="assignment_list">
    <!-- create 2 foreach loops, 1 for classes to be listed and organized by id in list dividers and another for listing individual assignments within each class -->   
     <ul data-role="listview">

     <!--classes loop-->
     <?php foreach ($classes as $class) : ?>

    <li data-role="list-divider"><?php echo $class['class_name']; ?></li>

    <?php $assignments=get_assignments_by_class($class['class_id']); ?> 
    <?php foreach ($assignments as $assignment) : ?><!--assignments loop-->
    <?php print'<li><a href="index.php?action=show_assign_details&assign_id='.$assignment['assign_id'].'&class_id='.$assignment['class_id'].'">'.$assignment['assign_title'].'</a></li>';?>
    <?php endforeach; ?> <!--/assignments loop-->
    <?php endforeach; ?> <!--/classes loop-->

     </ul>      
    </div><!--/data-role="content"-->


    <?php include 'footer.php'; ?>

assignment_tracker/model/class_db.php

    <?php
function get_classes() {
    global $db;
    $query = 'SELECT * FROM classes
              ORDER BY class_id';
    $result = $db->query($query);
    return $result;
}

function get_class_name($class_id) {
    global $db;
    $query = "SELECT * FROM classes
              WHERE class_id = $class_id";
    $class = $db->query($query);
    $class = $class->fetch();
    $class_name = $class['class_name'];
    return $class_name;
}

assignment_tracker/model/assignment_db.php

<?php
function get_assignments() {
    global $db;
    $query = 'SELECT * FROM assignments
              ORDER BY assign_ID';
    $assignments = $db->query($query);
    return $assignments;
}

function get_assignments_by_class($class_id) {
    global $db;
    $query = "SELECT * FROM assignments
              WHERE assignments.class_id = '".$class_id."'
              ORDER BY assign_id";
    $assignments = $db->query($query);
    return $assignments;
}

我尝试创建一个查询来根据用户的 id 查找分配,但是当我在 foreach 循环中实现解决方案时,我得到了 foreach 错误的无效参数,这意味着我的输入之一不是数组。我已经为此困扰了很多小时,所以我认为是时候使用我的资源并寻求帮助了。

如果您需要更多信息,请告诉我,我感谢所有提前提供帮助的人。

更新:

大家好,

我想出了如何通过将用户 ID 硬编码到我的查询中来获得结果。起初我认为由于某种原因它没有返回数组,但是在 phpMyAdmin 中进行测试后,当 user_id 被硬编码时,我的查询工作正常。

我在 class_db.php 中创建了一个新函数,只返回 user_id 匹配的类。我正在尝试使用$_SESSION['user_id']我在登录时在控制器中设置的变量。

function get_classes_by_user($user_id) {
global $db;
$query = 'SELECT * FROM classes
      WHERE user_id = 1
          ORDER BY class_id';
$result = $db->query($query);
return $result;
}

索引.php

    if ($action == 'list_assignments') {

    // Get the current class ID
   @ $class_id = $_GET['class_id'];
    if (!isset($class_id)) {
        $class_id = 1;
    }
    // Get the current user ID
   @ $user_id = $_SESSION['user_id'];
    if (!isset($user_id)) {
        $user_id = 1;
    }

    // Get assignment and class data
    $class_name = get_class_name($class_id);
    $classes = get_classes();

    //new function being called with resultant array stored in $userclasses
    $userclasses = get_classes_by_user($user_id);

    // needs to be looped for each class within assignment_list.php
    $assignments = get_assignments_by_class($class_id);

    // Display the assignment list, or build "the view"
    include('view/assignment_list.php');

}

assignment_list.php

<?php include ('header.php'); ?>
<?php print $_SESSION['user_id']; ?>
        <div data-role="content" id="assignment_list">
            <!-- create 2 foreach loops, 1 for classes to be listed and organized by id in list dividers and another for listing individual assignments within each class -->   
            <ul data-role="listview">

            <!--classes loop-->

            <?php foreach ($userclasses as $class) : ?>

                <li data-role="list-divider"><?php echo $class['class_name']; ?></li>

                <?php $assignments=get_assignments_by_class($class['class_id']); ?> 
                <?php foreach ($assignments as $assignment) : ?><!--assignments loop-->
                <?php print'<li><a href="index.php?action=show_assign_details&assign_id='.$assignment['assign_id'].'&class_id='.$assignment['class_id'].'">'.$assignment['assign_title'].'</a></li>';?>
                <?php endforeach; ?> <!--/assignments loop-->
            <?php endforeach; ?> <!--/classes loop-->

            </ul>       
        </div><!--/data-role="content"-->


<?php include 'footer.php'; ?>

我将上面 foreach 循环中的 $classes 更改为 $userclasses 以匹配新函数。

有人有想法么?我知道我非常接近解决这个问题!再次感谢您的帮助!

4

1 回答 1

0

好的,所以在搞砸了我的应用程序一段时间后,我终于让它工作了。

在我的 index.php 文件中,我设置$_SESSION['user_id']$user_id并添加了get_classes_by_user($user_id)函数并将结果返回给$userclasses

    if ($action == 'list_assignments') {

    // Get the current class ID
   @ $class_id = $_GET['class_id'];
    if (!isset($class_id)) {
        $class_id = 1;
    }

    // Get the current user ID
   @ $user_id = $_SESSION['user_id'];
    if (!isset($user_id)) {
        $user_id = 1;
    }

    // Get assignment and class data
    $class_name = get_class_name($class_id);
    $classes = get_classes();

    $userclasses = get_classes_by_user($user_id);

    // needs to be looped for each class within assignment_list.php
    $assignments = get_assignments_by_class($class_id);

    // Display the assignment list, or build "the view"
    include('view/assignment_list.php');
    }

在 class_db.php 中,我注意到我的查询有问题。

带单引号的原文不起作用

function get_classes_by_user($user_id) {
    global $db;
    $query = 'SELECT * FROM classes
              WHERE user_id = $user_id
              ORDER BY class_id';
    $result = $db->query($query);
    return $result;
}

用双引号更新并工作

function get_classes_by_user($user_id) {
    global $db;
    $query = "SELECT * FROM classes
              WHERE user_id = $user_id
              ORDER BY class_id";
    $result = $db->query($query);
    return $result;
}

我查看了差异并确定如果查询中有变量,则应该用双引号括起来(不要引用我,大声笑)。我也看到了一个用花括号 {} 包裹的变量的查询,我自己尝试过,使用单引号没有区别,至少对我来说是这样。

于 2013-03-19T03:13:53.477 回答