Reputation: 13
Here is a function I am writing:
function createDeck($deckId) {
global $dbConnection;
$queryCreateDeck = "SELECT c.card_name, dc.qty
FROM decks AS d
JOIN decks_to_cards AS dc
ON d.deck_id = dc.deck_id
JOIN cards AS c
ON dc.card_id = c.card_id
WHERE d.deck_id = {$deckId}
ORDER BY c.card_name";
$createDeck = mysqli_query($dbConnection, $queryCreateDeck);
$deckArray = array();
$deckFinal = array();
while ($deck = mysqli_fetch_assoc($createDeck)) {
$deckArray[] = $deck;
};
foreach ($deckArray["card_name"] as $card) {
while($deckArray["qty"] > 0) {
array_push($deckFinal, $deckArray["card_name"]);
};
};
print_r($deckArray);
};
This is what the Array $deckArray
is outputting:
Array (
[0] => Array (
[card_name] => Archangel Avacyn
[qty] => 4
)
[1] => Array (
[card_name] => Canopy Vista
[qty] => 4
)
[2] => Array (
[card_name] => Dromoka's Command
[qty] => 4
)
[3] => Array (
[card_name] => Evolutionary Leap
[qty] => 2
)
4] => Array (
[card_name] => Forest
[qty] => 9
)
[5] => Array (
[card_name] => Fortified Village
[qty] => 4
)
[6] => Array (
[card_name] => Gideon, Ally of Zendikar
[qty] => 4
)
[7] => Array (
[card_name] => Hangarback Walker
[qty] => 4
)
[8] => Array (
[card_name] => Nissa, Voice of Zendikar
[qty] => 4
)
[9] => Array (
[card_name] => Oath of Nissa
[qty] => 4
)
[10] => Array (
[card_name] => Plains
[qty] => 7
)
[11] => Array (
[card_name] => Secure the Wastes
[qty] => 2
)
[12] => Array (
[card_name] => Sylvan Advocate
[qty] => 4
)
[13] => Array (
[card_name] => Tragic Arrogance
[qty] => 2
)
[14] => Array (
[card_name] => Westvale Abbey
[qty] => 2
)
)
I am trying to create an array with an instance of each card_name for the qty in that array.
It is throwing an error at the foreach and subsequent while loops telling me the index is not correct.
However when I look at the array it seems that those are certainly the indexes. Any help at all would be greatly appreciated, I have been wracking my brain over this.
Upvotes: 0
Views: 43
Reputation: 13
This gave me the desired result, I was just overthinking the whole process. Thank you guys!
while ($deck = mysqli_fetch_assoc($createDeck)) {
while($deck['qty'] > 0) {
$deck['qty']--;
$deckFinal[] = $deck['card_name']; // push card name
};
};
Upvotes: 0
Reputation: 41893
If you're just trying to filter out with conditions WHERE qty > 0
why not just do it inside the query, then get the card names (the usual fetching). Much shorter route that way.
Should you go to the PHP route, just do it inside the while
block:
$deckFinal = array();
while ($deck = mysqli_fetch_assoc($createDeck)) {
if($deck['qty'] > 0) { // greater than zero
$deckFinal[] = $deck['card_name']; // push card name
}
}
No need to reassign to another container, then making another filter to pass into another array.
I'd take it a step further by using prepared statements, don't directly inject variables inside your query statement.
Use placeholders instead:
function createDeck($dbConnection, $deckId) {
$queryCreateDeck = ".
SELECT c.card_name, dc.qty
FROM decks AS d
JOIN decks_to_cards AS dc
ON d.deck_id = dc.deck_id
JOIN cards AS c
ON dc.card_id = c.card_id
WHERE d.deck_id = ?
AND dc.qty > 0
ORDER BY c.card_name
";
$createDeck = $queryCreateDeck->prepare($queryCreateDeck);
$createDeck->bind_param('i', $deckId);
$createDeck->execute();
$createDeck->bind_result($card_name, $qty);
$deckFinal = array();
while ($createDeck->fetch())) {
$deckFinal[] = $card_name;
// $deckFinal[] = ['card_name' => $card_name, 'qty' => $qty];
}
return $deckFinal;
}
Upvotes: 2