Hector James Haddow
Hector James Haddow

Reputation: 123

call from an array with an array

i want to call from an array using the get array but it doesn't work what is the correct way to format this?

if(isset($_GET["category"])){
    $cat = 'SELECT DISTINCT `Category` FROM `products`';
    $result = $mysqli->query($cat);
    $i='0';
    while ($row = $result->fetch_array(MYSQLI_ASSOC)){
        $i++;
        $Category = $row["Category"];
        $id = array($i => $Category);
    }
    $result->close();
    $query = 'SELECT * FROM products WHERE Category="'. $id[$_GET["category"]]. '" ORDER BY Code';
}

Upvotes: 0

Views: 79

Answers (2)

user1188420
user1188420

Reputation:

It seems, that you made a mistake inside the loop. If you want to assign a value to an array key, you need to replace this:

$id = array($i => $Category);

with this:

$id[$i] = $Category;

Otherwise $id would always be a new array only with the current key-value pair.

Upvotes: 1

juergen d
juergen d

Reputation: 204854

your query is wrong. use

$query = "SELECT * FROM products WHERE Category='". $id[$_GET["category"]]. "' ORDER BY Code";

The order by clause must be after the where clause.

Upvotes: 0

Related Questions