简体   繁体   English

查询数据并显示在html表中

[英]query data and display in a html table

I like to put links into a html table, these links are query from the mysql table 我喜欢将链接放入html表中,这些链接是从mysql表中查询的

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

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

this is difficult to me. 这对我来说很难。 Could someone offer me an idea or a library for me to start with ? 有人可以为我提供一个想法或图书馆吗? Thank you so much 非常感谢

If that's too difficult for you, I'd recommend using a CMS until you get your feet wet. 如果那对您来说太困难了,我建议您使用CMS,直到您弄湿为止。 There are plenty out there. 那里有很多。 Wordpress , Drupal , Symphony to name a few. WordpressDrupalSymphony等

If you use a CMS like AlientWebguy is suggesting, you'll never learn... and I assume you're here to learn. 如果您使用AlientWebguy建议的CMS,您将永远不会学习...而我想您是来这里学习的。 You should start with a tutorial though, as this is pretty basic stuff. 不过,您应该从教程开始,因为这是非常基本的内容。

But to accomplish what you are trying to do - if I was a novice - this is what I would do. 但是要完成您想做的事情-如果我是新手-这就是我会做的。 I'm not going to give you your code, but it's is definitely close enough that you should be able to figure it out. 我不会给您您的代码,但是绝对足够接近,您应该能够弄清楚它。

I would make a file called functions.php that included the following: 我将创建一个名为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;
}

?>

Then on the file that I was going to output data, I would put the following: 然后在要输出数据的文件上,放置以下内容:

    <?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>

If anything doesn't make sense here, just leave a comment. 如果此处没有任何意义,请发表评论。 I'm happy to explain. 我很高兴解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM