[英]How can I extract the value of an array key in PHP for MySQL query
I have here a SQL query to get course information from the data base. 我这里有一个SQL查询,用于从数据库中获取课程信息。 The variable courses
is an array saved by Session from the previous page. 可变courses
是Session从上一页保存的数组。 Here is what courses[]
looks like if I var_dump it. 如果我var_dump它,这是course courses[]
样子。 I have selected 3 courses to be put into the session. 我选择了3门课程进行学习。
array(3)
{
[0]=> string(7) "CAD8405"
[1]=> string(7) "CON8413"
[2]=> string(7) "ENG8328"
}
My $selectedYear
variable varies between 2012 to 2014. The is also saved by session. 我的$selectedYear
变量在2012年至2014年之间有所不同。该值也按会话保存。 Below is where I have the problem. 以下是我遇到的问题。 My query is fine, there is no need to check that. 我的查询很好,无需检查。 I cannot get $courseCode
to show up as my Course.CourseCode='$courseCode'
, therefore my query doesn't work. 我无法将$courseCode
显示为我的Course.CourseCode='$courseCode'
,因此我的查询无效。
This is what I found to be where it gets kinda confusing. 我发现这就是令人困惑的地方。 The $courses
array returns a normal array. $courses
数组返回一个普通数组。 but when I do $courses[0] = $courseCode;
但是当我执行$courses[0] = $courseCode;
then var_dump($courseCode) = null
This is what leads me to believe that data I need as my course ID's are the keys to the $courses[]
array. 然后var_dump($courseCode) = null
这就是让我相信我的课程ID所需的数据是$courses[]
数组的键的原因。 When trying to access the $courses[1]
, the code thinks its a multidimensional array with the key as the first data. 尝试访问$courses[1]
,代码会将其视为以键为第一个数据的多维数组。 But I could be wrong Is there better way to extract the key of my array? 但是我可能是错的。是否有更好的方法来提取数组的键?
for($i = 0; $i < count($courses); $i++)
{
//$courses[$i] = $courseCode;
$courseCode = $courses[$i];
$comfirmationString = "SELECT Course.CourseCode, Course.Title, Course.WeeklyHours, Semester.SemesterCode FROM Course, Semester, CourseOffer
WHERE Semester.YearNum='$selectedYear' AND Course.CourseCode='$courseCode'
AND Course.CourseCode=CourseOffer.CourseCode AND Semester.SemesterCode=CourseOffer.SemesterCode";
if($comfirmationResult = mysqli_query($link, $comfirmationString))
{
while($row = mysqli_fetch_assoc($comfirmationResult))
{
echo "<tr><td>$row[CourseCode]</td><td>$row[Title]</td><td>$row[WeeklyHours]</td><td>$row[SemesterCode]</td></tr>";
}
}
else
{
echo "<p>Query error: " + mysqli_error($link) + "</p>";
}
}
Below is the code to show how I have saved the $courses[]
variable: I get the results of a query, and if the check box is checked, this courseID gets thrown into the courses[]
array. 下面是显示如何保存$courses[]
变量的代码:获得查询的结果,如果选中了复选框,则将courseID扔到course courses[]
数组中。
if($Result2013 = mysqli_query($link, $string2013))
{
echo "<form action='CourseSelection.php' method='get'>
<table><tr><th>Code</th><th>Course Title</th><th>Hours</th><th>Term</th><th>Select</th></tr>";
while($row2013 = mysqli_fetch_assoc($Result2013))
{
echo "<tr><td>$row2013[CourseCode]</td><td>$row2013[Title]</td><td>$row2013[WeeklyHours]</td>
<td>$row2013[Term]</td><td><input type='checkbox' name='courses[]' value=$row2013[CourseCode]></td></tr>";
}
echo "</table>";
}
You might want to modify your query to associate the compound values to an easier to read value, like so: 您可能希望修改查询,以将复合值与更易于阅读的值相关联,如下所示:
$comfirmationString = "SELECT Course.CourseCode as courseCode, Course.Title as courseTitle, Course.WeeklyHours as courseWeeklyHours, Semester.SemesterCode as semesterCode FROM Course, Semester, CourseOffer
WHERE Semester.YearNum='$selectedYear' AND Course.CourseCode='$courseCode'
AND Course.CourseCode=CourseOffer.CourseCode AND Semester.SemesterCode=CourseOffer.SemesterCode";
You also need to wrap your variables in brackets, like so: 您还需要将变量包装在方括号中,如下所示:
echo "<tr><td>{$row['CourseCode']}</td><td>{$row['courseTitle']}</td><td>{$row['courseWeeklyHours']}</td><td>$row[SemesterCode]</td></tr>";
and so on. 等等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.