2016-05-06 5 views
0

私はダンスコンテストのサイトを持っており、各ユーザーはログインしてダンスの瞬間を追加できます。 すべてのユーザーのすべての瞬間を私のhtmlテーブルに入れます。 html列に「ログされたユーザーIDで追加された各瞬間のダンサーの数」を追加します。各瞬間のPHP/Mysqlカウントダンサー

私はこれを持っている:

$c = mysql_query("SELECT * FROM moments"); 
$dancers = 0; 
while($rows = mysql_fetch_array($c)){ 
    for($i = 1; $i <= 24; $i++){ 
     $dan_id = 'dancer'.$i; 
     if($rows[$dan_id] != "" || $rows[$dan_id] != null) 
      $dancers++; 
    } 
} 
echo "<th class="tg-amwm">NR of dancers</th>"; 
echo "<td class='tg-yw4l'>$dancers</td>"; 

phpMyAdminのモーメントテーブル:ID、clubname、カテゴリ、規律、セクション、およびこれがあります。 enter image description here

をしかし、このプロセスは、すべてのすべてのダンサー名を数えていますユーザーの瞬間。 このプロセスの例:合計200人のダンサーがいる!

私はすべての全員の瞬間、このような形で追加された各瞬間のすべてのダンサーの名前をカウントするプロセスが欲しい:ユーザージョンが2つの瞬間が追加されている場合:モーメント1:5ダンサー - 瞬間2:10人のダンサーなどが各ユーザーに割り当てられます。

+0

[mysql_ *関数を使用しない](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)ください。 [これらの拡張機能](http://php.net/manual/en/migration70.removed-exts-sapis.php)はPHP 7で削除されました。[prepared](http://en.wikipedia.org/ [PDO](http://php.net/manual/en/pdo.prepared-statements.php)および[MySQLi](http://php.net/manual/en/mysqli.quickstart)のwiki/Prepared_statement)ステートメント.prepared-statements.php)、PDOの使用を検討してください。[これは本当に簡単です](http://jayblanchard.net/demystifying_php_pdo.html)。 –

+0

私は新しい学習者です。アドバイスをいただきありがとうございます。Jay – Fido

+1

データベースを正規化することを検討してください。列の値のカンマ区切りリストはデザインが悪いので、なぜ名前とリストを保存する必要がありますか?リスト内の名前を数えることはできませんか? –

答えて

2

私は正しい方法であなたを入れようとします(それは長いポストのようですが、初心者に読ませる価値があると思います)。

コメントあなたのデータベースを正規化し、私があなたであり、あなたのプロジェクトが長い間うまくいくようにしたいのであれば...私はそれをやりたいと言われました。

多くのMySQL正規化チュートリアルがあります。あなたが興味を持っている場合は、あなた自身をグーグルにすることができます...私はちょうどあなたの特定の例についてお手伝いするつもりです。

基本的に、「異なる概念」を格納するために異なるテーブルを作成し、データベースをクエリするときにそれを結合する必要があります。この場合

、私はこれらのテーブルを作成します。

database diagram

categoriesdance_clubsusersdancers店 "基本的な" データを。

momentsおよびmoment_dancersは、データ間の関係を作成するための外部キーを格納します。

内容をよく理解してみましょう。

mysql> select * from categories; 
+----+---------------+ 
| id | name   | 
+----+---------------+ 
| 1 | Hip-hop/dance | 
+----+---------------+ 

mysql> select * from dance_clubs; 
+----+---------------+ 
| id | name   | 
+----+---------------+ 
| 1 | dance academy | 
+----+---------------+ 

mysql> select * from users; 
+----+-------+ 
| id | name | 
+----+-------+ 
| 1 | alex | 
+----+-------+ 

mysql> select * from dancers; 
+----+-------+ 
| id | name | 
+----+-------+ 
| 1 | alex | 
| 2 | dan | 
| 3 | mihai | 
+----+-------+ 

mysql> select * from moments; 
+----+--------------+---------------+-------------------+ 
| id | main_user_id | dance_club_id | dance_category_id | 
+----+--------------+---------------+-------------------+ 
| 1 |   1 |    1 |     1 | 
+----+--------------+---------------+-------------------+ 
      (user alex) (dance acad..)  (Hip-hop/dance) 

mysql> select * from moment_dancers; 
+----+-----------+-----------+ 
| id | moment_id | dancer_id | 
+----+-----------+-----------+ 
| 1 |   1 |   1 | (moment 1, dancer alex) 
| 2 |   1 |   2 | (moment 1, dancer dan) 
| 3 |   1 |   3 | (moment 1, dancer mihai) 
+----+-----------+-----------+ 

今度はPHPからいくつかの問い合わせをしたいと思います。

コメントでも述べたとおり、mysql_ *クエリの代わりに準備文を使用します。

準備されたステートメントの概念は、最初は分かりにくいものです。ブラウザで

// Your connection settings 
$connData = ["localhost", "user", "pass", "dancers"]; 

$conn = new mysqli($connData[0], $connData[1], $connData[2], $connData[3]); 
$conn->set_charset("utf8"); 

if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 

// Here we explain MySQL which will be the query 
$stmt = $conn->prepare("select * from dancers"); 
// Here we explain PHP which variables will store the values of the two columns (row by row) 
$stmt->bind_result($dancerId, $dancerName); 

// Here we execute the query and store the result 
$stmt->execute(); 
$stmt->store_result(); 

// Here we store the results of each row in our two PHP variables 
while($stmt->fetch()){ 
    // Now we can do whatever we want (store in array, echo, etc) 
    echo "<p>$dancerId - $dancerName</p>"; 
} 

$stmt->close(); 
$conn->close(); 

結果:

dancers list

ダンサーを(ちょうどそれを理解するために) リストへ)

簡単たとえば、ちょうど密接にコードを読んで、再びいくつかのチュートリアルを探して

いいえ!今何か少し難しい! リスト瞬間:ブラウザで

// Your connection settings 
$connData = ["localhost", "user", "pass", "dancers"]; 
$conn = new mysqli($connData[0], $connData[1], $connData[2], $connData[3]); 
$conn->set_charset("utf8"); 

if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 

// Query to read the "moments", but we have their main user and dancers in other tables 
$stmtMoments = $conn->prepare(" 
    select 
     moments.id, 
     (select name from users where users.id = moments.main_user_id) as main_user, 
     (select name from dance_clubs where dance_clubs.id = moments.dance_club_id) as dance_club, 
     (select name from categories where categories.id = moments.dance_category_id) as dance_category, 
     (select count(*) from moment_dancers where moment_dancers.moment_id = moments.id) as number_of_dancers 
    from moments 
    "); 
// Five columns, five variables... you know ;) 
$stmtMoments->bind_result($momentId, $momentMainUser, $momentDanceClub, $momentDanceCategory, $momentNumberOfDancers); 

// Query to read the dancers of the "moment" with id $momentId 
$stmtDancers = $conn->prepare(" 
    select 
     dancers.name as dancer_name 
    from 
     dancers join moment_dancers on dancers.id = moment_dancers.dancer_id 
    where 
     moment_dancers.moment_id = ? 
    "); 

$stmtDancers->bind_param("i", $momentId); 
$stmtDancers->bind_result($momentDancerName); 

// Executing the "moments" query 
$stmtMoments->execute(); 
$stmtMoments->store_result(); 

// We will enter once to the while because we have only one "moment" right now 
while($stmtMoments->fetch()){ 

    // Do whatever you want with $momentId, $momentMainUser, $momentDanceClub, $momentDanceCategory, $momentNumberOfDancers 
    // For example: 

    echo "<h3>Moment $momentId</h3>"; 
    echo "<p>Main user: $momentMainUser</p>"; 
    echo "<p>Dance club: $momentDanceClub</p>"; 
    echo "<p>Category: $momentDanceCategory</p>"; 
    echo "<p>Number of dancers: $momentNumberOfDancers</p>"; 
    echo "<p><strong>Dancers</strong>: "; 

    // Now, for this moment, we look for its dancers 
    $stmtDancers->execute(); 
    $stmtDancers->store_result(); 
    while($stmtDancers->fetch()){ 

     // Do whatever you want with each $momentDancerName 
     // For example, echo it: 

     echo $momentDancerName . " "; 
    } 

    echo "</p>"; 
    echo "<hr>"; 
} 

$stmtUsers->close(); 
$stmtMoments->close(); 

$conn->close(); 

結果:

moments list

そして、それがすべてだと!質問がある場合は私に尋ねてください!

(あなたがしたい場合、私は、コンテンツデータと例のデータベースを作成するDDLコードを投稿できる)

編集は:dancersテーブルを追加しました。名前がmoment_usersからmoment_dancersに変更されました。新しいテーブルと名前にスクリプトを適合させるように変更された機能。

+0

あなたの努力のためにnanocvありがとう、あなたの返事をお寄せください! 1つの質問は、私のテーブルではアレックスダン&ミハイは彼らはユーザーではない、ダンサーは、ユーザーがサインアップしたときに、1から24人のダンサーの形でいくつかのダンサーの名前を書くと、それらのダンサーの名前を数える方法、各ダンサーには1つの列があります:dancer1 |ダンサー2 |ダンサー3など.. – Fido

+0

@Fidoそれは私にとっては大変な仕事ではありません、私はこれをするのが好きです!そして、昨日私はあなたが見ることができるようにいくつかの自由時間を持っていた;)申し訳ありません私はこのアプリケーションを作るためにこの方法を主張するが、これは良い方法ですお約束!これは、各瞬間に可変数のダンサーを格納するための固定数の列を持つこと(データベースにヌルセルがたくさんあります)を行うのは良い習慣ではありません。私は自分の答えを編集し、適切な方法でそれらを保存するための「ダンサー」テーブルを作成しました(あなたは簡単にダイアグラムでそれを見ることができます)。 – nanocv

+1

@nanocv - 正規化の原則と利点を説明するために本当に努力してくださったことに感謝します。 –

関連する問題