php while loop variable for every third div

后端 未结 4 482
再見小時候
再見小時候 2020-12-10 16:23

Is their a way in a while loop to assign a variable to a class in a div, for every third item in a while loop. I am using the blueprint structure and the third div is at the

4条回答
  •  醉话见心
    2020-12-10 17:02

    for ($i = 0; $i < $numRecords; $i++)
    {
     $className = "";
     if (($i % 3) == 0)
     {
      $className = "last"
     }
    
     ....
    }
    

    The key part here is the ($i % 3) == 0.

    EDIT: The following is in response to your comment.

    /* LOOP THROUGH SHOEDATA TABLE */
    
    $results = mysql_query("SELECT * FROM shoeData");
    
    $i = 0;
    while($row = mysql_fetch_array($results)){
    $i++;
    $name = $row['name'];
    $about = $row['about'];
    $company = $row['company'];
    $buy = $row['buy'];
    $tags = $row['tags'];
    $id = $row['id'];
    $image = $row['image'];
    
    
    /* ECHO THE SHOEDATA RESULTS */         
        $additionalClass = ($i % 3) == 0 ? " last" : "";
        echo "
    "; echo "
    "; echo ""; echo "
    "; echo "
      "; echo "
    • $name"; echo "
    • $about"; echo "
    • $company"; echo "
    • BUY"; echo "
    • $tags"; echo "
    "; echo "
    "; }/*SHOEDATA WHILE LOOP ENDS */

提交回复
热议问题