Jump to content

PHP array

Joveice
Go to solution Solved by Limecat86,
2 hours ago, Joveice said:

So I'm trying to do this array but I have no clue on how it's done since I dident understand the documentation at all.

What I want to do is have an array with 4 values, the last value is going to be another array. like this Name > A name, Link > A link, Icon > Icon-name, Trigger > (1,2,3,4)

And I want to be able to store more of them in there. so I can do a foreach to get all the values out of it and make a row with it (gonna use this for a navigation bar).

 


$menu = array(
    array('name' => 'Home', 'link' => '/', 'icon' => 'fa fa-home', 'trigger' => array('Home')),
    array('name' => 'Shop', 'link' => '/shop', 'icon' => 'fa fa-shopping-cart', 'trigger' => array('Shop','Shop - Products'))
);

This is what I tryed, but I don't think it worked (or I'm doing it wrong)

 

2 hours ago, Joveice said:

That doesent work, tho I figured it out now, just how to do the foreach I have no clue of now. $menu[0]['name'] returns Home, tho how I can get the [0]value in foreach I have no idea

 

Hope this will be helpfull:

<?php

$menu = array(
    array('name' => 'Home', 'link' => '/', 'icon' => 'fa fa-home', 'trigger' => array('Home')), //$menuitem
    array('name' => 'Shop', 'link' => '/shop', 'icon' => 'fa fa-shopping-cart', 'trigger' => array('Shop','Shop - Products')) //$menuitem
);

foreach ($menu as $menuitem) {
	//First iteration
	//$menuitem['name'] = 'Home'
  	//$menuitem['link'] = '/'
	//$menuitem['trigger'][0] = 'Home'
  	//etc.
  
  	//Second iteration
  	//$menuitem['name'] = 'Shop'
  	//$menuitem['link'] = '/shop'
	//$menuitem['trigger'][0] = 'Shop'
  	//$menuitem['trigger'][1] = 'Shop - Products'
  	//etc.
  
    //or loop trough $menuitem['trigger']
  	foreach ($menuitem['trigger'] as $k => $value) {
    	//$k is the array index (0,1,2,etc)
      	//$value wil hold Home, Shop or Shop - Products
    }
}
?>

 

So I'm trying to do this array but I have no clue on how it's done since I dident understand the documentation at all.

What I want to do is have an array with 4 values, the last value is going to be another array. like this Name > A name, Link > A link, Icon > Icon-name, Trigger > (1,2,3,4)

And I want to be able to store more of them in there. so I can do a foreach to get all the values out of it and make a row with it (gonna use this for a navigation bar).

 

$menu = array(
    array('name' => 'Home', 'link' => '/', 'icon' => 'fa fa-home', 'trigger' => array('Home')),
    array('name' => 'Shop', 'link' => '/shop', 'icon' => 'fa fa-shopping-cart', 'trigger' => array('Shop','Shop - Products'))
);

This is what I tryed, but I don't think it worked (or I'm doing it wrong)

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Joveice said:

So I'm trying to do this array but I have no clue on how it's done since I dident understand the documentation at all.

What I want to do is have an array with 4 values, the last value is going to be another array. like this Name > A name, Link > A link, Icon > Icon-name, Trigger > (1,2,3,4)

And I want to be able to store more of them in there. so I can do a foreach to get all the values out of it and make a row with it (gonna use this for a navigation bar).

 


$menu = array(
    array('name' => 'Home', 'link' => '/', 'icon' => 'fa fa-home', 'trigger' => array('Home')),
    array('name' => 'Shop', 'link' => '/shop', 'icon' => 'fa fa-shopping-cart', 'trigger' => array('Shop','Shop - Products'))
);

This is what I tryed, but I don't think it worked (or I'm doing it wrong)

how to did you try get the values?

 

$menu['name'];

$menu['link'];

 

it should work on my phone on a bus just now so can't double check for you.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, vorticalbox said:

how to did you try get the values?

 

$menu['name'];

$menu['link'];

 

it should work on my phone on a bus just now so can't double check for you.

That doesent work, tho I figured it out now, just how to do the foreach I have no clue of now. $menu[0]['name'] returns Home, tho how I can get the [0]value in foreach I have no idea

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, Joveice said:

That doesent work, tho I figured it out now, just how to do the foreach I have no clue of now. $menu[0]['name'] returns Home, tho how I can get the [0]value in foreach I have no idea

for $I = 0; I <count($menu);i++

 

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, vorticalbox said:

for $I = 0; I <count($menu);i++

 

 

Um, what is this ? :P

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

29 minutes ago, Joveice said:

(gonna use this for a navigation bar).

.....

This is what I tryed, but I don't think it worked (or I'm doing it wrong)

why are you making a multidimensional array for an menu bar, whats the problem with 

<?php
	include "menubar.php";
?>

?

 

if you really want to do this method, you can take a look at http://www.w3schools.com/php/php_arrays_multi.asp

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, ard1998 said:

why are you making a multidimensional array for an menu bar, whats the problem with 


<?php
	include "menubar.php";
?>

?

 

if you really want to do this method, you can take a look at http://www.w3schools.com/php/php_arrays_multi.asp

Thank you for that link, will look into this. I want to do this so I can make it easyer to make the navbar have active states without using java or some messy php code it will take time to add to later.

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

personally, i like to make an top.php and an footer.php like this

 

top.php

<?php session_start(); ?>
<!DOCTYPE html>
<html>
<head>
	<title></title>
	<link rel="stylesheet" type="text/css" href="main.css">
	<script type="text/javascript" src="main.js"></script>
