OOP问题停止PHP功能& MySQL数据数据检索问题

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

标签: php mysql

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

首先,我使用以下方法尝试从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,而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;
}


?>

2 个答案:

答案 0 :(得分:0)

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

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

或者:

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

我确定循环将迭代并提取每个数据,您可以将其发送到表格或列表。

答案 1 :(得分:0)

此:

<?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);

?>

希望这些评论很有用......我会提供比平常更多的细节......