php
  • html
  • button
  • html-table
  • 2013-04-29 51 views 0 likes 
    0

    我通過從一本書我有,但希望一些額外的列添加到表的教程以下。我已經添加了欄目,購買和出售,並在每一個我想顯示一個按鈕。我不確定如何做到這一點,這有可能嗎?使用的按鈕在表 - PHP

    下面是從表中的頁面我的代碼:

    <?php // Example 21-9: members.php 
    include_once 'header.php'; 
    
    if (!$loggedin) die(); 
    
    echo "<div class='main'>"; 
    
    $con=mysqli_connect("localhost","root","usbw","stocktrading"); 
    // Check connection 
    if (mysqli_connect_errno()) { 
        echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 
    
    $result = mysqli_query($con,"SELECT * FROM stocks"); 
    
    echo "<table border='1 '> 
    <tr> 
    <th>ID</th> 
    <th>Name</th> 
    <th>Price</th> 
    <th>Buy</th> 
    <th>Sell</th> 
    </tr>"; 
    
    while($row = mysqli_fetch_array($result)) { 
        echo "<tr>"; 
        echo "<td>" . $row['id'] . "</td>"; 
        echo "<td>" . $row['name'] . "</td>"; 
        echo "<td>" . $row['price'] . "</td>"; 
        echo "</tr>"; 
    } 
    echo "</table>"; 
    
    mysqli_close($con); 
    ?> 
    
    +0

    '​​'? – 2013-04-29 17:41:25

    回答

    0

    剛纔添加的TD內的按鈕

    echo "<tr>". 
        "<td>" . $row['id'] . "</td>" . 
        "<td>" . $row['name'] . "</td>" . 
        "<td>" . $row['price'] . "</td>" . 
        '<td><button>Sell</button><td>' . 
        '<td><button>Buy</button><td>' . 
        "</tr>";  
    
    +0

    這很好,謝謝! – MalvEarp 2013-04-29 17:47:32

    +0

    很高興幫助... – Memolition 2013-04-29 17:48:08

    2
    echo "<tr>"; 
        echo "<td>" . $row['id'] . "</td>"; 
        echo "<td>" . $row['name'] . "</td>"; 
        echo "<td>" . $row['price'] . "</td>"; 
        echo "<td><input type='radio' name='buysell' value='buy'></td>"; 
        echo "<td><input type='radio' name='buysell' value='sell'></td>"; 
        echo "</tr>"; 
    

    像這樣的東西會增加單選按鈕。如果您願意,可以使用複選框或其他類型的按鈕。

    +0

    這也有效,甚至更好,因爲我可以使用名稱,謝謝! – MalvEarp 2013-04-29 17:48:02

    1

    我知道,你必須是一個新的程序員,但有一些,你可以使用避免字符串連接很酷的事情。字符串連接有時會使您的代碼變得混亂且不可讀,並且它並不酷。

    可以使用HEREDOC爲避免級聯(請避免串聯)。另外,使用HEREDOC或雙引號"時,可以使用{}訪問數組鍵或對象屬性。

    即與HEREDOC:

    // Guys, look, it's a HEREDOC, it make the HTML more readable :) 
    $html = <<<EOF 
    <tr> 
        <td>{$row['id']}</td> 
        <td>{$row['name']}</td> 
        <td>{$row['price']}</td> 
        <td><button>Sell</button><td> 
        <td><button>Buy</button><td> 
    </tr> 
    EOF; 
    

    即用雙引號"

    $html = "<tr> 
        <td>{$row['id']}</td> 
        <td>{$row['name']}</td> 
        <td>{$row['price']}</td> 
        <td><button>Sell</button><td> 
        <td><button>Buy</button><td> 
    </tr>"; 
    

    但是,如果我需要調用一些功能呢?

    sprintfprintf可以是溶液

    spritnf:返回根據格式化字符串格式產生的字符串。

    printf:打印根據格式化字符串格式產生的字符串。

    即:

    $str = sprintf("My name is <b>%s</b>", ucfirst("i am not procrastinating")); 
    echo $str; 
    //OR 
    printf("My name is <b>%s</b>", ucfirst("i am not procrastinating")); 
    

    或使用模板的方法(可能是硬)使用str_replacearray_keysarray_values

    $template = "My name is <b>:name:</b>, i'm from :from:."; 
    $templateVars = array(
        ":name:" => "I am not procrastinating", 
        ":from:" => "Brazil" 
    ); 
    echo str_replace(array_keys($templateVars),array_values($templateVars),$template); 
    

    快樂編碼

    對不起英語,但我是巴西人,我們不會說英語,甚至不會說西班牙語哈哈。

    相關問題