在PHP中将Array值赋予单个变量

时间:2013-06-15 07:38:40

标签: php

我有这个。

           <?php
                $SQLbrands="SELECT * FROM brands";
                $runBrands=mysqli_query($db, $SQLbrands) or die ("SQL Error");
                $noRow=mysqli_num_rows($runBrands);

                echo "<table border='0' cellspacing='0' cellpadding='1' id='brndTable1' class='brndTable1'>";
                echo "<thead><tr><th class='brT11'>Brand Name</th><th class='brT21'>Variant</th><th class='brT31'>SKU</th>
                      <th class='brT41'></th></tr></thead>";
                echo "<tbody>";
                        while ($reK = mysqli_fetch_array($runBrands))
                        {
                        $wec = $reK['id']; $wec2 = $reK['bvariant']; $wec3 = $reK['bsku'];
                        echo "<tbody class='colormine'><tr>";
                        echo "<td class='brT1'>".$reK["bname"]."</td>";
                        echo "<td class='brT2'>".$reK["bvariant"]."</td>";
                        echo "<td class='brT3'>".$reK["bsku"]."</td>";
                        echo "<td class='brT4'><input type='checkbox' name='delz[]' value='$wec' ></td>";
                        echo "</tr>";
                        }   
                echo "</tbody>";
                echo "</table>";
            ?>

我可以将结果输入单个变量吗?那么当我回显变量时,应该能够打印!

3 个答案:

答案 0 :(得分:2)

如果我正确理解,就像字符串连接一样:

<?php
$SQLbrands="SELECT * FROM brands";
$runBrands=mysqli_query($db, $SQLbrands) or die ("SQL Error");
$noRow=mysqli_num_rows($runBrands);


$brndTable = "<table border='0' cellspacing='0' cellpadding='1' id='brndTable1' class='brndTable1'>";
$brndTable .= "<thead><tr><th class='brT11'>Brand Name</th><th class='brT21'>Variant</th><th class='brT31'>SKU</th>
                      <th class='brT41'></th></tr></thead>";
$brndTable .= "<tbody>";
while ($reK = mysqli_fetch_array($runBrands))
{
    $wec = $reK['id']; $wec2 = $reK['bvariant']; $wec3 = $reK['bsku'];
    $brndTable .= "<tbody class='colormine'><tr>";
    $brndTable .= "<td class='brT1'>".$reK["bname"]."</td>";
    $brndTable .= "<td class='brT2'>".$reK["bvariant"]."</td>";
    $brndTable .= "<td class='brT3'>".$reK["bsku"]."</td>";
    $brndTable .= "<td class='brT4'><input type='checkbox' name='delz[]' value='$wec' ></td>";
    $brndTable .= "</tr>";
}
$brndTable .= "</tbody>";
$brndTable .= "</table>";


echo $brndTable;
?>

答案 1 :(得分:0)

然后将所有回声添加到变量http://php.net/manual/en/language.operators.string.php

喜欢:

$html = null;
$html .= "<table border='0' cellspacing='0' cellpadding='1' id='brndTable1' class='brndTable1'>";
$html .= "<thead><tr><th class='brT11'>Brand Name</th><th class='brT21'>Variant</th><th class='brT31'>SKU</th>
                  <th class='brT41'></th></tr></thead>";
$html .= "<tbody>";
.... etc...

echo $html;

答案 2 :(得分:-1)

如果要将查询中的所有结果都变为1变量,请使用此函数

$all = mysqli_fetch_all($runBrands);

http://pl1.php.net/manual/en/mysqli-result.fetch-all.php