对于每一行,自动为同一表格行内的变量生成索引号?

时间:2016-01-20 18:18:08

标签: php html mysql

我有很多表行,我根据计划从数据库中提取的项目数来选择。

我没有自动渲染行,因为在其中一些我希望可以自由地包含一些没有特定统一的静态元素(请参阅代码中的ABCXYZ例如)。

对于每个项目(人)我还手动设置PHP文件本身,其特定行的一些其他相关值,例如$company[i]

PHP:

<?php
// Choose Relevant people, and turn them into an array
$item_array = array(
'Mike',
'Bill',
'Joe'
);

//implode items, turn into string
$item_implode = join("','", $item_array);

//declare an overall array for result
$person = array();

$personList = array();
$result = $mysqli->query("SELECT available from people_table where available IN ('$item_implode') ORDER BY FIELD (available, '$item_implode');");

if ($result->num_rows > 0) {
    $x = 1;
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $person[$x]["available"] = $row['available'];

        $x = $x + 1;
    }
} else {
    echo "0 results";
}

// Manually Set Values:

$hide1=false;
$comment1="hello";
$company1="apple";

$hide2=false;
$comment2="something";
$company2="microsoft";

// And so on...

?>

由于我为每一行添加了另一个代码块,我必须在每个块中替换索引号,根据HTML代码中的行的顺序,这意味着在每个块中,我应该CTRL -F的数字,并用以下数字替换它,这样效率不高:

HTML:

// First Row

<?php if ($person[1]["available"]=="yes") { ?> 
           <tr<?= !$hide1 ? "" : " class=\"hidden\""; ?>>
            <td>ABC</td>
            <td><?= !$comment1 ? "" : "<div class=\"cell\">" . $comment1 . "</div>"; ?></td>
            <td><?= some_function( $company1 ) ?></td>
          </tr>
<?php } else { } ?>

// Second Row

<?php if ($person[2]["available"]=="yes") { ?> 
           <tr<?= !$hide2 ? "" : " class=\"hidden\""; ?>>
            <td>XYZ</td>
            <td><?= !$comment2 ? "" : "<div class=\"cell\">" . $comment2 . "</div>"; ?></td>
            <td><?= some_function( $company2 ) ?></td>
          </tr>
<?php } else { } ?> 

// and so on

有没有办法让每个变量末尾出现的索引编号根据HTML中的行号自动生成?

修改

感谢Andrew Cheong的回答,我取得了进步:

  1. 我将所有以固定数字结尾的变量切换为[i]形式。
  2. 我在每个表行中添加了一个while循环(代码启动后一行,代码结束前另一行),这样我就可以只指示一次索引号:
  3. 所以它工作正常,但我认为它还不完美,我还应该如何修改呢?

    新代码:

    // First Row
    
    <?php if ($person[1]["available"]=="yes") { ?> 
    
    <?php $i = 1; while ($i > 0) { ?>   /// New Line #1
    
               <tr<?= !$hide[$i] ? "" : " class=\"hidden\""; ?>>
                <td>ABC</td>
                <td><?= !$comment[$i] ? "" : "<div class=\"cell\">" . $comment[$i] . "</div>"; ?></td>
                <td><?= some_function( $company[$i] ) ?></td>
              </tr>
    
    <?php break; } ?>            /// New Line #2
    
    <?php } else { } ?>
    
    // Second Row
    
    <?php if ($person[2]["available"]=="yes") { ?> 
    
    <?php $i = 2; while ($i > 0) { ?> /// New Line #1
    
               <tr<?= !$hide[$i] ? "" : " class=\"hidden\""; ?>>
                <td>XYZ</td>
                <td><?= !$comment[$i] ? "" : "<div class=\"cell\">" . $comment[$i] . "</div>"; ?></td>
                <td><?= some_function( $company[$i] ) ?></td>
              </tr>
    
    <?php break; } ?>           /// New Line #2
    
    <?php } else { } ?> 
    
    // and so on
    

1 个答案:

答案 0 :(得分:1)

您可以使用实际数组 -

// Manually Set Values:

$hide[1]=false;
$comment[1]="hello";
$company[1]="apple";

