Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我做错了什么?使用foreach循环输入<;的值字段;选项>;_Php_Mysql - Fatal编程技术网

Php 我做错了什么?使用foreach循环输入<;的值字段;选项>;

Php 我做错了什么?使用foreach循环输入<;的值字段;选项>;,php,mysql,Php,Mysql,这个让我困惑。我需要从两个不同的mySQL表中提取信息,我正在创建一个html下拉表,并尝试从变量中填充值。它在第一次通过循环时工作,但在那之后不工作。我收到一条警告(查看html时): b>警告:array_combine()要求参数1为数组,第56行/MY URL中给出的整数 只需显示代码就更容易了,因此如下所示: $UserID = "118"; $UndefinedEvents = array(); $EventDates = array(); $UserTimelines = ar

这个让我困惑。我需要从两个不同的mySQL表中提取信息,我正在创建一个html下拉表,并尝试从变量中填充值。它在第一次通过循环时工作,但在那之后不工作。我收到一条警告(查看html时):

b>警告:array_combine()要求参数1为数组,第56行/MY URL中给出的整数

只需显示代码就更容易了,因此如下所示:

$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);  
?>
$UserID=“118”;
$UndefinedEvents=array();
$EventDates=array();
$usertimeline=array();
$query=“从UserID='$UserID'和ParentEventID为NULL的事件中选择*”;
$result=mysql\u query($query);
$query2=“从时间线中选择*,其中UserID='$UserID';
$result2=mysql\u查询($query2);
//获取时间轴名称及其ID
while($row=mysql\u fetch\u数组($result2))
{
$UserTimelines[]=$row['TimeLineName'];
$TimelineID[]=$row['TimelineID'];
}
//获取具有空值的事件和事件
while($row=mysql\u fetch\u数组($result))
{
echo$row['UserID']。.$row['EventName']。.$row['ParentEventID'];
$UndefinedEvents[]=$row['EventName'];
$EventDates[]=$row['StartDate'];
}
//遍历具有NULL值的事件并告诉用户哪些事件为NULL
foreach(数组\u组合($UndefinedEvents,$EventDates)为$event=>$dates){
echo“此事件没有与之关联的时间线:“$Event.”在“$dates.”
”; echo“选择时间线:
”; ?> //这是第56行:遍历时间线并创建下拉列表供用户选择时间线。

正如我所说,它在第一个循环中工作,并正确地将值放入,但每个循环之后都会创建上述警告。

在第二个foreach中,您正在覆盖
$TimelineID
变量:

foreach (array_combine($TimelineID, $UserTimelines) as $TimelineID=>$timeline){
只需使用其他方法,如:

foreach (array_combine($TimelineID, $UserTimelines) as $temp_TimelineID=>$timeline){

检查一下……在这段代码中,您从一行中获取ID和名称,并将它们放入两个单独的数组中

//Getting the Timeline names and their IDs
while($row = mysql_fetch_array($result2))
  {
    $UserTimelines[] = $row['TimeLineName'];
    $TimelineID[] = $row['TimeLineID'];
  }
…然后将这些数组组合在一起以生成选项值/说明

为什么不将每行中的两个值都存储在数组的一个元素中(数组数组数组)?或者使用关联数组-id作为键,文本作为值?(或者更好的是,将行映射到对象)


啊!谢谢!我完全错过了。我确实意识到我还忘了初始化$TimelineID=array();但这很奇怪,因为它似乎并不在意???@Joel最好将它初始化为空数组,以防已经有了一些东西……我想在我的学习中,我很高兴能够构建一些可以工作的东西。:)上面的代码几乎是我用它实现所需的所有代码,所以我看不到它这真的为我节省了很多做关联数组的工作。然后要研究的一个短语是“技术债务”。当你在六个月后看这段代码时,你会诅咒自己:-)
//Getting the Timeline names and their IDs
while($row = mysql_fetch_array($result2))
  {
    $UserTimelines[] = $row['TimeLineName'];
    $TimelineID[] = $row['TimeLineID'];
  }