0

我的网站上有一个包含 4 列的表格。我正在使用 ACF 自定义字段来输入我的内容。

 $string = get_field('projets');

我的字符串被分成两个 div,date_index 和 texte_index ... date_index 显示日期,text_index 显示文本,所以每个字符串在找到空格时都会被分割。像这样 :

这是我输入的内容 = 2013-07 PARIS COLLÈGE DE FRANCE,它的显示方式如下:

<div class="date_index">2013-07</div><div class="texte_index">PARIS COLLÈGE DE FRANCE</div>

它工作正常。

然后我的第一个数组被分成两列,当第一列有 130 行时,下一行显示在另一列中。

这是我的代码:

<div class="columns_projets_1">
<p><?php 
$string = get_field('projets'); 
$array = explode("\n", $string);
for($i = 0; $i <130; $i++){
$dateAndText= explode(' ', $array[$i], 2);
echo '<div class="date_index">'.$dateAndText[0].'</div>';
echo '<div class="texte_index">'.$dateAndText[1].'</div>';
}
?></p>
</div>

<div class="columns_projets_2">
<p><?php 
$string = get_field('projets'); 
$array = explode("\n", $string);
for($i = 130; $i <count($array)-1; $i++){
$dateAndText= explode(' ', $array[$i], 2);
echo '<div class="date_index">'.$dateAndText[0].'</div>';
echo '<div class="texte_index">'.$dateAndText[1].'</div>';
}
?></p>
</div>

然后我有第三列,这里是代码:

<div class="columns_conferences">
<p><?php 
$string = get_field('conferences'); 
$array = explode("\n", $string);
for($i = 0; $i <300; $i++){
$dateAndText= explode(' ', $array[$i], 2);
echo '<div class="date_index">'.$dateAndText[0].'</div>';
echo '<div class="texte_index">'.$dateAndText[1].'</div>';
}
?></p>
</div>

和我的第 4 列:

<div class="columns_monographies">
<p><?php 
$string = get_field('monographies'); 
$array = explode("\n", $string);
for($i = 0; $i <300; $i++){
$dateAndText= explode(' ', $array[$i], 2);
echo '<div class="date_index">'.$dateAndText[0].'</div>';
echo '<div class="texte_index">'.$dateAndText[1].'</div>';
}
?></p>
</div>

首先,我想删除第 3 列和第 4 列的条件( $i <300 ),我不想在这两列中有限制,但是当我尝试删除条件时,什么都没有显示...

其次,我想计算第三列中的行数,这个数字将是我第一列的限制。所以我的第一列和第三列将有相同的行号。

你明白我的意思吗 ?它应该是这样的,但我无法让它工作:

在我的第一列:

for($i = 0; $i <130; $i++){

而不是 130,是我的第三列数组的行数。

和第二列;

for($i = 130; $i

而不是 130,是我的第三列数组的行数。

我真的希望你能帮助我,

这是一个github链接:

https://gist.github.com/mattieumoreau/7431037

非常感谢你的帮助,

4

2 回答 2

0

要解决您的第一个问题,请使用 sizeof(array) 而不是特定数字,例如:

for($i = 0; $i < sizeof($array); $i++){
    $dateAndText= explode(' ', $array[$i], 2);
    echo '<div class="date_index">'.$dateAndText[0].'</div>';
    echo '<div class="texte_index">'.$dateAndText[1].'</div>';
}

编辑:你的第三列看起来有点像这样:

<div class="columns_conferences">
    <p><?php
        $string = get_field('conferences');
        $array = explode("\n", $string);
        for($i = 0; $i < sizeof($array); $i++){
            $dateAndText= explode(' ', $array[$i], 2);
            echo '<div class="date_index">'.$dateAndText[0].'</div>';
            echo '<div class="texte_index">'.$dateAndText[1].'</div>';
        }
        ?></p>
</div>
于 2013-11-12T13:58:16.090 回答
0

这里有几个问题。

  1. 如果您删除条件,则您的 for 循环要么出错,要么永远持续(取决于您尝试删除它的方式)。要获取所有记录,您需要for ($i = 0; $i < count($array); $i += 1) { ...

  2. 要将“项目”的第一列的长度限制为“会议”的长度,您需要提前获取数据,然后显示它。这就是我的意思:

    $projets = explode("\n", get_field('projets'));
    $conferences = explode("\n", get_field('conferences'));
    $monographies = explode("\n", get_field('monographies'));
    
    // Now we want to limit the length of the first column of projets (col 1) to the
    // length of conferences (col 3).
    $projets = array_chunk($projets, count($conferences)); // Split the projets into a set of arrays of the right length.
    $projets1 = array_shift($projets); // Grab the first one... that's the data for the first column.
    $projets2 = array_reduce($projets, 'array_merge', array()); // Merge the remaining arrays into a single array for display.
    

    接着 :

    function displayDateAndText ($obj)
    {
        $dateAndText = explode(' ', $obj, 2);
        echo "<div class=\"date_index\">{$dateAndText[0]}</div>";
        echo "<div class=\"texte_index\">{$dateAndText[1]}</div>";
    }
    function displayRecords ($records)
    {
        echo '<p>';
        $limit = count($records);
        for ($i = 0; $i < $limit; $i += 1)
        {
            displayDateAndText($records[$i]);
        }
        echo '</p>';
    }
    ?>
    <div class="columns_projets_1">
        <?php
           displayRecords($projets1);
        ?>
    </div>
    <div class="columns_projets_2">
        <?php
           displayRecords($projets2);
        ?>
    </div>
    <div class="conferences">
        <?php
           displayRecords($conferences);
        ?>
    </div>
    <div class="monographies">
        <?php
            displayRecords($monographies);
        ?>
    </div>
    
于 2013-11-12T13:58:59.433 回答