2017-06-15 7 views
0

私は自分のデータベースのカラムラウンドから「ラウンド」の価値を得ようとしています。だから、私は各議論/投稿のフィールドの数である値を得たいと思う。値/番号に基づいて、別のものを表示することになっています。私はおそらく、その議論/投稿のdbフィールドの値が4であっても、それはNULLと言う価値を得るでしょう。これは議論だけではありませんが、これはすべてに起こります。列内のフィールドの実際の値を取得し、$ roundsという変数に代入するにはどうすればよいですか。この変数は、その議論だけでなく、各議論の価値を持つ必要があります。カラムから値を取得してもnullを返しますか?

<?php 
$servername = ""; 
$username = ""; 
$password = ""; 
$dbname = ""; 

$con = new mysqli($servername, $username, $password, $dbname); 

if($con->connect_error) 
{ 
    die("Connection failed: " . $con->connect_error); 
} 
else 
{ 
    $sql = "SELECT rounds FROM vf_Discussion"; 
    $result = $con->query($sql); 

    $allRounds = $result->fetch_row(); 
    $rounds = $allRounds[0]; 

    var_dump($rounds); 
} 

mysqli_close($con); 

$rounds1 = '<h2 class="CommentHeading">Round 1 (Pro)</h2> <br> <h2 class="CommentHeading">Round 1 (Con) </h2>'; 
$rounds2 = '<h2 class="CommentHeading">Round 2 (Pro)</h2> <br> <h2 class="CommentHeading">Round 2 (Con)</h2>'; 
$rounds3 = '<h2 class="CommentHeading">Round 3 (Pro)</h2> <br> <h2 class="CommentHeading">Round 3 (Con)</h2>'; 
$rounds4 = '<h2 class="CommentHeading">Round 4 (Pro)</h2> <br> <h2 class="CommentHeading">Round 4 (Con)</h2>'; 
$rounds5 = '<h2 class="CommentHeading">Round 5 (Pro)</h2> <br> <h2 class="CommentHeading">Round 5 (Con)</h2>'; 

foreach($allRounds as $rounds) 
{ 
    if($rounds == 1) 
    { 
    echo $rounds1; 
    foreach($Sender->Data('Answers') as $Row) 
    { 
     $Sender->EventArguments['Comment'] = $Row; 
     WriteComment($Row, $Sender, Gdn::Session(), 0); 
    } 
    } 
    if($rounds == 2) 
    { 
    echo $rounds1; 
    echo $rounds2; 
    foreach($Sender->Data('Answers') as $Row) 
    { 
     $Sender->EventArguments['Comment'] = $Row; 
     WriteComment($Row, $Sender, Gdn::Session(), 0); 
    } 
    } 
    if($rounds == 3) 
    { 
    echo $rounds1; 
    echo $rounds2; 
    echo $rounds3; 
    foreach($Sender->Data('Answers') as $Row) 
    { 
     $Sender->EventArguments['Comment'] = $Row; 
     WriteComment($Row, $Sender, Gdn::Session(), 0); 
    } 
    } 
    if($rounds == 4) 
    { 
    echo $rounds1; 
    echo $rounds2; 
    echo $rounds3; 
    echo $rounds4; 
    foreach($Sender->Data('Answers') as $Row) 
    { 
     $Sender->EventArguments['Comment'] = $Row; 
     WriteComment($Row, $Sender, Gdn::Session(), 0); 
    } 
    } 
    if($rounds == 5) 
    { 
    echo $rounds1; 
    echo $rounds2; 
    echo $rounds3; 
    echo $rounds4; 
    echo $rounds5; 
    foreach($Sender->Data('Answers') as $Row) 
    { 
     $Sender->EventArguments['Comment'] = $Row; 
     WriteComment($Row, $Sender, Gdn::Session(), 0); 
    } 
    } 
} 
?> 
+0

わからないあなたは」それらのすべてを使用している理由。次のようなよりシンプルなコードを使用することをお勧めします:$ result = mysql_query( "SELECT rounds from vf_Discussion"); while($ row = mysql_fetch_array($ result)) { echo $ row ['column']; .... } – mele

+0

@mele、まだ動作しません。 – julereca

答えて

0

私はPDOを使用しました。あなたの間違いは$roundsの割り当てにありました。 は、と私は読みやすくするためにコードをクリーンアップ:の場合

<?php 
$servername = ""; 
$dbname = ""; 
$username = ""; 
$password = ""; 

$pdo = NULL; 

try 
{ 
    $pdo = new PDO('mysql:host=' . $servername . ';dbname=' . $dbname, $username, $password); 
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
} 
catch(PDOException $exception) 
{ 
    die("Connection failed: " . $exception->getMessage()); 
} 

$rounds = []; // array we want to save all the rounds to 

// the database 
$query = "SELECT rounds, COUNT(*) AS cnt FROM vf_Discussion GROUP BY rounds ORDER BY rounds"; 
$statement = $pdo->prepare($query); 
$statement->execute(); 

$rows = $statement->fetchAll(\PDO::FETCH_ASSOC); 

foreach($rows as $row) 
{ 
    $rounds[] = ['name' => $row['rounds'], 'count' => $row['cnt']]; 
} 

foreach($rounds as $round) 
{ 
    $name = $round['name']; 
    $cnt = $round['cnt']; 

    echo '<h2 class="CommentHeading">Round ' . $round . ' (Pro)</h2> <br> <h2 class="CommentHeading">Round ' . $round . ' (Con)</h2> <br> <h2 class="CommentHeading">Number of Rounds ' . $cnt . '</h2>'; 

    foreach($Sender->Data('Answers') as $Row) 
    { 
    $Sender->EventArguments['Comment'] = $Row; 
    WriteComment($Row, $Sender, Gdn::Session(), 0); 
    } 
} 
?> 
+1

実装後、すべてのifsを返します。したがって、1回目、1回目のラウンド2、1回目のラウンド2回目のラウンド3、1回目のラウンド2回のラウンド3回目のラウンド4、今すぐ取得します。これをどうすれば解決できますか? – julereca

+0

投稿を編集しました。それを試してください@julereca – Phil

+0

これで、決して終わらないか、ほとんど決して終わらない「ラウンド(PRO)」を繰り返すだけです。あなたが以前に与えたすべてのコードを、編集済みのものか単なるスニペットに置き換える必要がありますか?スニペットなら、どの部分? – julereca

関連する問題