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

时间:2016-08-08 09:22:50

标签: php mysql html5

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

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

所以现在我想获取数据,以便所有选择'正确'的人进入左侧的第一列。所有选择“错误”的人都会进入右侧的第二列。

我不知道如何在网站上的两列(div)中分隔数据。

我有以下脚本:

Select Case

4 个答案:

答案 0 :(得分:1)

我可以想到两种方法:

第一个:您将用户分为两类,然后在相应的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>
    ";
}

第二个:你执行两个查询并显示它们:

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

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

答案 1 :(得分:0)

你必须将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);
?>

答案 2 :(得分:0)

首先创建两个新阵列,例如。将$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

答案 3 :(得分:0)

花了太长时间才回答,但另一种方法。我要说的一件事是,如果提供的id不是数字,则无法创建数据库连接,因此请在相关检查之后放置该代码。

<?

    $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>";

?>