</head>
<body>
	<div id="top">
		<div id="menu">
			<a href="index.php">HOME</a>
			<a href="guestbook.php">GUEST BOOK</a>
          	<?php //check if user is logged in
          		if(isset($_SESSION["username"]){
                  echo "<a href="forum.php">OUR SECRET FORUM</a>";
                } else {
                  echo "<a href="login.php">LOGIN</a>";
                }
          	?>
		</div>
	</div>

 

index.php

<?php include "top.php" ?>

content here

<?php include "footer.html" ?>

 

footer.html

	<div id="bottom">
		<h2>Footer</h2>
	</div>

</body></html>

 

i just seperate one php file into multiple files, so i can just copy the top and the bottom of the page. one change is easy done and can directly been seen on all pages this is included in. and the menubar is dynamic.

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Joveice said:

Um, what is this ? :P

it is a poorly wrote for loop LOL. I'm stuck ona phone just now anyway to get the length of an array you use count($array_name);

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Joveice said:

So I'm trying to do this array but I have no clue on how it's done since I dident understand the documentation at all.

What I want to do is have an array with 4 values, the last value is going to be another array. like this Name > A name, Link > A link, Icon > Icon-name, Trigger > (1,2,3,4)

And I want to be able to store more of them in there. so I can do a foreach to get all the values out of it and make a row with it (gonna use this for a navigation bar).

 


$menu = array(
    array('name' => 'Home', 'link' => '/', 'icon' => 'fa fa-home', 'trigger' => array('Home')),
    array('name' => 'Shop', 'link' => '/shop', 'icon' => 'fa fa-shopping-cart', 'trigger' => array('Shop','Shop - Products'))
);

This is what I tryed, but I don't think it worked (or I'm doing it wrong)

 

2 hours ago, Joveice said:

That doesent work, tho I figured it out now, just how to do the foreach I have no clue of now. $menu[0]['name'] returns Home, tho how I can get the [0]value in foreach I have no idea

 

Hope this will be helpfull:

<?php

$menu = array(
    array('name' => 'Home', 'link' => '/', 'icon' => 'fa fa-home', 'trigger' => array('Home')), //$menuitem
    array('name' => 'Shop', 'link' => '/shop', 'icon' => 'fa fa-shopping-cart', 'trigger' => array('Shop','Shop - Products')) //$menuitem
);

foreach ($menu as $menuitem) {
	//First iteration
	//$menuitem['name'] = 'Home'
  	//$menuitem['link'] = '/'
	//$menuitem['trigger'][0] = 'Home'
  	//etc.
  
  	//Second iteration
  	//$menuitem['name'] = 'Shop'
  	//$menuitem['link'] = '/shop'
	//$menuitem['trigger'][0] = 'Shop'
  	//$menuitem['trigger'][1] = 'Shop - Products'
  	//etc.
  
    //or loop trough $menuitem['trigger']
  	foreach ($menuitem['trigger'] as $k => $value) {
    	//$k is the array index (0,1,2,etc)
      	//$value wil hold Home, Shop or Shop - Products
    }
}
?>

 

CPU: i7-12700KF Grill Plate Edition // MOBO: Asus Z690-PLUS WIFI D4 // RAM: 16GB G.Skill Trident Z 3200MHz CL14 

GPU: MSI GTX 1080 FE // PSU: Corsair RM750i // CASE: Thermaltake Core X71 // BOOT: Samsung Evo 960 500GB

STORAGE: WD PC SN530 512GB + Samsung Evo 860 500GB // COOLING: Full custom loop // DISPLAY: LG 34UC89G-B

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Limecat86 said:

 

 

Hope this will be helpfull:


<?php

$menu = array(
    array('name' => 'Home', 'link' => '/', 'icon' => 'fa fa-home', 'trigger' => array('Home')), //$menuitem
    array('name' => 'Shop', 'link' => '/shop', 'icon' => 'fa fa-shopping-cart', 'trigger' => array('Shop','Shop - Products')) //$menuitem
);

foreach ($menu as $menuitem) {
	//First iteration
	//$menuitem['name'] = 'Home'
  	//$menuitem['link'] = '/'
	//$menuitem['trigger'][0] = 'Home'
  	//etc.
  
  	//Second iteration
  	//$menuitem['name'] = 'Shop'
  	//$menuitem['link'] = '/shop'
	//$menuitem['trigger'][0] = 'Shop'
  	//$menuitem['trigger'][1] = 'Shop - Products'
  	//etc.
  
    //or loop trough $menuitem['trigger']
  	foreach ($menuitem['trigger'] as $k => $value) {
    	//$k is the array index (0,1,2,etc)
      	//$value wil hold Home, Shop or Shop - Products
    }
}
?>

 

Thanks alot man :) makes it alot easyer to create a long navbar with active states

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, ard1998 said:

personally, i like to make an top.php and an footer.php like this

 

top.php


<?php session_start(); ?>
<!DOCTYPE html>
<html>
<head>
	<title></title>
	<link rel="stylesheet" type="text/css" href="main.css">
	<script type="text/javascript" src="main.js"></script>
</head>
<body>
	<div id="top">
		<div id="menu">
			<a href="index.php">HOME</a>
			<a href="guestbook.php">GUEST BOOK</a>
          	<?php //check if user is logged in
          		if(isset($_SESSION["username"]){
                  echo "<a href="forum.php">OUR SECRET FORUM</a>";
                } else {
                  echo "<a href="login.php">LOGIN</a>";
                }
          	?>
		</div>
	</div>

 

index.php


<?php include "top.php" ?>

content here

<?php include "footer.html" ?>

 

footer.html


	<div id="bottom">
		<h2>Footer</h2>
	</div>

</body></html>

 

i just seperate one php file into multiple files, so i can just copy the top and the bottom of the page. one change is easy done and can directly been seen on all pages this is included in. and the menubar is dynamic.

Well yea, I have footer.php, header.php thefile.php scripts.php.

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×