Jump to content

Trouble with a Function: Need Help Displaying Corresponding Second Buttons and Parsing Data to row.php


jbezza123

Recommended Posts

I have a PHP function named gameMenu which generates a menu with buttons in a game application. I want to modify it so that if the second_button property is set to true, a corresponding second button appears next to that button, then parse that data to the row.php where it should place the 1st in the list on the first 2nd on the 2nd ect... I've kind of got it working except all buttons have a second button and they all have 1 of each in the list, also some of it may be a mess its been bugging me for a whole day! i attached 3 screens 1st is just normal i guess 2nd and 3rd is when i hover, to show that they have stacked up!, any help is appreciated!

 

//page.php
  /**
 * gameMenu
 *
 * @return string
 */
private function gameMenu()
{
    $lang = $this->langs->loadLang('game/menu', true);

    $menu_block1 = '';
    $menu_block2 = '';
    $menu_block3 = '';
    $modules_array = explode(';', Functions::readConfig('modules'));
    $tota_rank = $this->current_user['user_statistic_total_rank'] == '' ?
    $this->current_planet['stats_users'] : $this->current_user['user_statistic_total_rank'];
    $pages = [/*main buttons column*/
        ['overview', $lang->line('lm_overview'), '', 'FFF', '', '1', '1', 'second_button' => true, 'button_enabled' => true],
        ['empire', $lang->line('lm_empire'), '', 'FFF', '', '1', '2', 'second_button' => false, 'button_enabled' => false],
        ['resources', $lang->line('lm_resources'), '', 'FFF', '', '1', '3', 'second_button' => true, 'button_enabled' => true],
        ['resourceSettings', $lang->line('lm_resources_settings'), '', 'FFF', '', '1', '4', 'second_button' => false, 'button_enabled' => false],
        ['station', $lang->line('lm_station'), '', 'FFF', '', '1', '5', 'second_button' => false, 'button_enabled' => true],
        ['traderOverview', $lang->line('lm_trader'), '', 'FF8900', '', '1', '6', 'second_button' => false, 'button_enabled' => true],
        ['research', $lang->line('lm_research'), '', 'FFF', '', '1', '7', 'second_button' => true, 'button_enabled' => true],
        ['techtree', $lang->line('lm_technology'), '', 'FFF', '', '1', '8', 'second_button' => false, 'button_enabled' => false],
        ['shipyard', $lang->line('lm_shipyard'), '', 'FFF', '', '1', '9', 'second_button' => false, 'button_enabled' => true],
        ['defense', $lang->line('lm_defenses'), '', 'FFF', '', '1', '10', 'second_button' => false, 'button_enabled' => true],
        ['fleet1', $lang->line('lm_fleet'), '', 'FFF', '', '1', '11', 'second_button' => false, 'button_enabled' => true],
        ['movement', $lang->line('lm_movement'), '', 'FFF', '', '1', '12', 'second_button' => false, 'button_enabled' => false],
        ['galaxy', $lang->line('lm_galaxy'), 'mode=0', 'FFF', '', '1', '13', 'second_button' => false, 'button_enabled' => true],
        ['alliance', $lang->line('lm_alliance'), '', 'FFF', '', '1', '14', 'second_button' => false, 'button_enabled' => true],
        ['officier', $lang->line('lm_officiers'), '', 'FF8900', '', '1', '15', 'second_button' => false, 'button_enabled' => true],
        ['messages', $lang->line('lm_messages'), '', 'FFF', '', '1', '16', 'second_button' => false, 'button_enabled' => true],
        ['statistics', $lang->line('lm_statistics'), 'range=' . $tota_rank, 'FFF', '', '2', '17', 'second_button' => false, 'button_enabled' => true],
        ['notes', $lang->line('lm_notes'), '', 'FFF', 'true', '2', '18', 'second_button' => false, 'button_enabled' => true],
        ['buddies', $lang->line('lm_buddylist'), '', 'FFF', '', '2', '19', 'second_button' => false, 'button_enabled' => true],
        ['search', $lang->line('lm_search'), '', 'FFF', '', '2', '20', 'second_button' => false, 'button_enabled' => true],
        ['preferences', $lang->line('lm_options'), '', 'FFF', '', '2', '21', 'second_button' => false, 'button_enabled' => true],
        ['logout', $lang->line('lm_logout'), '', 'FFF', '', '2', '22', 'second_button' => false, 'button_enabled' => true],
        ['forums', $lang->line('lm_forums'), '', 'FFF', '', '2', '23', 'second_button' => false, 'button_enabled' => false],
    ];

    $second_button_pages = [/*if second_button is true this appears to the right of that button*/
        ['empire', $lang->line('lm_empire'), '', 'FFF', '', '1', '1'],/*overview settings*/
        ['resourceSettings', $lang->line('lm_resources_settings'), '', 'FFF', '', '1', '3'],/*resource settings*/
        ['techtree', $lang->line('lm_technology'), '', 'FFF', '', '1', '7'],/*research settings*/
        ['movement', $lang->line('lm_movement'), '', 'FFF', '', '1', '11'],/*fleet1 movement*/
    ];
	$snd = [
    0 => ['game.php?page=empire'],
    1 => ['game.php?page=resourceSettings'],
    2 => ['game.php?page=techtree'],
    3 => ['game.php?page=movement'],
];

	$parse['snd'] = $snd;
	//$parse['second_button_pages'] = $second_button_pages;
    $count = ('0');
	// BUILD THE MENU
    foreach ($pages as $key => $data) {
        // IF THE MODULE IT'S NOT ENABLED, CONTINUE!
        if (isset($modules_array[$data[6]]) && $modules_array[$data[6]] == 0 && $modules_array[$data[6]] != '') {
            continue;
        }
		if ($data[6] == $second_button_pages[$count][0] && $count <= 4){
		if ($count == $count){
			$parse['snd[$count]'] = $snd[$count];
		}
		$count += 1;
		}
		
		
        // BUILD URL
        if ($data[2] != '') {
            $link = 'game.php?page=' . $data[0] . '&' . $data[2];
        } else {
            $link = 'game.php?page=' . $data[0];
        }

        // POP UP OR NOT
        if ($data[4] == 'true') {
            $link_type = '<a href="#" onClick="f(\'' . $link . '\', \'' . $data[1] . '\')">
                <font color="' . (($data[3] != 'FFF') ? $data[3] : '') . '">' . $data[1] . '</font></a>';
        } else {
            $link_type = '<a href="' . $link . '">
                <font color="' . (($data[3] != 'FFF') ? $data[3] : '') . '">' . $data[1] . '</font></a>';
        }
		
	
        // MENU BUTTON & SECOND BUTTON IF SECOND BUTTON IS ENABLED
        //var_dump($data['button_enabled']); // Add this line to check the value
        //$parse['second_button'] = isset($data['second_button']) && $data['second_button'];/*this is the second_button thats a square and to the right of the main button*/
        $parse['main_button'] = isset($data['button_enabled']) && $data['button_enabled'];



        // COLOR AND URL
        $parse['color'] = $data[3];
        $parse['menu_link'] = $link_type;
		
		

        // ONLY FOR THE CHANGELOG
        if ($data[5] == 0) {
            $parse['changelog'] = '(' . $link_type . ')';
        }

        // MENU BLOCK [1 - 2 - 3]
        switch ($data[5]) {
            case '1':
                $menu_block1 .= $this->template->set(
                    'general/left_menu_row_view',
                    $parse
                );

                break;

            case '2':
                $menu_block2 .= $this->template->set(
                    'general/left_menu_row_view',
                    $parse
                );

                break;

            case '3':
                $menu_block3 .= $this->template->set(
                    'general/left_menu_row_view',
                    $parse
                );

                break;
        }
    }

    // PARSE THE MENU AND OTHER DATA
    $parse['dpath'] = DPATH;
    $parse['lm_players'] = $lang->line('lm_players');
    $parse['user_name'] = UrlHelper::setUrl('game.php?page=preferences', $this->current_user['user_name']);
    $parse['menu_block1'] = $menu_block1;
    $parse['menu_block2'] = $menu_block2;
    $parse['menu_block3'] = $menu_block3;
    $parse['admin_link'] = (($this->current_user['user_authlevel'] > 0) ?
        '<tr><td><div align="center"><a href="admin.php" target="_blank">
        <font color="lime">' . $lang->line('lm_administration') . '</font></a></div></td></tr>' : '');
    $parse['servername'] = Functions::readConfig('game_name');
    $parse['changelog'] = UrlHelper::setUrl('game.php?page=changelog', SYSTEM_VERSION);
    $parse['version'] = SYSTEM_VERSION;
    $parse['year'] = $this->current_year;

    return $this->template->set(
        'general/left_menu_view',
        $parse
    );
}
//left_row.php
<body>
    <table>
        @if ($main_button)
        <tr>
            <td style="display: flex;flex-direction: row;flex-wrap: nowrap; justify-content: center;">
    <div class="holo-container" style="background-image: url('public/upload/skins/xgproyect/menu/holomen.png'); background-size: contain;">
        <span>
            {!! $menu_link !!}
        </span>
    </div>

    <div>@if ($main_button)
        @if ($snd)
    @foreach ($snd as $button)
        @if ($button)
            <a href="{{ $button[0] }}" style="color: #fff">
                <button class="holo-button" style="right: 25px; position: absolute; background-image: url('public/upload/skins/xgproyect/menu/sbutton.png'); background-size: contain; width: 32px; height: 32px; background-color: #fff0; border: none;">
                    <img src="public/upload/skins/xgproyect/menu/cog.svg" alt="Cog Icon" style="width: 100%; height: 100%; filter: invert(100%);">
                </button>
            </a>
        @endif
    @endforeach
@endif
@endif
    </div>
</td>

        </tr>
		@endif
    </table>
</body>

 

New Project.png

Link to comment
Share on other sites

I'm not really sure what you're after, and your code is not very easy to understand.  Your screenshots all seem basically identical to me so that doesn't help clear things up either.  Is the goal to only show the "settings button" next to specific items?

I'd suggest you start with just trying to clean up your code to make it easier to understand and improve the formatting.  Your pages array for example should be using all named keys rather than a bunch of numeric ones and a couple named ones.  That way it'll be much clearer what all the settings are for each item.  A more advanced improvement may be to replace the array's with an object with properties, but just adding named keys would be good enough for now.  Something like:

$pages = [
    [
        'name?'=>'overview',
        'label?'=>$lang->line('lm_overview'),
        '???1'=>'',
        '???2'=>'FFF',
        '???3'=>'',
        '???4'=>'1',
        '???5'=>'1',
        'second_button' => true,
        'button_enabled' => true
    ],
    ...
];

$second_button_pages = [
    [
        'name?'=>'empire',
        'label?'=>$lang->line('lm_empire'),
        '???1'=>'',
        '???2'=>'FFF',
        '???3'=>'',
        '???4'=>'1',
        '???5'=>'1'
    ],
    ...
];

I labeled them all with a ? because I honestly have no idea what those elements are for.  Giving them a descriptive key name would solve that and make your code much easier to read and understand.  Think about when you come back to this in 9 months and need to add a new menu item, the way it is now you'll have no idea what data to put in the array or where.

In your template file, the indentation could be improved.  Make sure the content that is within a conditional is indented to be within that conditional, right now it's kind of all over the place.  You also seem to have unnecessary conditions, possibly related to the poor indentation.

Notice you are checking if $main_button is true when you're in a branch that has already determined that it's true.  That check is unnecessary.  

Your check of $snd could be unnecessary.  If $snd is an empty array, the foreach loop will do nothing and thus not output anything, so there is no need to check that it's not empty before hand.

Checking if $button is true is also probably unnecessary, as you probably just shouldn't be putting any empty values into your $snd array in the first place.  If you take all that out, your template code is much simpler.  You should also be moving your style="" stuff into classes and just apply the class to simplify the template even more.

<table>
@if ($main_button)
    <tr>
        <td style="display: flex;flex-direction: row;flex-wrap: nowrap; justify-content: center;">
            <div class="holo-container" style="background-image: url('public/upload/skins/xgproyect/menu/holomen.png'); background-size: contain;">
                <span>
                    {!! $menu_link !!}
                </span>
            </div>

            <div>
                @foreach ($snd as $button)
                    <a href="{{ $button[0] }}" style="color: #fff">
                        <button class="holo-button" style="right: 25px; position: absolute; background-image: url('public/upload/skins/xgproyect/menu/sbutton.png'); background-size: contain; width: 32px; height: 32px; background-color: #fff0; border: none;">
                            <img src="public/upload/skins/xgproyect/menu/cog.svg" alt="Cog Icon" style="width: 100%; height: 100%; filter: invert(100%);">
                        </button>
                    </a>
                @endforeach
            </div>
        </td>
    </tr>
@endif
</table>

You also probably shouldn't be using <table> for this, and you shouldn't be using <font> either for your links.  For your list of menu items you'd probably want <nav> containing a <ul> and a <li> per button.  For the button text, put a css class on the span tag that controls the color.

Take a pass at cleaning things up, then try posting again to explain what you want and better screenshots of what you want to see vs what you are seeing if you still have issues.

 

Link to comment
Share on other sites

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.