【问题标题】:OOP Issue Stopping PHP Functioning & MySQL Data Array Retrieval IssueOOP 问题停止 PHP 功能和 MySQL 数据数组检索问题
【发布时间】:2015-05-27 15:31:27
【问题描述】:

现在,一个非常友好的 StackOverflow 使用帮助我解决了很多问题,但是在我的代码准备就绪之前还有两个问题,任何想法都会很棒,因为我目前正在尖叫:

首先,我使用以下内容尝试从 MySQL 数据库中提取数据,将其作为数字数组返回并按 ID 排序。里面有 2 个项目,无论我做什么,我只能显示 1 个(当表格填满时,我需要它来显示所有数据):

$query = "SELECT * FROM batch ORDER by ID";
$result = $mysqli->query($query);

/* numeric array */
$row = $result->fetch_array(MYSQLI_NUM);
printf ("%s (%s)\n", $row[0], $row[1]);

?> 

其次,稍微偏离主题,但下面的代码是由 StackOverflow 用户提供的,但是我无法让它工作,他们已经将它用于 OOP,这不是我熟悉的领域,无论我做什么纠正 $this->public / private 它仍然拒绝工作(代码的目的是在 $userinput 中有一个数字,它会根据 $widgetBatches 数组检查最接近的匹配(即输入是1100 和最接近的是 1000)然后从输入中扣除(留下 100)并且过程再次循环检查,这次返回 100 作为最接近的,这个过程继续直到 $userinput 达到 0 或负数:

<?php

$userinput = 10000; // Our magic variable - user input

$iterations = 0;

function Widget($userinput)
{
    $this->iterations++;
    $widgetRequested = $userinput;
    $widgetBatches = array("250", "500", "1000", "2000");

    echo "Iteration " . $iterations;
    echo "<br/>";

    echo "Widget requested: " . $widgetRequested;
    echo "<br/>";

    $closest = GetClosest($widgetBatches, $widgetRequested);
    echo "Closest: " . $closest;
    echo "<br/>";

    $widgetRemaining = $widgetRequested - $closest;
    echo "Remainder: " . $widgetRemaining;

    echo "<hr/>";
    if($widgetRemaining > 0)
    {
        Widget($widgetRemaining);
    }
    else
    {
        echo "The value is now below or equaling zero: " . $widgetRemaining . "!";
    }
}

function GetClosest($array, $value)
{
    $lowest = null;
    foreach($array as $val)
    {
        if($lowest == null || abs($value - $lowest) > abs($val - $value))
        {
            $lowest = $val;
        }
    }
    return $lowest;
}


?>

【问题讨论】:

  • 把你的 fetch_array 放到一个 while 循环中 -> while($row = $result-&gt;fetch_array(MYSQLI_NUM)){ printf ("%s (%s)\n", $row[0], $row[1]); }
  • 感谢您的快速回复 :) 刚刚尝试过,但它仍然不会让步,非常奇怪 - 编辑:我收回它,它现在似乎可以工作了,谢谢 :) 可能是我的错字! !对 OOP 有什么想法吗?
  • 我对您发布的 PHP 代码感到有些困惑。如果您在使用 $this 时遇到问题,那是因为它在该上下文中没有任何意义。如果你想让它有意义,那么整个代码块需要被包装在一个类中。或者,如果您确实需要该信息,您可以将 $iterations 作为参数传递给 Widget 函数。Like this.
  • 您的第二个代码块不是 OOP,它只是常规函数。所以$this-&gt;/public/private 将不起作用,除非您使用Class 而不是function 将代码更改为OOP。注意 - $this-&gt;iterations++; 不会做任何事情,因为您的 $iteration 不在函数 php.net/manual/en/language.variables.scope.php 的范围内
  • 在您当前的代码格式中,这是一种在您的函数stackoverflow.com/a/4127831/689579 中更改/增加$iteration 的方法

标签: php mysql


【解决方案1】:

这个:

<?php

function Widget($input) {
    $currentValue = $input; // Set internal variable from input (Think of this as an initial "remainder")
    $i = 0;
    $widgetBatches = [250, 500, 1000, 2000]; // Setup batch array

    while ($currentValue > 0) { // While the remainder is more than 0
        $i++;
        echo "Iteration " . $i . "<br/>";
        echo "Widget requested: " . $currentValue . "<br/>";
        $closest = GetClosest($widgetBatches, $currentValue); // Find the closest value from batch array
        echo "Closest: " . $closest . "<br/>";
        $currentValue = $currentValue - $closest; // Work out new remainder
        echo "Remainder: " . $currentValue . "<hr/>";
    }

    // Loop will exit when remainder is less than 0
    echo "The value is now below or equaling zero: " . $currentValue . "!";
}

function GetClosest($array, $value) {
    $result = null; // Innitialise the returned variable in case of failure
    foreach($array as $val) { // For every array value, unless stopped
        $result = $val; // Set result to current array value
        if($value <= $result) break; // Stop foreach loop if value is less than or equal to result
    }
    return $result; // Return last result from Foreach loop
}

Widget(9000);

?>

希望这些 cmets 有用...我比平时更详细...

【讨论】:

  • 再次干杯詹姆斯 :) 它仍然无法正常工作,但它是服务器 PHP 版本!!!早在 1 点就出来了,所以我已经更改了它,现在一切正常
【解决方案2】:

你试过 fetchAll(),我用的是 PDO,所以不确定,但建议你使用 while 循环,比如:

while ($result = $mysqli->query($query));

或者:

foreach($result as $r) then $r['data'];

我 %100 确定循环会迭代并提取所有数据,您可以将这些数据发送到表格或列表。

【讨论】:

  • 这可以使用更好的格式。要自动处理代码,请在编辑时按{} 按钮。
  • 谢谢,阵列现在似乎正在与@Sean 解决方案一起工作,对 OOP 有什么想法吗?
  • @tadman 你指的是我的回复吗??我是新来的,所以只是习惯了环境
  • 欢迎来到 Stack Overflow。只是提出一些建议。也请尝试使用完整的单词,因为这里的许多人不熟悉诸如“u”之类的简写术语,而是依靠谷歌翻译来理解诸如此类单词无法翻译的事物。
  • @tadman 非常感谢你会接受一切。我确实有一个问题我发布了但没有得到回复,你能告诉我如何为将来的参考如何发布问题并得到回复吗?
猜你喜欢
  • 2012-04-20
  • 1970-01-01
  • 2015-03-27
  • 2011-07-19
  • 2011-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-02
相关资源
最近更新 更多