0

Now a very kind StackOverflow use has helped me out with a lot of my issues however there's two remaining probelms with my code before it's ready to go, any ideas would be great as i'm currently screaming at it:

First of all i'm using the following to try and pull data from a MySQL Database, return it as a Numeric Array and Order It By ID. There's 2 items in there and no matter what I do I can only get 1 to display (I need it to display ALL data when the table fills up more):

$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]);

?> 

Secondly, slightly off topic but this code below was given by a StackOverflow user however I can't get it to work, they've geared it to OOP which is not an area i'm familiar with and no matter what I do to correct the $this-> or public / private it still refuses to work (the aim of the code is to have a number in $userinput which gets checked against the $widgetBatches array for the closest match (i.e. input is 1100 and closest is 1000) this then gets deducted from the input (to leave 100) and the process loops again to check and this time returns 100 as the closest, this process continues until the $userinput reaches 0 or a negative number:

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


?>
Tom James
  • 33
  • 4
  • 1
    put your fetch_array in a while loop -> `while($row = $result->fetch_array(MYSQLI_NUM)){ printf ("%s (%s)\n", $row[0], $row[1]); }` – Sean May 27 '15 at 15:37
  • Thanks for the fast reply :) just tried that and it still wont budge, very odd - edit: I take it back, it seems to be working now thanks :) possibly a typo my end!!! any thoughts on the OOP? – Tom James May 27 '15 at 15:45
  • I'm kind of confused by the PHP code you've posted. If you're having trouble with $this, it's because it has no meaning in that context. If you want it to have a meaning, then this entire block of code needs to be wrapped up in a class. Alternately, you could pass $iterations in as a parameter to the Widget function if you really need that information.[Like this.](http://viper-7.com/dqRVoM) – thewatcheruatu May 27 '15 at 16:29
  • Your 2nd code block is not OOP, it is just regular functions. So `$this->`/`public`/`private` will not work, unless you change your code to OOP with `Class` instead of `function`. Note - `$this->iterations++;` will do nothing as your `$iteration` is not in scope of the function http://php.net/manual/en/language.variables.scope.php – Sean May 27 '15 at 17:20
  • in your current code format, here is a way to change/increase `$iteration` inside your function http://stackoverflow.com/a/4127831/689579 – Sean May 27 '15 at 17:26
  • Thanks for the replies, i'm trying to re-write it now in a 'while' loop to try and simplify the process, basically it needs to go through find the closest match to the user input from the array, decrease the total from the closest match found and run the loop again until it reaches 0 or a - number. – Tom James May 27 '15 at 20:26

2 Answers2

0

Did you try fetchAll(), I use PDO so not sure, but would suggest you use a while loop, like:

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

Or:

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

I'm %100 sure the loop will iterate and pull out every data, which you can send to a table or a list.

tadman
  • 208,517
  • 23
  • 234
  • 262
Raphael
  • 51
  • 6
  • This could use some better formatting. To automatically handle code, press the `{}` button when editing. – tadman May 27 '15 at 15:43
  • Thanks, the Array seems to be working now with @Sean solution, any thoughts on the OOP? – Tom James May 27 '15 at 15:50
  • @tadman are u referring to my reply?? I am new on here so just getting used to the environment – Raphael May 27 '15 at 15:50
  • Welcome to Stack Overflow. Just making some suggestions. Also do try and use full words, as many people here are not familiar with short-hand terms like "u" and rely on Google Translate to make sense of things, where words like that don't translate. – tadman May 27 '15 at 16:21
  • @tadman Thank you very much will take everything on board. I do have a question I posted and got no reply, can you please advise me on how for future references how to post question and get a reply please? – Raphael May 27 '15 at 16:51
  • Just made some quick edits to your answer to clean it up a little. It's not a big deal, but having code formatted properly makes the answer a lot easier to understand. The only specific advice I have is to make what you're saying as clear as possible, often by being neat and tidy. – tadman May 27 '15 at 16:53
  • @tadman You are a star! I dont know if you can help on this issue please: http://stackoverflow.com/questions/30484865/closing-featherlight-upon-submitting-form – Raphael May 27 '15 at 20:48
0

This:

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

?>

Hopefully the comments are useful... I put more detail in than I would usually...

  • Cheers again James :) it wasn't working still but it was server PHP version!!! was out by 1 so i've changed it and it's all working now – Tom James May 28 '15 at 11:35