简体   繁体   中英

How would I echo something different with every 4th row of a mySQL database

I am fairly new to PHP and mySQL and have small issue I am busy trying to create a 4 column layout with using echo and currently I have managed to achieve this with with a mySQL database but the last column requires and additional style in the div class. With my Limited knowledge I thought of trying a count using one of the solutions I found on the forum, but I think I may be using it wrong. my idea is to echo the database array, then do a count and echo the same content block but the with additional css element in div class. Your help is greatly appreciated. if this question has been asked before please accept my apologies I am just under a lot of pressure to get it resolved.

SEE CURRENT PHP Code Below

  <?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);
?>

As you're new to PHP, it's really important that you understand why your code isn't working properly. So, let's step over it and describe it in brief, simple English:

  1. Connect to MySQL
  2. Run a database query which has multiple rows as its response
  3. Whilst there are more rows to go..
    • Output every row
  4. Whilst there are more rows to go.. [!] Always false - we already used them all up
    • Output every 4th result [!] Vars like $image also aren't defined here

So, because of the way how mysqli_fetch_array works, you can hopefully see that we need to loop using it only once . This is also great for you, because it fits with the DRY (don't repeat yourself) principle .

Edit: You were also mixing MySQL API's; mysql_error is very different from mysqli_error .

So instead, during our one loop, we'll check if we're on the 4th row, and react differently. That results in something like this:

<?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);
?>

Your code is echoing only for the 4th column. For columns 1, 2, and 3, you need to add an else clause.

Optional: Besides instead of echoing you could close the php tag (?>) and write the html directly.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM