这个令我困惑。我有两个不同的mySQL表我需要从中提取信息,我正在创建一个html下拉列表并尝试从变量中填充值。它第一次通过循环,但不是在那之后。我收到警告(查看HTML时):
b>Warning</b>: array_combine() expects parameter 1 to be array, integer given in <b>/MY URL</b> on line <b>56</b><br />
<br />
更容易显示代码,所以这里是:
$UserID = "118";
$UndefinedEvents = array();
$EventDates = array();
$UserTimelines = array();
$query = "SELECT * FROM events WHERE UserID='$UserID' AND ParentEventID IS NULL";
$result = mysql_query($query);
$query2 = "SELECT * FROM timelines WHERE UserID='$UserID'";
$result2 = mysql_query($query2);
//Getting the Timeline names and their IDs
while($row = mysql_fetch_array($result2))
{
$UserTimelines[] = $row['TimeLineName'];
$TimelineID[] = $row['TimeLineID'];
}
//Getting and events that have a NULL value
while($row = mysql_fetch_array($result))
{
echo $row['UserID'] . " ".$row['EventName']. " " . $row['ParentEventID'];
$UndefinedEvents[] = $row['EventName'];
$EventDates[] = $row['StartDate'];
}
//Iterating through events with NULL value and telling user which events are NULL
foreach (array_combine($UndefinedEvents, $EventDates) as $event=>$dates){
echo "This Event does not have a Timeline associated with it: " .$event . " on ".$dates. '<br>';
echo "Choose a Timeline:<br>";
?>
<select>
//THIS IS LINE 56: Iterating through the timelines and creating a dropdown for the user to choose a timeline.
<?php foreach (array_combine($TimelineID, $UserTimelines) as $TimelineID=>$timeline){
echo "<option value=".$TimelineID."> ".$timeline. "</option>";
}
echo " </select><br><br>";
}
?>
<?php
mysql_close($con);
?>
就像我说的那样,它适用于第一个循环,并且正确地将值放入,但是每个循环之后都会产生上述警告。
答案 0 :(得分:1)
在你的第二个foreach中,你覆盖了$TimelineID
变量:
foreach (array_combine($TimelineID, $UserTimelines) as $TimelineID=>$timeline){
只需使用其他内容:
foreach (array_combine($TimelineID, $UserTimelines) as $temp_TimelineID=>$timeline){
答案 1 :(得分:0)
只是为了检查......在这段代码中,您从一行获取ID和名称,并将它们分成两个独立的数组......
//Getting the Timeline names and their IDs
while($row = mysql_fetch_array($result2))
{
$UserTimelines[] = $row['TimeLineName'];
$TimelineID[] = $row['TimeLineID'];
}
...然后您将这些数组组合在一起以生成选项值/描述?
为什么不在数组的一个元素(数组数组)中的每一行存储两个值?或使用关联数组 - 以id作为键,文本作为值? (或者更好的是,将行映射到对象)