从mysql数据库中获取时,每个X结果都不同

时间:2015-07-15 07:31:27

标签: php mysql

我有简单的php foreach循环,从MySQL数据库中获取一些数据

$result = $pdo->prepare( "SELECT * FROM table ORDER BY RAND() LIMIT 30");
$result->execute();
foreach ($result as $row)
{
    echo 'data';                            
}

如您所见,有LIMIT 30.是否可以在每10个结果上插入不同的数据。Data which is not from database。它的静态数据并不会改变。我的意思是像

if ( $row=10 )
{
   echo $row['name'];
}
else
{
   echo '<div> some static text not from database </div>';
}

2 个答案:

答案 0 :(得分:1)

$counter=0;
foreach ($result as $row)
{
    $counter++;
    if($counter %10==0){} //10th result
    else{} //not 10th result
}

答案 1 :(得分:0)

$counter = 0; //This is the counter which we will use to count row numbers.
$result = $pdo->prepare( "SELECT * FROM table ORDER BY RAND() LIMIT 30");
$result->execute();
foreach ($result as $row)
{
    $counter++; //We are incrementing the counter.
    echo $row['name'];
    //If we are at a row which is multiple of 10, we output a static value.
    if($counter % 10 == 0) 
        echo 'Hello World!';               
}