2017-06-15 111 views
0

我試圖從我的數據庫中的列回合中獲得「回合」的值。所以,我想要獲得每個辯論/帖子的一個字段值。基於價值/數量,它應該顯示不同的東西。當我認爲它的值爲NULL時,即使該辯論/帖子的db字段中的值是4.這不僅僅是辯論,而是所有這些都會發生。如何獲得列中字段的實際值並將其分配給名爲$ 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

不確定您爲什麼使用所有'if'。你最好使用這樣簡單的代碼:$ result = mysql_query(「SELECT rounds FROM vf_Discussion」); ($ row = mysql_fetch_array($ result)) echo $ row ['column']; .... } – mele

+0

@mele,仍然不起作用。 – julereca

回答

0

我用PDO s。您的錯誤是在$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