donglun7151 2012-02-06 02:56
浏览 44
已采纳

查询数据并在html表中显示

I like to put links into a html table, these links are query from the mysql table

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

  • 写回答

2条回答 默认 最新

  • doulu1325 2012-02-06 03:23
    关注

    If you use a CMS like AlientWebguy is suggesting, you'll never learn... and I assume you're here to learn. 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:

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测