Php 在一个网站的两个不同列(div)中从mysql获取数据

Php 在一个网站的两个不同列(div)中从mysql获取数据,php,mysql,html,Php,Mysql,Html,我有一个关于从MySQL数据库获取数据到网站的问题。在这个网站上,我有两列(或div)需要放置数据 在数据库中,我有一行(列)名为“选项”。用户填写表格,对于“选项”(单选按钮),他们必须在“正确”和“错误”之间进行选择 现在我想获取数据,这样所有选择“右”的人都会进入左侧的第一列。所有选择“错误”的人都在右边的第二栏 我不知道如何将网站上的数据分成两列(div) 我有以下脚本: <? $con = mysql_connect("localhost","name_comment","Pas

我有一个关于从MySQL数据库获取数据到网站的问题。在这个网站上,我有两列(或div)需要放置数据

在数据库中,我有一行(列)名为“选项”。用户填写表格,对于“选项”(单选按钮),他们必须在“正确”和“错误”之间进行选择

现在我想获取数据,这样所有选择“右”的人都会进入左侧的第一列。所有选择“错误”的人都在右边的第二栏

我不知道如何将网站上的数据分成两列(div)

我有以下脚本:

<?
$con = mysql_connect("localhost","name_comment","Pass");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("name_comment", $con);
$article_id = $_GET['id'];
if( ! is_numeric($article_id) )
die('invalid article id');
$query = "SELECT * FROM `comments` WHERE `articleid` =$article_id LIMIT 0 , 30";
$comments = mysql_query($query);
echo "<h1>User Comments</h1>"; 
while($row = mysql_fetch_array($comments, MYSQL_ASSOC))
{
    $name = $row['name'];
    $options = $row['options'];
    $email = $row['email'];
    $website = $row['website'];
    $comment = $row['comment'];
    $timestamp = $row['timestamp'];
    $name = htmlspecialchars($row['name'],ENT_QUOTES);
    $options = htmlspecialchars($row['options'],ENT_QUOTES);
    $email = htmlspecialchars($row['email'],ENT_QUOTES);
    $website = htmlspecialchars($row['website'],ENT_QUOTES);
    $comment = htmlspecialchars($row['comment'],ENT_QUOTES);
    echo "  <div style='margin:30px 0px;'>
    Name: $name<br />
    Options: $options<br />
    Email: $email<br />
    Website: $website<br />
    Comment: $comment<br />
    Timestamp: $timestamp
    </div>
    ";
}
mysql_close($con);
?>

您应该将DB数据保存在两个数组中,一个包含具有“right”选项的记录,另一个包含错误的记录,然后分别对这两个数组迭代循环以创建左和右div。并给出相应的css。请参考代码片段以了解更多信息

<?
$con = mysql_connect("localhost","name_comment","Pass");
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("name_comment", $con);
$article_id = $_GET['id'];
if( ! is_numeric($article_id) )
die('invalid article id');

$query = "SELECT * FROM `comments` WHERE `articleid` =$article_id LIMIT 0 , 30";
$comments = mysql_query($query);

echo "<h1>User Comments</h1>"; 
$rightValues = [];
$wrongValues = [];

while($row = mysql_fetch_array($comments, MYSQL_ASSOC))
{
  if($row['options']=='right'){
    $rightValues[] = $row;
  }else if($row['options']=='wrong'){
    $wrongValues[] = $row;
  } 
}

//Div with right values
//Left side Div
foreach($rightValues as $row){
  $name = $row['name'];
  $options = $row['options'];
  $email = $row['email'];
  $website = $row['website'];
  $comment = $row['comment'];
  $timestamp = $row['timestamp'];
  $name = htmlspecialchars($row['name'],ENT_QUOTES);
  $options = htmlspecialchars($row['options'],ENT_QUOTES);
  $email = htmlspecialchars($row['email'],ENT_QUOTES);
  $website = htmlspecialchars($row['website'],ENT_QUOTES);
  $comment = htmlspecialchars($row['comment'],ENT_QUOTES);
  //Give Css such a way that this div will display on the left side
  echo "  <div style='margin:30px 0px;'>
  Name: $name<br />
  Options: $options<br />
  Email: $email<br />
  Website: $website<br />
  Comment: $comment<br />
  Timestamp: $timestamp
  </div>
  ";
}

//Div with wrong values
//Right side Div
foreach($wrongValues as $row){
  $name = $row['name'];
  $options = $row['options'];
  $email = $row['email'];
  $website = $row['website'];
  $comment = $row['comment'];
  $timestamp = $row['timestamp'];
  $name = htmlspecialchars($row['name'],ENT_QUOTES);
  $options = htmlspecialchars($row['options'],ENT_QUOTES);
  $email = htmlspecialchars($row['email'],ENT_QUOTES);
  $website = htmlspecialchars($row['website'],ENT_QUOTES);
  $comment = htmlspecialchars($row['comment'],ENT_QUOTES);
  //Give Css such a way that this div will display on the right side
  echo "  <div style='margin:30px 0px;'>
  Name: $name<br />
  Options: $options<br />
  Email: $email<br />
  Website: $website<br />
  Comment: $comment<br />
  Timestamp: $timestamp
  </div>
  ";
}
mysql_close($con);
?>

我可以想出两种方法:

第一个:将用户分为两个类别,然后在相应的div中显示每个类别:

// sorting users in two categories
$users = array(
    'right' => array(),
    'wrong' => array();
);

while($row = mysql_fetch_array($comments, MYSQL_ASSOC)){
    $users[$row['comment']][] = $row;
}

//displaying them 
foreach($users['right']) as $row){
    $name = $row['name'];
    $options = $row['options'];
    $email = $row['email'];
    $website = $row['website'];
    $comment = $row['comment'];
    $timestamp = $row['timestamp'];
    $name = htmlspecialchars($row['name'],ENT_QUOTES);
    $options = htmlspecialchars($row['options'],ENT_QUOTES);
    $email = htmlspecialchars($row['email'],ENT_QUOTES);
    $website = htmlspecialchars($row['website'],ENT_QUOTES);
    $comment = htmlspecialchars($row['comment'],ENT_QUOTES);
    echo "<h1>Right</h1>";
    echo "  <div style='margin:30px 0px;'>
    Name: $name<br />
    Options: $options<br />
    Email: $email<br />
    Website: $website<br />
    Comment: $comment<br />
    Timestamp: $timestamp
    </div>
    ";
}

foreach($users['wrong']) as $row){
    $name = $row['name'];
    $options = $row['options'];
    $email = $row['email'];
    $website = $row['website'];
    $comment = $row['comment'];
    $timestamp = $row['timestamp'];
    $name = htmlspecialchars($row['name'],ENT_QUOTES);
    $options = htmlspecialchars($row['options'],ENT_QUOTES);
    $email = htmlspecialchars($row['email'],ENT_QUOTES);
    $website = htmlspecialchars($row['website'],ENT_QUOTES);
    $comment = htmlspecialchars($row['comment'],ENT_QUOTES);
    echo "<h1>Wrong</h1>";
    echo "  <div style='margin:30px 0px;'>
    Name: $name<br />
    Options: $options<br />
    Email: $email<br />
    Website: $website<br />
    Comment: $comment<br />
    Timestamp: $timestamp
    </div>
    ";
}

首先创建两个新数组,分别是。将
$row
数组推送到相应的数组中,并回显相应div中的数据

$left=[];
$right=[];
while($row = mysql_fetch_array($comments, MYSQL_ASSOC)) {
 if($row["option"]=="right"){ // right is just an assumption. Replace with the real data
        $left[]=$row;
    }else{
        $right[]=$row;
    }
}

echo '<div id="left">';
foreach($left as $data){
    echo 'Name: '.$data["name"].'<br />
          Options: '.$data["options"]'.<br />'; 
// continue
}
echo '</div>';
//similarly for right div using $right array
$left=[];
$right=[];
while($row=mysql\u fetch\u数组($comments,mysql\u ASSOC)){
if($row[“option”]=“right”){//right只是一个假设。替换为实际数据
$left[]=$row;
}否则{
$right[]=$row;
}
}
回声';
foreach($保留为$数据){
回显“名称:”.$data[“名称”]。
选项:'.$data[“选项”].
; //继续 } 回声'; //类似地,对于使用$right数组的right div
回答时间太长,但另一种方法。我想说的一件事是,如果提供的
id
不是数字,那么创建db连接就没有意义了,所以请将该代码放在相关检查之后

<?

    $right = $wrong = array();
    $article_id = $_GET['id'];
    if( !is_numeric( $article_id ) ) die('invalid article id');

    $con = mysql_connect("localhost","name_comment","Pass");
    if (!$con) die('Could not connect: ' . mysql_error());
    mysql_select_db("name_comment", $con);



    $query = "SELECT * FROM `comments` WHERE `articleid` =$article_id LIMIT 0 , 30";
    $comments = mysql_query( $query );
    while( $row = mysql_fetch_array( $comments, MYSQL_ASSOC ) ){

        $data=(object)array(
            'name'      =>  htmlspecialchars($row['name'],ENT_QUOTES),
            'email'     =>  htmlspecialchars($row['email'],ENT_QUOTES),
            'website'   =>  htmlspecialchars($row['website'],ENT_QUOTES),
            'comment'   =>  htmlspecialchars($row['comment'],ENT_QUOTES),
            'timestamp' =>  htmlspecialchars($row['timestamp'],ENT_QUOTES)
        );

        /* 
            Assumed that the column holds boolean 1/0, 
            otherwise change logic here according to 
            data in the column.
        */
        if( intval( $row['options'] ) )==1 ) $right[]=$data;
        else $wrong[]=$data;
    }
    mysql_close($con);



    echo "<h1>User Comments</h1>"; 

    /* process RIGHT answers */
    echo "<div id='right'>";
    foreach( $right as $row ){
        echo "
        <div style='margin:30px 0px;'>
            Name: {$row->name}<br />
            Options: {$row->options}<br />
            Email: {$row->email}<br />
            Website: {$row->website}<br />
            Comment: {$row->comment}<br />
            Timestamp: {$row->timestamp}
        </div>";
    }
    echo "</div>";




    /* Process WRONG answers */
    echo "<div id='wrong'>";
    foreach( $wrong as $row ){

        echo "
        <div style='margin:30px 0px;'>
            Name: {$row->name}<br />
            Options: {$row->options}<br />
            Email: {$row->email}<br />
            Website: {$row->website}<br />
            Comment: {$row->comment}<br />
            Timestamp: {$row->timestamp}
        </div>";
    }
    echo "</div>";

