PHP while循环行数

时间:2016-04-29 10:34:59

标签: php html css loops

我正在努力与php循环。目标是从数据库中获取引用并在每行上显示3列。现在看来,while循环每行显示一列。我该如何纠正这个问题?

 <?php

$servername = "localhost";
$username = "";
$password = "";
$dbname = "";


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

if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
} 
$row = $ip['id']; 
$row = $ip['quote'];  
$row = $ip['topic'];
$row = $ip['author'];
$nr = 0;


$sql = "SELECT * FROM quotes ORDER BY date DESC limit 10";
$result = $conn->query($sql);


if ($result->num_rows > 0) {



     while($row = $result->fetch_assoc() ) {
         $nr++;


          echo  
        "<div class='container row'>
          <div class='col s12 m6 l4 z-depth-1'>
          <div class='card-panel grey darken-4 white-text center'><h5>Citat: ". $row["id"] ."</h5></div> <pre class='flow-text black-text' wrap='soft'>" ."<p class=''>Författare: ". $row["author"] ."</p>" 
          . "<p class=''>Citat: ". $row["quote"] ."</p>" .  $row["topic"] ."</pre>

        <div class='content_wrapper'>
    <h4>Vote </h4> 

        <div class='voting_wrapper' id='". $row["id"] ."'>
            <div class='voting_btn'>
                <div class='up_button'>&nbsp;</div><span class='up_votes'>0</span>
            </div>
            <div class='voting_btn'>
                <div class='down_button'>&nbsp;</div><span class='down_votes'>0</span>
            </div>
             <br>
        </div>


        </div>
        </div>
</div>";

}
 }else {
     echo "0 results";
}

$conn->close();
?> 

7 个答案:

答案 0 :(得分:1)

你可以这样做:

<?php

$servername     = "localhost";
$username       = "";
$password       = "";
$dbname         = "";


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

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

//DO YOU NEED THESE VARIABLES? I DON'T SEE THEIR USE HERE... BESIDES, $row DOES NOT EXIST YET...
$row    = $ip['id'];
$row    = $ip['quote'];
$row    = $ip['topic'];
$row    = $ip['author'];
$nr     = 0;


$sql = "SELECT * FROM quotes ORDER BY date DESC limit 10";
$result = $conn->query($sql);


if ($result->num_rows > 0) {
    $output     = "";
    while($row  = $result->fetch_assoc() ) {
        $topic   = trim($row["topic"]);
        $quote   = trim($row["quote"]);
        $author  = trim($row["author"]);
        $id      = trim($row["id"]);

        $output .= injectNColumnWrapper(3, $nr, "container row", $nr);
        $output .="<div class='col s12 m6 l4 z-depth-1'>";
        $output .="<div class='card-panel grey darken-4 white-text center'>";
        $output .=" <h5>Citat: {$id}</h5>";
        $output .="</div>";
        $output .="pre class='flow-text black-text' wrap='soft'>";
        $output .="<p class='flow-text-p author'>Författare: {$author}</p>";
        $output .="<p class='flow-text-p citat'>Citat: {$quote}</p>";
        $output .="<p class='flow-text-p topic'>{$topic}</p>";
        $output .="</pre>";
        $output .="<div class='content_wrapper'>";
        $output .="<h4>Vote </h4>";
        $output .="<div class='voting_wrapper' id='vote-{$id}'>";
        $output .="<div class='voting_btn'>";
        $output .="<div class='up_button'>&nbsp;</div>";
        $output .="<span class='up_votes'>0</span>";
        $output .="</div>";
        $output .="<div class='voting_btn'>";
        $output .="<div class='down_button'>&nbsp;</div>";
        $output .="<span class='down_votes'>0</span>";
        $output .="</div>";
        $output .="<br>";
        $output .="</div>";
        $output .="</div>";
        $output .="</div>";
        $nr++;

    }
    $output    .= "</div>";
    echo $output;
}else {
    echo "0 results";
}

$conn->close();


function injectNColumnWrapper($cols_per_row, $closePoint, $cssClass="container row", $nthElem=""){
    $blockDisplay       = "";
    if( ($closePoint == 0) ){
        $blockDisplay   = "<div class='" . $cssClass . " container_nr_" . $nthElem . "'>"  . PHP_EOL;
    }else if( ($closePoint % $cols_per_row) == 0 && ($closePoint != 0) ){
        $blockDisplay   = "</div><div class='" . $cssClass . " container_nr_" . $nthElem . "'>"  . PHP_EOL;
    }
    return $blockDisplay;
}

&GT;

每行应该有3列,如下所示:

<div class="container">
    <div class="col s12 m6 l4 z-depth-1">Column 1</div>
    <div class="col s12 m6 l4 z-depth-1">Column 2</div>
    <div class="col s12 m6 l4 z-depth-1">Column 3</div>
</div>

我希望这有点帮助...

答案 1 :(得分:0)

阐述@RuchishParikh评论,其中已包含解决方案的核心:

