我如何回应mySQL数据库的每第4行不同的东西

时间:2017-03-18 15:08:46

标签: php html css mysql

我对PHP和mySQL相当新并且有小问题我正忙着尝试使用echo创建一个4列布局,目前我已经设法使用mySQL数据库实现了这一点,但最后一列需要和其他样式div类。凭借我有限的知识,我想到使用我在论坛上找到的解决方案之一尝试计数,但我想我可能错了。我的想法是回显数据库数组,然后进行计数并回显相同的内容块,但在div类中使用额外的css元素。非常感谢您的帮助。如果之前已经提出过这个问题,请接受我的道歉,我只是承受很大的压力才能解决问题。

见下面的当前PHP代码

  <?php
    $conn = mysqli_connect('localhost', 'root', 'password')
or die ( mysql_error());
$rs = mysqli_select_db($conn, 'database name' ) or die
( mysql_error());
$query = "SELECT * FROM database name";
$rs = mysqli_query( $conn, $query ) or die
( mysql_error());
while( $row = mysqli_fetch_array($rs, MYSQLI_BOTH))
{
$id = $row['id'];
$image = $row['image'];
$product = $row['product'];
$code = $row['code'];   
$material = $row['material'];
$sizes = $row['sizes']; 
echo "
<div class='percent-one-fourth'>

<div class='catalogue_content'>
<img src='bridal_gowns/$image.jpg' alt='Bridal Gown Catalogue -  $product'>
<h3>$product</h3>
<div class='content'>
<p id='content'><span style='font-weight:bold; color:#969;'>Code:</span> $code<br>
<span style='font-weight:bold; color:#969;'>Material:</span> $material<br>
<span style='font-weight:bold; color:#969;'>Sizes Available:</span> $sizes
<br>
<br>
</p>
<a href='mailto:lizette@bridalalchemy.co.za; raymond@bridalalchemy.co.za?subject=Enquiry from Web / Product: $product,  Code: $code;'><img src='enquire.jpg'></a>
</div>
</div>

</div>
";
}
$i = 0;
while( $row = mysqli_fetch_array($rs, MYSQLI_BOTH)){
  if($i++ % 4 == 0) {
    echo "
<div class='percent-one-fourth column-last'>

<div class='catalogue_content'>
<img src='bridal_gowns/$image.jpg' alt='Bridal Gown Catalogue -  $product'>
<h3>$product</h3>
<div class='content'>
<p id='content'><span style='font-weight:bold; color:#969;'>Code:</span> $code<br>
<span style='font-weight:bold; color:#969;'>Material:</span> $material<br>
<span style='font-weight:bold; color:#969;'>Sizes Available:</span> $sizes
<br>
<br>
</p>
<a href='mailto:lizette@bridalalchemy.co.za; raymond@bridalalchemy.co.za?subject=Enquiry from Web / Product: $product,  Code: $code;'><img src='enquire.jpg'></a>
</div>
</div>

</div>
";  
  }
}
mysqli_close($conn);
?>

2 个答案:

答案 0 :(得分:1)

由于您不熟悉PHP,因此了解代码无法正常运行的原因非常重要。所以,让我们一步一步地简单介绍一下简单的英语:

  1. 连接MySQL
  2. 运行数据库查询,其中有多行作为其响应
  3. 虽然还有更多的行......
    • 每行输出
  4. 虽然还有更多的行.. [!]总是假的 - 我们已经全部使用了它们
    • 每4个结果输出 [!]像$ image这样的Vars也没有在这里定义
  5. 因此,由于mysqli_fetch_array的工作方式,您可以看到我们需要循环使用仅一次。这对你也很好,因为它适合GET request limit

    编辑:你也在混合使用MySQL API; mysql_errormysqli_error非常不同。

    相反,在我们的一个循环中,我们将检查我们是否在第4行,并做出不同的反应。结果是这样的:

    <?php
    // 1. Connect to MySQL
    // (this should be in a separate file and included so you don't repeat it)
    $conn = mysqli_connect('localhost', 'root', 'password')
    or die ( mysqli_error()); // <-- *mysqli*
    
    $rs = mysqli_select_db($conn, 'database name' ) or die
    ( mysqli_error());
    
    // 2. Run the query which has multiple rows as its response:
    $query = "SELECT * FROM database name";
    $rs = mysqli_query( $conn, $query ) or die
    ( mysqli_error());
    
    // 3. Whilst there are more rows to go..
    $i=0;
    
    while( $row = mysqli_fetch_array($rs, MYSQLI_BOTH))
    {
    
    // Get easier references to various fields:
    $id = $row['id'];
    $image = $row['image'];
    $product = $row['product'];
    $code = $row['code'];   
    $material = $row['material'];
    $sizes = $row['sizes']; 
    
    // Start outputting this row:
    echo "<div class='percent-one-fourth";
    
    // The important part! We check here in this single loop:
    if($i++ % 4 == 0){
        // Every 4th row - output that extra class now!
        echo " column-last";
    }
    
    // Output everything else:
    echo "'>
    
    <div class='catalogue_content'>
    <img src='bridal_gowns/$image.jpg' alt='Bridal Gown Catalogue -  $product'>
    <h3>$product</h3>
    <div class='content'>
    <p id='content'><span style='font-weight:bold; color:#969;'>Code:</span> $code<br>
    <span style='font-weight:bold; color:#969;'>Material:</span> $material<br>
    <span style='font-weight:bold; color:#969;'>Sizes Available:</span> $sizes
    <br>
    <br>
    </p>
    <a href='mailto:lizette@bridalalchemy.co.za; raymond@bridalalchemy.co.za?subject=Enquiry from Web / Product: $product,  Code: $code;'><img src='enquire.jpg'></a>
    </div>
    </div>
    
    </div>
    ";
    }
    
    // Tidy up:
    mysqli_close($conn);
    ?>
    

答案 1 :(得分:0)

您的代码仅针对第4列回应。对于第1列,第2列和第3列,您需要添加else子句。

可选:除了回复你可以关闭php标签(?&gt;)并直接编写html。

...
$i = 0;
while( $row = mysqli_fetch_array($rs, MYSQLI_BOTH)){
  if($i++ % 4 == 0) {
    echo "html for the 4th column";
  }else{
    echo "html for column 1, 2, and 3";
  }
}