简体   繁体   English

PHP:如何在MySQL的每第10行的新行中插入一个图像?

[英]PHP: How to Insert an Image in a new row for every 10th row in the MySQL?

I would like to insert a new random image in a row (all by itself) after 10 rows of data have been populated. 我想在填充10行数据后,在一行中插入一个新的随机图像(全部为自己)。

I'm thinking I should input something like this: 我想我应该输入这样的东西:

if($counter % 10 == 0) {
  echo 'image file';
}

But not really sure how to incorporate this in my code: 但不确定如何在我的代码中加入它:

 echo "<table border='1' CELLPADDING=5 STYLE='font-size:13px'>";
 echo "<tr> <td><H3>No.</H3></td><td><H3>Date</H3></td>";
 echo "<td><H3>First Name</H3></td> <td><H3>Last Name</H3></td>";
 echo "<td><H3>City</H3></td><td><H3>Province</H3></td></tr>";

 //declaring counter
 $count=0;

 // keeps getting the next row until there are no more to get
 while ($row = mysql_fetch_array( $data, MYSQL_ASSOC )) {
     //counter equals
     $count=$count+1;   

     // Print out the contents of each row into a table
     echo "</td><td>";
     echo $count;
     echo "</td><td>";
     echo $row['DateIn'];
     echo "</td><td>";
     echo $row['FirstName'];
     echo "</td><td>";
     echo $row['LastName'];
     echo "</td><td>";
     echo $row['City'];
     echo "</td><td>";
     echo $row['Province_State'];
     echo "</td></tr>";
}
echo "</table>";

if($counter % 10 == 0) { echo 'image file'; if($ counter%10 == 0){echo'image file'; } 10, not 100 :). 10,而不是100 :)。 Put this after your $count=$count+1 statement in a td colspan=however many columns you have. 把这个$ count = $ count + 1语句放在td colspan =你有多少列之后。

echo "<table border='1' CELLPADDING=5 STYLE='font-size:13px'>";
echo "<tr> <td><H3>No.</H3></td><td><H3>Date</H3></td><td><H3>First Name</H3></td> <td>    <H3>Last Name</H3></td> <td><H3>City</H3></td><td><H3>Province</H3></td></tr>";

//declaring counter
$count=0;

// keeps getting the next row until there are no more to get
while ($row = mysql_fetch_array( $data, MYSQL_ASSOC )) {

//counter equals
$count++;  

//insert an image every 10 rows
if($count==10){
    $count=0;
    echo 'yourimage.jpg';
}

// Print out the contents of each row into a table
echo "</td><td>";
echo $count;
echo "</td><td>";
echo $row['DateIn'];
echo "</td><td>";
echo $row['FirstName'];
echo "</td><td>";
echo $row['LastName'];
echo "</td><td>";
echo $row['City'];
echo "</td><td>";
echo $row['Province_State'];
echo "</td></tr>";
}
echo "</table>";

Thanks to the help of Here is the completed code that works (however, I still can't get the random image to show, but I can get an image :-) 感谢帮助,这里有完整的代码可以使用(但是,我仍然无法显示随机图像,但我可以得到一个图像:-)

    echo "<table border='1' CELLPADDING=5 STYLE='font-size:13px'>";
    echo "<tr> <td><H3>No.</H3></td><td><H3>Date</H3></td><td><H3>First Name</H3></td> <td><H3>Last Name</H3></td> <td><H3>City</H3></td><td><H3>Province</H3></td></tr>";

    //declaring counter for data
    $count=0;

    //declaring counter for random image
    $counter_for_image=0;

    // keeps getting the next row until there are no more to get
    while ($row = mysql_fetch_array( $data, MYSQL_ASSOC )) {

    //counter for actual count
    $count=$count+1;    

    //counter for image count
    //so I can reset count and
    //not affect actual count
    $counter_for_image++;



    /* does not work
    // start random image code
    $images = array(
    0 => '1.jpg',
    1 => '2.jpg',
    );
    $image = $images[ rand(0,(count($images)-1)) ];
    $randomimage = "<img src=\"/http://wwww.my site.com/storm/images/".$image."\" alt=\"\" border=\"0\" />";
    //print($output);
    // end random image code
    */

    // start every 10th row image display code  
    if($counter_for_image==10){
    $counter_for_image=0;

    echo '<tr><td colspan="6"><center><img src="http://www.mysite.com/storm/images/eric.jpg"/></center></td></tr>';

    /* would use this if I could get random image to work
    echo '<tr><td colspan="6"><center>';
    print($randomimage);
    echo '</center></td></tr>';
    */

    } // end every 10th row image display code  


    // Print out the contents of each row into a table
    echo "</td><td>";
    echo $count;
    echo "</td><td>";
    echo $row['DateIn'];
    echo "</td><td>";
    echo $row['FirstName'];
    echo "</td><td>";
    echo $row['LastName'];
    echo "</td><td>";
    echo $row['City'];
    echo "</td><td>";
    echo $row['Province_State'];
    echo "</td></tr>";
}
    echo "</table>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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