簡體   English   中英

查詢數據並顯示在html表中

[英]query data and display in a html table

我喜歡將鏈接放入html表中,這些鏈接是從mysql表中查詢的

------------------------------
item Number |  Item purchased
------------------------------
1           |  a vase
2           |  a candle

etc.....
10
------------------------------
           <<1,2,...100|next>>

這對我來說很難。 有人可以為我提供一個想法或圖書館嗎? 非常感謝

如果那對您來說太困難了,我建議您使用CMS,直到您弄濕為止。 那里有很多。 WordpressDrupalSymphony等

如果您使用AlientWebguy建議的CMS,您將永遠不會學習...而我想您是來這里學習的。 不過,您應該從教程開始,因為這是非常基本的內容。

但是要完成您想做的事情-如果我是新手-這就是我會做的。 我不會給您您的代碼,但是絕對足夠接近,您應該能夠弄清楚它。

我將創建一個名為functions.php的文件,其中包括以下內容:

<?PHP
// these are freebies.  You don't need to understand them yet.
function sqlarr($sql, $numass=MYSQL_BOTH) {
    // MYSQL_NUM  MYSQL_ASSOC  MYSQL_BOTH
    $got = array();
    $result=mysql_query($sql) or die("$sql: " . mysql_error());                             

    if(mysql_num_rows($result) == 0)
        return $got;
    mysql_data_seek($result, 0);
    while ($row = mysql_fetch_array($result, $numass)) {
        array_push($got, $row);
    }
    return $got;
} 

// Sql fetch assoc
function sqlassoc($sql){
    $query = mysql_query($sql) or die("$sql:". mysql_error());
    $row = mysql_fetch_assoc($query);
    return $row;
}

function sqlrow($sql){
    $query = mysql_query($sql) or die("$sql:". mysql_error());
    $row = mysql_fetch_row($query);
    return $row;
}

function sqlquery($sql){
    $query = mysql_query($sql) or die("$sql:". mysql_error());
    return $row;
}

?>

然后在要輸出數據的文件上,放置以下內容:

    <?PHP  include('./functions.php'); 
    // It sounds like you are already connected to your database, so I'm going to skip that.  If you need it, add a comment.
    $sql = "SELECT `colNames`, `colName2` FROm `tableName` WHERE `col` = 'condition' ";
    // obviously change this to your names, such as `itemNumber`
    $results = sqlarr( $sql ); // Now results is going to automatically contain a 2D array.  
    echo '<pre>'; print_r( $results ); echo '</pre>';
    /* this is just to show you what is happening so far.  You should get in the habit of using things like this to debug.  A lot of people prefer var_dump instead of print_r.  I use both because var_dump is harder to read.
    // Result should be returning something like this:
    // array(
            [0] => array(
                    [0] => 'ABC123',
                    ["itemNumber"] => 'ABC123',
                    [1] => 'http://www.abc.com',
                    ["link"] => 'http://www.abc.com' ),
            [1] => array( ... )
            )
    // the first level - the [0] => array(  or the [1] => array( part - corresponds to a row in your database
    // so now we need a way to filter through those rows.  Look up php.net/for or php.net/foreach to see how to accomplish that.  A lot of people use php.net/while too, but I don't prefer that personally. */
    ?>
    <html>
    <body>
    <table>
    <?PHP
    foreach( $results as $row ){  // this is turning $result[0] => array(  into $row.  So now we can access $result[0]['linkName'] as $row['linkName']
       echo '<tr><td>'.$row['linkName'].'</td></tr>';
    }  // foreach $row - dont forget to close your curly bracket.  Good practice is to always close it as soon as you open it, and to put a comment after it like I just did letting you know what it goes to
    ?>
    </table>
    </body>
    </html>

如果此處沒有任何意義,請發表評論。 我很高興解釋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM