在PHP代码中设置或分配ID或REL

时间:2011-12-14 05:00:15

标签: php html-table

我想在我的表代码中设置或分配id / rel,看看下面我的代码,这样每个表td都有id和rel所以我可以在jQuery中给出语句和操作日期,任何帮助都将是非常感谢:

function printdata()
{
    if(is_array($_SESSION['chart']))
    {
    echo "Here's your chart" . "<br>";
    $max = count($_SESSION['chart']);  
    $th1 = "<th>" . "No" . "</th>";
    $th2 = "<th>" . "Nama Barang" . "</th>";
    $th3 = "<th>" . "Harga Barang" . "</th>";
    $th4 = "<th>" . "Stok Barang" . "</th>";
    echo "<table border=1>";
    echo "<tr>";
    echo $th1 ;
    echo $th2;
    echo $th3;
    echo $th4;
    echo "</tr>";
    for ($indexo = 0; $indexo < $max; $indexo++) {
       echo "<tr>";
       echo "<td>" . $indexo."</td>";
       echo "<td >" . $_SESSION['chart'][$indexo]['namabarang']."</td>";
       //i want to assign td rel inside of this echo 
       echo "<td>" . $_SESSION['chart'][$indexo]['hargabarang']."</td>";
       echo "<td>" . $_SESSION['chart'][$indexo]['stokbarang']."</td>";
       echo " <td><button id=".$indexo." name=\"button\">Edit</button></td>";
       echo "</tr>";
    }
    echo "</table>";
    }else
    {
        echo "Chart is still Empty";
    }
}

2 个答案:

答案 0 :(得分:0)

echo "<table border=1 id=\"{$some_php_variable_with_an_id_value_in_it}\">"

答案 1 :(得分:0)

首先你的代码太多了,没有必要这么做,构建一些具有相同结果的输出可能更简单,这里可能可以帮助你了

function printdata(){
    if(is_array($_SESSION['cart'])){
        echo "Here's Your Chart ".count($_SESSION['cart'])."<br />";
        echo '<table><tr>th>No</th><th>Nama Barang</th><th>Harga Barang</th><th>Stok Barang</th><th>Action</th></tr>';
        for($indexo =0; $indexp < count($_SESSION['cart']); $indexo++){
            echo "<tr>
                <td id=".$indexo.">".$indexo."</td>
                <td id=".$_SESSION['chart'][$indexo]['namabarang'].">".$_SESSION['chart'][$indexo]['namabarang']."<td>
                <td id=".$_SESSION['chart'][$indexo]['hargabarang'].">".$_SESSION['chart'][$indexo]['hargabarang']."<td>
                <td id="$_SESSION['chart'][$indexo]['stokbarang']">".$_SESSION['chart'][$indexo]['stokbarang']."<td>
                <td><button id=".$indexo." name=\"button\">Edit</button></td>
            </tr>";
        }
    }else{
         echo "Chart is still Empty";
    }
}

您可以将<td id=".$indexo.">更改为您想要的内容,就像其他内容一样,idrel将与客人相同