Php Foreach:为指定行数添加换行符(段落)的正确方法
我在mysql数据库中有一堆随机的句子,我可以用下面的代码将它们提取出来并显示出来,但是,我在下一个过程中遇到了难题 如何为每X行添加nl或封装在标记中?选定的行已经限制为14行(任意),我的目标是将这14行每3或4行添加一个换行符(也是任意的),这样它们看起来更流畅Php Foreach:为指定行数添加换行符(段落)的正确方法,php,mysql,foreach,while-loop,Php,Mysql,Foreach,While Loop,我在mysql数据库中有一堆随机的句子,我可以用下面的代码将它们提取出来并显示出来,但是,我在下一个过程中遇到了难题 如何为每X行添加nl或封装在标记中?选定的行已经限制为14行(任意),我的目标是将这14行每3或4行添加一个换行符(也是任意的),这样它们看起来更流畅 <?php include $_SERVER['DOCUMENT_ROOT'] . '/dbConnect.php'; $q = $dbc->query("SELECT DISTINCT sentence F
<?php
include $_SERVER['DOCUMENT_ROOT'] . '/dbConnect.php';
$q = $dbc->query("SELECT DISTINCT sentence FROM sentences ORDER BY rand() LIMIT 14");
while($r = $q->fetch_array(MYSQLI_ASSOC)):
foreach($r as $value) {
$value = str_replace('$keyword', '<b>replaced keyword</b>', $value);
echo $value." ";
}
endwhile;
?>
但是,我希望它能做更多类似的事情,而不会创建一个低效的3块相同的代码,限制为3、2、4或类似的代码
This is a complete sentence that I'm outputting to the page.
This is a complete sentence that I'm outputting to the page.
This is a complete sentence that I'm outputting to the page.
This is a complete sentence that I'm outputting to the page.
This is a complete sentence that I'm outputting to the page.
This is a complete sentence that I'm outputting to the page.
This is a complete sentence that I'm outputting to the page.
This is a complete sentence that I'm outputting to the page.
This is a complete sentence that I'm outputting to the page.
我想这就是你想要的:
<?php
include $_SERVER['DOCUMENT_ROOT'] . '/dbConnect.php';
$i=0; $j=0;
$seq=array(3, 2, 4);
$q = $dbc->query("SELECT DISTINCT sentence FROM sentences ORDER BY rand() LIMIT 14");
while($r = $q->fetch_array(MYSQLI_ASSOC)):
foreach($r as $value) {
$value = str_replace('$keyword', '<b>replaced keyword</b>', $value);
echo $value."<br>";
if($i==$seq[$j])
{
echo "<br>";
$j++;
$i=0;
if($j==count($seq)) $j=0;
} else {
$i++;
}
}
endwhile;
?>
希望有帮助 以下是我对你的问题的看法:
$numberOfSentencesPerParagraph = 4; // specify
$totalNumberOfSentences = 14; // or use a count function to calculate the number of returned MySQL records // or make this equal to a new variable you will introduce in your SQL query (after `LIMIT`)
while($r = $q->fetch_array(MYSQLI_ASSOC)):
$theCounter = 1;
foreach($r as $value) {
$value = str_replace('$keyword', '<b>replaced keyword</b>', $value);
echo $value." ";
$theCounter++;
if(($theCounter % $numberOfSentencesPerParagraph) == 0){
echo "<br>";
}
}
endwhile;
$numberOfSentencesPerParagraph=4;//具体说明
$TotalNumberOfSequences=14;//或者使用count函数计算返回的MySQL记录数//或者使其等于将在SQL查询中引入的新变量(在“LIMIT”之后)
而($r=$q->fetch_数组(MYSQLI_ASSOC)):
$theCounter=1;
foreach($r为$value){
$value=str_replace(“$keyword”,“replaced keyword”,“$value”);
回声$value.“;
$theCounter++;
如果($theCounter%$NumberOfSentenceSperparGraph)==0){
回声“
”;
}
}
结束时;
它只是运行一个“计数器”,当它的值是变量的倍数时,它就会发出一个换行符
$numberOfSentencesPerParagraph = 4; // specify
$totalNumberOfSentences = 14; // or use a count function to calculate the number of returned MySQL records // or make this equal to a new variable you will introduce in your SQL query (after `LIMIT`)
while($r = $q->fetch_array(MYSQLI_ASSOC)):
$theCounter = 1;
foreach($r as $value) {
$value = str_replace('$keyword', '<b>replaced keyword</b>', $value);
echo $value." ";
$theCounter++;
if(($theCounter % $numberOfSentencesPerParagraph) == 0){
echo "<br>";
}
}
endwhile;