Jump to content

PHP Array help


jonassvensson4
Go to solution Solved by Barand,

Recommended Posts

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>';  
    }
}
 
?>
Link to comment
Share on other sites

  • Solution

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>';  
    }
}
 
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.