$nr = 0;
while ($row = $result->fetch_assoc()) {
  $nr++;
  if ($nr % 3 == 0) {
    echo "<div class='container row'>\n"; # start of row
  }
  echo "<div class='col s4 m6 l4 z-depth-1'>\n"; # start of column
  echo "  ...\n";
  echo "</div>\n"; # end of column
  if ($nr % 3 == 0) {
    echo "</div>\n"; # end of row
  }
}

答案 2 :(得分:0)

试一试: -

<?php

$servername = "localhost";
$username = "";
$password = "";
$dbname = "";


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

if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
} 
$row = $ip['id']; 
$row = $ip['quote'];  
$row = $ip['topic'];
$row = $ip['author'];
$nr = 0;


$sql = "SELECT * FROM quotes ORDER BY date DESC limit 10";
$result = $conn->query($sql);

$chngrow=0;

if ($result->num_rows > 0) {



     while($row = $result->fetch_assoc() ) {
         $nr++;

   $chngrow = $chngrow + 1;

          echo  
        "<div class='container row'>
          <div class='col s12 m6 l4 z-depth-1'>
          <div class='card-panel grey darken-4 white-text center'><h5>Citat: ". $row["id"] ."</h5></div> <pre class='flow-text black-text' wrap='soft'>" ."<p class=''>Författare: ". $row["author"] ."</p>" 
          . "<p class=''>Citat: ". $row["quote"] ."</p>" .  $row["topic"] ."</pre>

        <div class='content_wrapper'>
    <h4>Vote </h4> 

        <div class='voting_wrapper' id='". $row["id"] ."'>
            <div class='voting_btn'>
                <div class='up_button'>&nbsp;</div><span class='up_votes'>0</span>
            </div>
            <div class='voting_btn'>
                <div class='down_button'>&nbsp;</div><span class='down_votes'>0</span>
            </div>
             <br>
        </div>


        </div>
        </div>
</div>";

          $mod = ($chngrow % 3);
          echo ($mod==0) ? "<br>" : "";

}
 }else {
     echo "0 results";
}

$conn->close();
?> 

答案 3 :(得分:0)

你弄乱了变量。 不需要在下面声明$ row:

    $row = $ip['id']; 
    $row = $ip['quote'];  
    $row = $ip['topic'];
    $row = $ip['author'];

如此处Why shouldn't I use mysql_* functions in PHP?所述,不要使用mysql_ *函数。

您需要获取3行并将它们放在一个容器中。

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

$db = new PDO("mysql:host=$servername;dbname=$dbname;charset=UTF-8", 
              $username 
              $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

$sql = "SELECT * FROM quotes ORDER BY date DESC limit 10";
$nr = 0;

$stmt = $db->query('SELECT * FROM table');

while($row1 = $stmt->fetch(PDO::FETCH_ASSOC)) {
   $row2 = $stmt->fetch(PDO::FETCH_ASSOC);
   $row3 = $stmt->fetch(PDO::FETCH_ASSOC);
   $rows[] = $row1;
   if ($row2) $rows[] = $row2;
   if ($row3) $rows[] = $row3;

   echo "<div class='container row'>\n"

   foreach($rows as $row) {
      $nr++;
      $id_     = $row["id"];
      $author_ = $row["author"];
      $quote_  = $row["quote"];
      $topic_  = $row["topic"];

      echo 
        "<div class='col s12 m6 l4 z-depth-1'>
         <div class='card-panel grey darken-4 white-text center'><h5>Citat:$id_ </h5></div> 
           <pre class='flow-text black-text' wrap='soft'>
              <p class=''>Författare: $author_</p> 
              <p class=''>Citat: $quote_</p>
              $topic_
           </pre>

        <div class='content_wrapper'>
           <h4>Vote </h4> 
           <div class='voting_wrapper' id='$id_'>
            <div class='voting_btn'>
                <div class='up_button'>&nbsp;</div><span class='up_votes'>0</span>
            </div>
            <div class='voting_btn'>
                <div class='down_button'>&nbsp;</div><span class='down_votes'>0</span>
            </div>
            <br />
        </div>
        </div>
        </div>";
    }

    echo "</div>\n";
    $rows = null;
}

if ($nr == 0){
   echo "0 results";
}
?>

答案 4 :(得分:0)

试试这个,

devicePushToken

答案 5 :(得分:0)

为每个循环增加一个计数器变量。在循环外打开表标签和tr标签。 while循环应该只有td标记。如果count%3 == 0则关闭tr标签并打开一个新的tr标签。

$i=0
?><table><tr><?
while(condition)
{
    $i++;
    if($i%3==0)
    {
        ?></tr><tr><?
    }

    ?><td>your data</td><?
}
?></tr></table><?

答案 6 :(得分:0)

试试这个

$i=0
?><table><tr><?
while(condition)
{
    $i++;
    if($i%3==0)
    {
        ?></tr><tr><?
    }

    ?><td>your data</td><?
}
?></tr></table><?