0

我有一个应用程序,它通过一个 CSV 条目记录月份和年份,即名为answer1is 的db 列jan,2008和 column answer2is feb,2013。我想在 html 中使用一个特定的月份(jan),但它只给了我最后一个数组数据。

<?php
foreach ($row as $colName => $colValue )
  {
  if($colName === answer1 || $colName === answer2)
    {               
     //break apart date data
     $myArray = explode(',', $colValue);
     echo '<div id=" ' .$colName . $myArray[0] . ' ">test</div> ';//this works
        };
  };

?>

我想在文档后面的另一个 php 块中使用类似的内容。

<?php echo '<div id=" '.$colName[answer2] . $myArray[0] . ' ">test</div> ';?>
4

1 回答 1

0

这可能要容易得多。在我们使用 PHP 之前,我们正在从 MySQL 中的原始列中分解(或提取两个子字符串)。然后,您可以随意使用它们:

$query = "SELECT SUBSTRING_INDEX(answer1,',',1) as a1_month,
SUBSTRING_INDEX(answer1,',',-1) as a1_year,
SUBSTRING_INDEX(answer2,',',1) as a2_month,
SUBSTRING_INDEX(answer2,',',-1) as a2_year from my_table";
$result = mysqli_query($query);


$answers = array();
$x = 0;
while ($row = mysqli_fetch_array($result)){

    $answers[$x]['answer1_year'] = $row['a1_year'];
    $answers[$x]['answer1_month'] = $row['a1_month'];
    $answers[$x]['answer2_year'] = $row['a2_year'];
    $answers[$x]['answer2_month'] = $row['a2_month'];
 $x++;
}

print_r($answers);

如果由于某种原因您仍然想要连接的字符串,您可以这样做:

$answer1 = $answers[0]['answer1_month'].','.$answers[0]['answer1_year'];
$answer2 = $answers[0]['answer2_month'].','.$answers[0]['answer2_year'];
于 2013-05-24T22:19:34.400 回答