简体   繁体   中英

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

------------------------------
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. There are plenty out there. Wordpress , Drupal , Symphony to name a few.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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