?>

您有两种方法,首先您可以创建两个div并使用它们的id进行调用。在第一个div中,您可以为正确的选项编写php脚本,在第二个div中,您可以为错误的选项编写php脚本。第二种是使用表格。
<?

    $right = $wrong = array();
    $article_id = $_GET['id'];
    if( !is_numeric( $article_id ) ) die('invalid article id');

    $con = mysql_connect("localhost","name_comment","Pass");
    if (!$con) die('Could not connect: ' . mysql_error());
    mysql_select_db("name_comment", $con);



    $query = "SELECT * FROM `comments` WHERE `articleid` =$article_id LIMIT 0 , 30";
    $comments = mysql_query( $query );
    while( $row = mysql_fetch_array( $comments, MYSQL_ASSOC ) ){

        $data=(object)array(
            'name'      =>  htmlspecialchars($row['name'],ENT_QUOTES),
            'email'     =>  htmlspecialchars($row['email'],ENT_QUOTES),
            'website'   =>  htmlspecialchars($row['website'],ENT_QUOTES),
            'comment'   =>  htmlspecialchars($row['comment'],ENT_QUOTES),
            'timestamp' =>  htmlspecialchars($row['timestamp'],ENT_QUOTES)
        );

        /* 
            Assumed that the column holds boolean 1/0, 
            otherwise change logic here according to 
            data in the column.
        */
        if( intval( $row['options'] ) )==1 ) $right[]=$data;
        else $wrong[]=$data;
    }
    mysql_close($con);



    echo "<h1>User Comments</h1>"; 

    /* process RIGHT answers */
    echo "<div id='right'>";
    foreach( $right as $row ){
        echo "
        <div style='margin:30px 0px;'>
            Name: {$row->name}<br />
            Options: {$row->options}<br />
            Email: {$row->email}<br />
            Website: {$row->website}<br />
            Comment: {$row->comment}<br />
            Timestamp: {$row->timestamp}
        </div>";
    }
    echo "</div>";




    /* Process WRONG answers */
    echo "<div id='wrong'>";
    foreach( $wrong as $row ){

        echo "
        <div style='margin:30px 0px;'>
            Name: {$row->name}<br />
            Options: {$row->options}<br />
            Email: {$row->email}<br />
            Website: {$row->website}<br />
            Comment: {$row->comment}<br />
            Timestamp: {$row->timestamp}
        </div>";
    }
    echo "</div>";

?>