我目前正在使用jQuery Mobile和MVC框架开发移动应用程序。 它作为一个任务/任务管理器,允许用户输入类的信息(姓名,讲师,描述等)和分配信息为每个类(标题,备注的dueDate等)在jQuery移动应用程序中显示用户特定的类和任务
我创建了一个登录页面使用会话变量并可以成功登录。登录后,我有一个默认行动=“list_assignments”,它采用所有类名称,并将它们转换为分列表,以便在每个类下分配列表视图。
我的问题是,当登录时,我看到所有用户的每个任务。
我有在MySQL 3个表,
- 分配(assign_id [PKEY和AUTOINC],assign_title,笔记,的dueDate,USER_ID,类标识码)
- 类(类标识码[PKEY & AI],CLASS_NAME, class_instructor,class_description,USER_ID)
- 用户(USER_ID [PKEY & AI],用户名,密码)
assignment_tracker/index.php的(CONTRO米勒)
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 /视图/ 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 /模型/ 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 /模型/ 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;
}
我试过crea根据用户的id找到分配的查询,但是当我在foreach循环中实现一个解决方案时,我得到了一个foreach错误的无效参数,这意味着我的一个输入不是数组。我一直坚持了很多个小时,所以我觉得是时候使用我的资源并寻求帮助。
如果您需要更多信息,请让我知道,我感谢所有提前帮助的人。
UPDATE:
大家好,
我想通了如何通过硬编码的用户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;
}
的index.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以匹配新函数。
任何人有任何想法?我知道我非常接近解决这个问题!再次感谢您的帮助!
[**请不要在新代码中使用'mysql_ *'函数**](http://bit.ly/phpmsql)。他们不再被维护[并被正式弃用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**红框**](http://j.mp/Te9zIL)?学习[*准备的语句*](http://j.mp/T9hLWi),并使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [这篇文章](http://j.mp/QEx8IB)将帮助你决定哪个。如果你选择PDO,[这里是一个很好的教程](http://j.mp/PoWehJ)。 – 2013-03-18 13:16:01
@teresko感谢您的提示,能否请您更具体地指出我在这里抛弃了错误的'mysql_ *'函数? – Domenick 2013-03-18 13:32:00