
[英]How to create a string with x numbers of the same character without looping in PHP
[英]PHP/MYSQL BETWEEN Numbers Looping same function
我已经尝试解决了好几天,并且我确定合适的人可以轻松地做到这一点。
<?php
displayMatrixNumbers();
?>
<?php
function displayMatrixNumbers()
{
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
$statement = "SELECT number, association, image_file, skeleton, sound, colour, comments ";
$statement .= "FROM num_image ";
$statement .= "ORDER BY number ";
$sqlResults = selectResults($statement);
$error_or_rows = $sqlResults[0];
if (substr($error_or_rows, 0 , 5) == 'ERROR')
{
print "<br />Error on DB";
print $error_or_rows;
} else {
$arraySize = $error_or_rows;
for ($i=1; $i <= $error_or_rows; $i++)
{
$number = $sqlResults[$i]['number'];
$association = $sqlResults[$i]['association'];
$image_file = $sqlResults[$i]['image_file'];
$skeleton = $sqlResults[$i]['skeleton'];
$sound = $sqlResults[$i]['sound'];
$colour = $sqlResults[$i]['colour'];
$comments = $sqlResults[$i]['comments'];
print "<div id='mcnumbers-container'>";
print "<h3>".$number."</h3>";
print "<p><img id='mcnumber-image' src='images/matrix/".$number. ' - '.$association.".jpg'>";
print "<br />".$association."</p>";
print "</div>";
}
}
}
?>
我需要插入的行数吗? 可以在某个时候派上用场吗?
我要完成的主要目标是:
我想使用此代码并将函数displayMatrixNumbers()粘贴到不同的DIV选项卡中100次,或者使用函数不是最佳实践?
在pastebin中,我想更改以下行: $statement .= "ORDER BY number ";
到$statement .= "BETWEEN 00 to 09 ORDER BY ASC ";
或类似的东西?
但是,我需要将两个数字从“ 00到09”更改为“ 10到19”再更改为“ 20更改为29”,依此类推。
简而言之,可以从函数外部仅更改BETWEEN数字。 这样,我可以输入两个数字$lownum = '00'; $highnum = '09';
$lownum = '00'; $highnum = '09';
然后$statement .= "BETWEEN $lownum to $highnum ORDER BY ASC "
说我有3个DIV
<div id="div1">
<?php displayMatrixNumbers(); ?> /* This one displays 00 to 09 */
</div>
<div id="div2randomID">
<?php displayMatrixNumbers(); ?> /* 10 to 19 */
</div>
<div id="div3">
<?php displayMatrixNumbers(); ?> /* 20 to 29.. and so on */
</div>
假设我有100个div。如何在不创建100个单独的.php页面的情况下,具体告诉每个人仅显示所需的结果? 我知道我需要某种仅编辑BETWEEN数字之间的循环,但不知道如何循环吗?
我必须正确地重写它以使其更清楚,但我的大脑却发疯了,希望有人能理解我的意思。 即使您需要我澄清一些问题,也请询问。
我很陌生,如果不是我用过的所有PHP,我深表歉意,并希望向您展示如何使用PHP做到这一点。
干杯,丹:)
使用类来解决永久显示。 类包含可以存储和重用的数据。
class display {
private $count = 0;
public function display(){
for($i = $this->count; $i < $this->count + 10; $i++){
echo $i;
}
$this->count += 10;
}
}
$display = new display();
$display->display(); //prints 0 - 9
$display->display(); // prints 10 - 19
$display->display(); //prints 20 - 21
如果使用LIMIT sql子句,则可以轻松完成此操作
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
$statement = "SELECT number, association, image_file,
skeleton, sound, colour, comments
FROM num_image
ORDER BY number
LIMIT $start, 10";
$sqlResults = selectResults($statement);
$error_or_rows = $sqlResults[0];
// I am not sure what library you are using to access your database
// but there must be a better way of identifying errors
// than this next piece of code
if (substr($error_or_rows, 0 , 5) == 'ERROR')
{
print "<br />Error on DB";
print $error_or_rows;
} else {
foreach ( $sqlResults as $row ) {
$number = $row['number'];
$association = $srow['association'];
$image_file = $row['image_file'];
$skeleton = $row['skeleton'];
$sound = $row['sound'];
$colour = $row['colour'];
$comments = $row['comments'];
print "<div id='mcnumbers-container'>";
print "<h3>".$number."</h3>";
print "<p><img id='mcnumber-image' src='images/matrix/".$number. ' - '.$association.".jpg'>";
print "<br />".$association."</p>";
print "</div>";
}
}
}
然后使用您要使用的开始行来调用它
<div id="div1">
<?php displayMatrixNumbers(0); ?> /* This one displays 00 to 09 */
</div>
<div id="div2randomID">
<?php displayMatrixNumbers(10); ?> /* 10 to 19 */
</div>
<div id="div3">
<?php displayMatrixNumbers(20); ?> /* 20 to 29.. and so on */
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.