2012-02-06 4 views
1

は、これらのリンクはMySQLのテーブルからクエリクエリデータと表示

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

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

あり、これは私には難しいです。誰か私にアイデアやライブラリを提供してもらえますか? ありがとうございました

+0

+1、今は8名です。 – AlienWebguy

答えて

0

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> 

ここで何かが意味をなさない場合は、コメントを残してください。私は説明してうれしいです。

+1

スタックオーバーフローへようこそ。答えがあなたに役立つなら、投票してください。特定の回答があなたの問題を解決した場合は、横の小さなチェックマークをクリックして問題を解決します。それは私たちに評判を与えます。 – phpmeh

+1

upvotesのための釣りはあなたが愚かに見える。そして、オッズは、OPはあなたのスパゲッティコードで何をすべきか分かりません。 – AlienWebguy

+0

この回答は、そのまま使用した場合、大きな太ったエラーが発生します。 – Maverick

0

あなたにとってはそれほど難しい場合は、足を濡らすまでCMSを使用することをお勧めします。そこにはたくさんのものがあります。 Wordpress,Drupal,Symphonyなどが挙げられる。

関連する問題