PHP我的代码失败了

时间:2016-06-24 18:50:27

标签: php html

我是个白痴。 2个小时看似简单的东西,我不明白。 我有一个这样的数组:

    Date      | Surname
   10/06/2016    Alex
   10/06/2016    Marc
   12/06/2016    John
   12/06/2016    Steve
   13/06/2016    Elliot

我想要的是:

<div>
<div><h4>10/06/2016</h4></div>
<ul>
<li>Alex</li><li>Marc</li>
</ul>
</div>
<div>
<div><h4>12/06/2016</h4></div>
<ul>
<li>John</li><li>Steve</li>
</ul>
</div>
<div>
<div><h4>13/06/2016</h4></div>
<ul>
<li>Elliot</li>
</ul>
</div>

这是我失败的代码:

<?php

$dateShareHistoric = '';
$createShareHistoric = false;

foreach ($listFileShareHistoric as $tFilesShareHistoric) {
if($dateShareHistoric == $tFilesShareHistoric['dateShare'])
{
?>
<li rel="<?php echo $tFilesShareHistoric['idFiles']; ?>" data-uniqueid="<?php echo $tFilesShareHistoric['uniqueid']; ?>"><?php echo $tFilesShareHistoric['dateShare'] .' :: '. $tFilesShareHistoric['nomDonne']; ?></li>
<?php
}
else if($createShareHistoric){
$createShareHistoric = false;
?>
</ul></p></div>
<div class="callout callout-info"><h4><?php echo $tFilesShareHistoric['dateShare']; ?></h4><p><ul>
<li rel="<?php echo $tFilesShareHistoric['idFiles']; ?>" data-uniqueid="<?php echo $tFilesShareHistoric['uniqueid']; ?>"><?php echo $tFilesShareHistoric['dateShare'] .' :: '. $tFilesShareHistoric['nomDonne']; ?></li>
<?php
}
else{
$createShareHistoric = true;
?>
<div class="callout callout-info"><h4><?php echo $tFilesShareHistoric['dateShare']; ?></h4><p><ul>
<li rel="<?php echo $tFilesShareHistoric['idFiles']; ?>" data-uniqueid="<?php echo $tFilesShareHistoric['uniqueid']; ?>"><?php echo $tFilesShareHistoric['dateShare'] .' :: '. $tFilesShareHistoric['nomDonne']; ?></li>
<?php
}
$dateShareHistoric = $tFilesShareHistoric['dateShare'];

}
对不起,我很惭愧。如果需要,我可以发布它给我的图片。但它的逻辑div包含其他div和其他div ......

enter image description here

1 个答案:

答案 0 :(得分:1)

根据你的php和输出图像,我猜你的数组是这样的:

// Custom UITableViewCell

override func awakeFromNib() {
    super.awakeFromNib()

    let tapGR = UITapGestureRecognizer(target: self, action: #selector(collectionViewTapped(_:)))
    tapGR.numberOfTapsRequired = 1
    self.collectionView.addGestureRecognizer(tapGR)
}

func collectionViewTapped(gr: UITapGestureRecognizer) {
    let point = gr.locationInView(self.collectionView)
    if let indexPath = self.collectionView.indexPathForItemAtPoint(point) {
        // Do stuff
    }
}

在我看来,最好按$data = [ [ 'dateShare' => '10/06/2016', 'nomDonne' => 'Alex' ], [ 'dateShare' => '10/06/2016', 'nomDonne' => 'Marc' ], [ 'dateShare' => '12/06/2016', 'nomDonne' => 'John' ], [ 'dateShare' => '12/06/2016', 'nomDonne' => 'Steve' ], [ 'dateShare' => '13/06/2016', 'nomDonne' => 'Elliot' ] ]; 转换给定的数组和组项,这样可以使其余的更容易

dateShare

现在我们可以迭代给定的数组并构建一个你需要的html

$groupedByDate = [];
foreach ($data as $item) {
    $groupedByDate[$item['dateShare']][] = $item;
}

请注意,它会执行大量字符串连接,但最终结果是您想要的:

$html = '';
foreach ($groupedByDate as $date => $items) {
    $html .= "<div><div><h4>{$date}</h4></div><ul>";
    foreach ($items as $item) {
        $html .= "<li>{$item['nomDonne']}</li>";
    }
    $html .= "</ul></div>\n";
}
echo $html;