PHP/MYSQL BETWEEN Numbers 循环相同的函数
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>";
}
}
}
?>
我需要插入的行数吗?到时候能派上用场吗?
我要完成的主要目标是:
尽可能简化我的代码。
我想使用此代码并在不同的 DIV 选项卡中粘贴函数 displayMatrixNumbers() 100 次,或者使用函数可能不是最佳做法?
在 pastebin 中,我想将行:$statement .= "ORDER BY number ";
更改为 $statement .= "BETWEEN 00 to 09 ORDER BY ASC ";
或类似的内容?
但是,对于我正在构建的项目,我需要将两个数字之间的值从“00 到 09”更改为“10 到 19”再到“20 到 29”等等。
简而言之,是否可以从函数外部仅更改 BETWEEN 数字。这样 'some how' 我可以输入两个数字 $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解决永久显示问题。 类 保存可以存储和重复使用的数据。
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>
几天来我一直在努力解决这个问题,我相信合适的人可以轻松解决。
<?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>";
}
}
}
?>
我需要插入的行数吗?到时候能派上用场吗?
我要完成的主要目标是:
尽可能简化我的代码。
我想使用此代码并在不同的 DIV 选项卡中粘贴函数 displayMatrixNumbers() 100 次,或者使用函数可能不是最佳做法?
在 pastebin 中,我想将行:
$statement .= "ORDER BY number ";
更改为$statement .= "BETWEEN 00 to 09 ORDER BY ASC ";
或类似的内容?
但是,对于我正在构建的项目,我需要将两个数字之间的值从“00 到 09”更改为“10 到 19”再到“20 到 29”等等。
简而言之,是否可以从函数外部仅更改 BETWEEN 数字。这样 'some how' 我可以输入两个数字 $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解决永久显示问题。 类 保存可以存储和重复使用的数据。
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>