jonassvensson4 Posted August 22, 2015 Share Posted August 22, 2015 Hello! I can't manage to get this to work and i have no idea what im doing wrong. It doesnt give me an error. (NOTICE: IM A BEGINNER AT PHP). Thanks /Jonas <?php class GameStruct { public $Name; public $Database; public $Game; public $NoticeLeave; public function Init($n, $db, $g, $nl) { $Name = $n; $Database = $db; $Game = $g; $NoticeLeave = $nl; } } $struct = Array(); function AddDefinition( $name, $database, $game, $notice ) { global $instance; $instance = new GameStruct; $instance->Init( $name, $database, $game, $notice ); $struct[] = $instance; } AddDefinition( "leavecsgo", "comp_user", "Csgo", "Du har nu lämnat CS:GO-turneringen!" ); AddDefinition( "leavesmite", "comp_user", "Smite", "Du har nu lämnat Smite-turneringen!" ); AddDefinition( "leavefifa", "comp_user", "Fifa", "Du har nu lämnat Fifa-turneringen!" ); AddDefinition( "leavehearthstone", "comp_user", "Hearthstone", "Du har nu lämnat Hearthstone-turneringen!" ); AddDefinition( "leavedota2", "comp_user", "Dota2", "Du har nu lämnat Dota2-turneringen!" ); for( $i = 0; $i < count( $struct ); $i++ ) { if( !isset( $_POST[ $struct->Name ] ) ) { $sql = $con->query("DELETE FROM `{$struct->Database}` WHERE Username='{$Username}' AND Game='{$struct->Game}'"); $format = 'alert("%s");'; echo '<script >'; echo sprintf( $format, $struct->NoticeLeave ); echo '</script>'; } } ?> Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted August 22, 2015 Solution Share Posted August 22, 2015 Most of your problems are to do with "variable scope". Variables used inside functions only exist within that function. so public function Init($n, $db, $g, $nl) { $Name = $n; $Database = $db; $Game = $g; $NoticeLeave = $nl; } needs to be like this to set the class properties public function Init($n, $db, $g, $nl) { $this->Name = $n; $this->Database = $db; $this->Game = $g; $this->NoticeLeave = $nl; } The same goes for your "$struct" array in the "AddDefinition()" function. Also you should avoid globals (which,BTW, should have been global $struct. A better way is for your function to return the instance then add it to the array. Finally, $struct is an array of objects and not an object so you cannot use $struct->name. Use foreach to loop through the array. class GameStruct { public $Name; public $Database; public $Game; public $NoticeLeave; public function Init($n, $db, $g, $nl) { $this->Name = $n; $this->Database = $db; $this->Game = $g; $this->NoticeLeave = $nl; } } function AddDefinition( $name, $database, $game, $notice ) { $instance = new GameStruct; $instance->Init( $name, $database, $game, $notice ); return $instance; } $struct = Array(); $struct[] = AddDefinition( "leavecsgo", "comp_user", "Csgo", "Du har nu lämnat CS:GO-turneringen!" ); $struct[] = AddDefinition( "leavesmite", "comp_user", "Smite", "Du har nu lämnat Smite-turneringen!" ); $struct[] = AddDefinition( "leavefifa", "comp_user", "Fifa", "Du har nu lämnat Fifa-turneringen!" ); $struct[] = AddDefinition( "leavehearthstone", "comp_user", "Hearthstone", "Du har nu lämnat Hearthstone-turneringen!" ); $struct[] = AddDefinition( "leavedota2", "comp_user", "Dota2", "Du har nu lämnat Dota2-turneringen!" ); foreach ($struct as $sobj) { if( !isset( $_POST[ $sobj->Name ] ) ) { $sql = $con->query("DELETE FROM `{$sobj->Database}` WHERE Username='{$Username}' AND Game='{$sobj->Game}'"); $format = 'alert("%s");'; echo '<script >'; echo sprintf( $format, $sobj->NoticeLeave ); echo '</script>'; } } Quote Link to comment Share on other sites More sharing options...
jonassvensson4 Posted August 23, 2015 Author Share Posted August 23, 2015 Thank you so much for the answer! That solved my problem Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.