0

I have generated a function that will go through my records within my database table and turn them into an array for further use

public function cycleCount()
{
    global $db;

    $selectProducts = $db->query("SELECT * FROM stockroom_products");

    $results = array("catA", "catB", "catC");

    while($row = $db->assoc($selectProducts))
    {
        if($row['Product_PRICE'] >= 10)
        {
            array_push($results, "catA", $row['ID']);
        }
        else if ($row['Product_PRICE'] >= 5 AND $row['Product_PRICE'] < 10) 
        {
            array_push($results, "catB", $row['ID']);
        }
        else if ($row['Product_PRICE'] > 0 AND $row['Product_PRICE'] < 5)
        {
            array_push($results, "catC", $row['ID']);
        }


        $json_data = json_encode($results);
        //echo $json_data;

        echo $json_data;

    }

}

And I can't seem to figure out how to make the array like

[catC[ID1, ID2, ID3]], [catB[ID1, ID2, ID3]], [catA[ID1, ID2, ID3]]

at the moment it does this

["catA","catB","catC","catB","1"]["catA","catB","catC","catB","1","catC","2"]["catA","catB","catC","catB","1","catC","2","catC","3"]["catA","catB","catC","catB","1","catC","2","catC","3","catC","4"]["catA","catB","catC","catB","1","catC","2","catC","3","catC","4","catA","5"]["catA","catB","catC","catB","1","catC","2","catC","3","catC","4","catA","5","catC","6"]["catA","catB","catC","catB","1","catC","2","catC","3","catC","4","catA","5","catC","6","catC","9"]["catA","catB","catC","catB","1","catC","2","catC","3","catC","4","catA","5","catC","6","catC","9","catA","10"]["catA","catB","catC","catB","1","catC","2","catC","3","catC","4","catA","5","catC","6","catC","9","catA","10","catC","11"]["catA","catB","catC","catB","1","catC","2","catC","3","catC","4","catA","5","catC","6","catC","9","catA","10","catC","11","catC","12"]["catA","catB","catC","catB","1","catC","2","catC","3","catC","4","catA","5","catC","6","catC","9","catA","10","catC","11","catC","12","catC","13"]["catA","catB","catC","catB","1","catC","2","catC","3","catC","4","catA","5","catC","6","catC","9","catA","10","catC","11","catC","12","catC","13","catA","14"]["catA","catB","catC","catB","1","catC","2","catC","3","catC","4","catA","5","catC","6","catC","9","catA","10","catC","11","catC","12","catC","13","catA","14","catA","16"]["catA","catB","catC","catB","1","catC","2","catC","3","catC","4","catA","5","catC","6","catC","9","catA","10","catC","11","catC","12","catC","13","catA","14","catA","16","catB","17"]["catA","catB","catC","catB","1","catC","2","catC","3","catC","4","catA","5","catC","6","catC","9","catA","10","catC","11","catC","12","catC","13","catA","14","catA","16","catB","17","catA","18"]["catA","catB","catC","catB","1","catC","2","catC","3","catC","4","catA","5","catC","6","catC","9","catA","10","catC","11","catC","12","catC","13","catA","14","catA","16","catB","17","catA","18","catA","19"]["catA","catB","catC","catB","1","catC","2","catC","3","catC","4","catA","5","catC","6","catC","9","catA","10","catC","11","catC","12","catC","13","catA","14","catA","16","catB","17","catA","18","catA","19","catB","20"]["catA","catB","catC","catB","1","catC","2","catC","3","catC","4","catA","5","catC","6","catC","9","catA","10","catC","11","catC","12","catC","13","catA","14","catA","16","catB","17","catA","18","catA","19","catB","20","catB","21"]
2
  • I suppose catC (and catA) should be the keys? Commented Nov 20, 2017 at 15:12
  • 1
    Also you would do json_encode and echo after the while-loop, not inside it. Commented Nov 20, 2017 at 15:13

3 Answers 3

2

You're pushing values to a string which doesn't work.

Try defining your result array like so: $result = array("catA" => array(), "catB" => array(), "catC" => array());

This should get you nested arrays and thus the result you seek. Also use array_push($result['catA'], $row['ID']); instead of array_push($reuslt, 'catA', $row[ID'])

And as @u_mulder correctly states the json should be echoed after the while loop!

Sign up to request clarification or add additional context in comments.

7 Comments

Instead of array_push, you should use [] - much more readable
Also mention that echoing json should be done after while-loop
@Philipp you can only use [] instead of array_push if you have a key and a value to go with it. Since this example only has a value you need to push it onto the array.
When I do this I end up with a white screen nothing is echoed
Are you encoding/echoing AFTER the while loop?
|
0

try it

      piblic  function cycleCount(){
    global $db;

    $selectProducts = $db->query("SELECT * FROM stockroom_products");
    $catA =array();
    $catB =array();
    $catC =array();
    $results = array();

    while($row = $db->assoc($selectProducts))
    {
        if($row['Product_PRICE'] >= 10)
        {
             array_push($catA,array('catA' =>$row['ID']));
        }
        else if ($row['Product_PRICE'] >= 5 AND $row['Product_PRICE'] < 10) 
        {
            array_push($catB,array('catB' =>$row['ID']));

        }
        else if ($row['Product_PRICE'] > 0 AND $row['Product_PRICE'] < 5)
        {

            array_push($catC,array('catC' =>$row['ID']));

        }


        $json_data = json_encode($results);
        //echo $json_data;

        echo $json_data;

    }
    $results=array($catA,$catB,$catC);
    print_r($results);
}
?>

1 Comment

Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ',' or ')' in C:\xampp\htdocs\assets\system\classes\functions.php on line 19 this is the line: array_push($catA, "catA"=> $row['ID']);
0

array_push does not key as second parameter according to the docs: http://php.net/manual/de/function.array-push.php

You should instead do it like this:

$results = array("catA" => [], "catB" => [], "catC" => []);
if($row['Product_PRICE'] >= 10)
{
    $results["catA"][] = $row['ID'];
}
else if ($row['Product_PRICE'] >= 5 AND $row['Product_PRICE'] < 10) 
{
    $results["catB"][] = $row['ID'];
}
else if ($row['Product_PRICE'] > 0 AND $row['Product_PRICE'] < 5)
{
    $results["catC"][] = $row['ID'];
}

Does that work for you?

4 Comments

Also mention that echoing json should be done after while-loop
When I move the echo after the while loop it shows only one record and when I do this above it echos this {"catA":[],"catB":["1"],"catC":[]}{"catA":[],"catB":[],"catC":["2"]}{"catA":[],"catB":[],"catC":["3"]}{"catA":[],"catB":[],"catC":["4"]}{"catA":["5"],"catB":[],"catC":[]}{"catA":[],"catB":[],"catC":["6"]}
Isn't that what you wanted? Here's an example of my code: glot.io/snippets/evo86oyc8p
The foreach simulates what your while loop is doing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.