FaT3oYCG Posted March 24, 2009 Share Posted March 24, 2009 Hi, im creating a little script for a card game that i know that will calculate and work out how long it will take someone to win at the game with a randomised deck of cards, i will eventually make a form and make it all submittable but untill then im using my database and an automatically randomised deck, here is my code below i dont know why im getting these errors but here they are Warning: array_pop() [function.array-pop]: The argument should be an array in H:xampphtdocsphpcmspagespairs.php on line 70 Warning: array_push() [function.array-push]: First argument should be an array in H:xampphtdocsphpcmspagespairs.php on line 73 just those two errors repeatedley as it is from a function and is looped through many times <?php echo('<b><u>Pairs Game Calculator:</u></b><br><br>'); ?> <div style="margin: 5px 20px 20px;"> <div style="margin-bottom: 2px;"><b>Randomised Deck:</b> <input value="Hide" onclick="if (this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display != '') { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = ''; this.innerText = ''; this.value = 'Hide'; }else{ this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = 'none'; this.innerText = ''; this.value = 'Show'; }" type="button"> </div> <div style="border: 1px inset ; margin: 0px; padding: 6px;"> <div style=""> <?php include_once('../includes/DBConnector.php'); $connector = new DBConnector(); $result = $connector->query("SELECT * FROM cards"); $cards = Array(); $card = 0; while($row = $connector->GetArray($result)) { $cards[$card][1] = $row['Card_ID']; $cards[$card][2] = $row['Name']; $cards[$card][3] = $row['Suit']; $card = $card + 1; } echo('<table border="1" cellpadding="2"><tr><td>Card No:</td><td>ID No:</td><td>Card Name:</td><td>Card Suit:</td></tr>'); shuffle($cards); for($i=0; $i<=51; $i++) { $num = $i + 1; echo("<tr><td>" . $num . "</td><td>" . $cards[$i]['1'] . "</td><td>" . $cards[$i]['2'] . "</td><td>" . $cards[$i]['3'] . "</td></tr>"); } echo('</table>'); ?> </div> </div> </div> <?php $table = Array(); for($i=1;$i<=8;$i++) { $table[$i] = Array(); } function Place_Card($position = '') { $card_placed = false; $card = array_pop($cards); if($position != '') { array_push($table[$position], $card); return true; }else{ for($i=1;$i<=8;$i++) { if(count($table[$i]) == 0) { array_push($table[$i], $card); return true; }elseif($i =={ return false; } } } } function Check_Cards() { $card_placed = false; for($i=1;$i<=8;$i++) { for($j=1;$j<=8;$j++) { if($table[$i]['Name'] == $table[$j]['Name']) { Place_Card($i); Place_Card($j); $card_placed = true; } if($i == 8 && $j == 8 && $card_placed == false) { $place_card = Place_Card(); if($place_card == true) { $card_placed = true; }else{ $lost = true; } } } } } function Pickup_Cards() { $cards = array_merge_recursive($cards, $table[1], $table[2], $table[3], $table[4], $table[5], $table[6], $table[7], $table[8]); $lost = false; } $lost = false; $won = false; $games_played = 0; $hands_played = 0; $losses = 0; while($won == false) { if(count($cards) > 0) { $games_played = $games_played + 1; if($lost == false) { Check_Cards(); $hands_played = $hands_played + 1; }else{ Pickup_Cards(); $losses = $losses + 1; } }else{ $won = true; echo('Game Won<br><br>' . 'Games Played: ' . $games_played . '<br>Hands Played: ' . $hands_played . '<br>Losses: ' . $losses); } } ?> </div> here is the page if you would like to visit it for reference http://gaming-network.ath.cx/phpcms/index.php?page=6 Link to comment https://forums.phpfreaks.com/topic/150981-solved-php-array_pop-and-array_push-passing-an-array-but-says-im-not/ Share on other sites More sharing options...
trq Posted March 24, 2009 Share Posted March 24, 2009 You need to read up about variable scope in relation to functions. $cards does not exist within your Place_Card() function. Link to comment https://forums.phpfreaks.com/topic/150981-solved-php-array_pop-and-array_push-passing-an-array-but-says-im-not/#findComment-793205 Share on other sites More sharing options...
FaT3oYCG Posted March 25, 2009 Author Share Posted March 25, 2009 yeah i was thinking that but if i make cards a global it still doesnt seem to pick it up what is the correct way to do something like that? Link to comment https://forums.phpfreaks.com/topic/150981-solved-php-array_pop-and-array_push-passing-an-array-but-says-im-not/#findComment-793217 Share on other sites More sharing options...
trq Posted March 25, 2009 Share Posted March 25, 2009 Pass the argument into the function. eg; function foo($arg) { echo $arg; } $a = 'hello'; foo($a); Link to comment https://forums.phpfreaks.com/topic/150981-solved-php-array_pop-and-array_push-passing-an-array-but-says-im-not/#findComment-793223 Share on other sites More sharing options...
trq Posted March 25, 2009 Share Posted March 25, 2009 $table also does not exist. Read this functions. Link to comment https://forums.phpfreaks.com/topic/150981-solved-php-array_pop-and-array_push-passing-an-array-but-says-im-not/#findComment-793226 Share on other sites More sharing options...
FaT3oYCG Posted March 25, 2009 Author Share Posted March 25, 2009 nvm lol, i just wasnt using global in the right way, it all works now, i just need to fix my code as the output i get is not the expected output thanks anyway Link to comment https://forums.phpfreaks.com/topic/150981-solved-php-array_pop-and-array_push-passing-an-array-but-says-im-not/#findComment-793228 Share on other sites More sharing options...
trq Posted March 25, 2009 Share Posted March 25, 2009 globals are bad, it is much cleaner to pass your arguments to your functions. It also makes your functions reusable and easier to read. Link to comment https://forums.phpfreaks.com/topic/150981-solved-php-array_pop-and-array_push-passing-an-array-but-says-im-not/#findComment-793237 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.