$hide[2]=false;
$comment[2]="something";
$company[2]="microsoft";

并且它们不适用的地方,只是不设置它们,例如

$comment[3]="only a comment";

并在您的HTML中

<?php
     if ($person[1]["available"]=="yes") { ?> 
           <tr<?= !isset($hide[$i]) || !$hide[$i] ? "" : " class=\"hidden\""; ?>>
            <td>ABC</td>
            <td><?= !isset($comment[$i]) || !$comment[$i] ? "" : "<div class=\"cell\">" . $comment[$i] . "</div>"; ?></td>
            <td><?= if (isset($company[$i])) { some_function( $company[$i] ); } ?></td>
          </tr>
<?php } else { } ?>

我没有为你完成所有的工作,但是你可以看到我已经修改了它,所以你现在可以把这个东西放在一个循环索引$i上。

  • 如果您不知道会有多少$i,那么请使用while循环,然后发挥创意,例如 break何时isset($hide[$i]) && isset($comment[$i]) && isset($company[$i]) == false

  • 如果您想完全隐藏评论栏,请将isset拉出<td>,以便在这种情况下甚至不创建<td>。< / p>

  • 我在some_function...中包裹if( ... )的方式可能无效,因为您使用的是短回音形式<?= =>但我会告诉您想出来。

重新:新代码/评论

让我把你的新代码修改一下吧。我也要修改格式,以便缩进也匹配:

<?php
    $i = 1;
    while ($i > 0) {
        if ($person[$i]["available"]=="yes") {
            ?>
            <!--
            Only add the hidden class if $hide[$i] has been set by someone,
            i.e. you, manually, AND it has been set to true.
            -->
            <tr<?= isset($hide[$i]) && $hide[$i] ? " class=\"hidden\"" : ""; ?>>
                <td>ABC</td>
                <!--
                Same as above, only add the comment div if it has been set, but
                instead of what you're doing, I'm going to do what I think you
                meant to do: only if there's a non-empty string for a comment.
                -->
                <td><?= isset($comment[$i]) && $comment[$i] != "" ? "<div class=\"cell\">" . $comment[$i] . "</div>" : ""; ?></td>
                <!--
                Same thing yet again: only proceed if $company[i] is set and not
                an empty string.
                -->
                <td><?= isset($company[$i]) && $company[$i] != "" ? some_function( $company[$i] ) : "" ?></td>
            </tr>
            <?php
        } // The `else` case is not required.
        $i++;
        if (!isset($hide[$i]) &&
            !isset($comment[$i]) &&
            !isset($company[$i]))
        {
            // Now, this entire thing can be put in a form inside the
            // `while`'s condition so that there is no need for this `if`
            // statement nor the `break`, but I'm doing it this way to
            // make it clearer to you what's going on.
            break;
        }
    }
?>

就是这样。没有其他循环,没有其他块。现在让我在你的评论中回答你的问题:

  1. 当然,不,我不是故意每行使用while。那太傻了,不给你工作。我的意思是上面的,你只需要一行。

  2. 在我的“不设置”示例中,我打算表明$hide[3]$company[3]未设置;仅$comment[3]

  3. 我确信有些人试图告诉你,你的问题不是你只需要将数字转换为变量。你实际上选择了错误的方法,不幸的是,顽固地坚持这种方法。我知道感觉是什么感觉,“是的,我知道!我知道!但我有一个特例!在我的特殊情况下,我需要这样做!”但经验丰富的程序员知道什么样的特殊情况值得某些技术,很明显你的特殊情况与你试图使用的所谓“反射”或“变量变量”无关。你缺少的是一些简单的isset()(并且可能还有其他10种方法可以做到这一点,没有反思)。 isset()检查您是否设置变量,手动非手动等等。这意味着您现在可以为每个<tr>创建一个循环,并在每行的相应变量上使用isset(),以查看是否要对其执行任何操作。如果您复制粘贴此代码以创建新的自定义一次性页面,其中$hide$comment以及$company不再相关,那么请猜测:什么都没有,因为isset()只会忽略它们。我希望这会让事情变得更清楚,但如果没有,也不用担心,当你继续编程时,你会习惯于各种模式,知识会自动出现。

相关问题