Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,161,446 members, 7,846,851 topics. Date: Saturday, 01 June 2024 at 03:58 AM

How Can I Create A Listed Link Using Foreachloop In Php? - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / How Can I Create A Listed Link Using Foreachloop In Php? (1639 Views)

Online Banking Source Code In PHP, MSQL / How Can I Build A Twitter like @mention System In PHP? / How Can I Create A Virus Or Malware? (2) (3) (4)

(1) (Reply) (Go Down)

How Can I Create A Listed Link Using Foreachloop In Php? by Slim1978(m): 12:57pm On Jul 23, 2015
Hey guys, i am trying to create a listed link
using foreach loope where by a user can click
in any of the links and it will take them to
approriate page.

this is wat I mean

Home
About
Shirts
Trousers
Contact

I tried using the following codes but it
echos out a black result in my browser, please
is there something I am doing wrong?
I need assistance

view code

<ul class="menu_sidebar">
<h4 >Menu</h4>
<?php

$menus = array();
$menu[001] = array(
"menu" => "Home",
"link" => "wears/home.php"
);

$menu[002] = array(
"menu" => "About us",
"link" => "wears/about_us.php"
);

$menu[003] = array(
"menu" => "Shirts",
"link" => "wears/shirts.php"
);

$menu[004] = array(
"menu" => "Trousers",
"link" => "wears/trousers.php"
);

$menu[005] = array(
"menu" => "Contact",
"link" => "wears/contact.php"
);


?>

<?php foreach($menus as $menu) {
echo "<li>";
echo '<a href="' . $menu["link"] . '">';
echo $menu["menu"];
echo "</a>";
echo "</li>";

}


?>



</ul>
Re: How Can I Create A Listed Link Using Foreachloop In Php? by thewebcraft(m): 1:06pm On Jul 23, 2015
Slim1978:

<?php foreach($menus as $menu) {
echo "<li>";
echo '<a href="' . $menu["link"] . '">';
echo $menu["menu"];
echo "</a>";
echo "</li>";

}


?>

Change foreach($menus as $menu) to foreach($menu as $menu) it works
Re: How Can I Create A Listed Link Using Foreachloop In Php? by babatope88(m): 1:14pm On Jul 23, 2015
You can access the internal array like this $menu[001]['link'], what you have in ur code is an array inside array. Let's say two level deep. Try this <?php foreach($menus as $menu) {
echo "<li>";
echo '<a href="' . $menu[001]["link"] . '">';
echo $menu[001]["menu"];
echo "</a>";
Re: How Can I Create A Listed Link Using Foreachloop In Php? by Slim1978(m): 1:18pm On Jul 23, 2015
thewebcraft:

Change foreach($menus as $menu) to foreach($menu as $menu)

Thanks for helping it wont work dat way because menus is the variable for the array while menu is the place holder variable.
I hv just debugged d issue now, d array variables with key has no "S" as in menu for menus.
Re: How Can I Create A Listed Link Using Foreachloop In Php? by Slim1978(m): 1:24pm On Jul 23, 2015
babatope88:
You can access the internal array like this $menu[001]['link'], what you have in ur code is an array inside array. Let's say two level deep. Try this <?php foreach($menus as $menu) {
echo "<li>";
echo '<a href="' . $menu[001]["link"] . '">';
echo $menu[001]["menu"];
echo "</a>";

Thanks for helping my broda, I made a mistake for not putting "S" in the variable dat contains the array. it is suppose to be
menus[109] and not menu[109].
Re: How Can I Create A Listed Link Using Foreachloop In Php? by Slim1978(m): 1:30pm On Jul 23, 2015
thewebcraft:

Change foreach($menus as $menu) to foreach($menu as $menu) it works
Oops broda ur method also worked Ive learnt something too, thanks bro.
Re: How Can I Create A Listed Link Using Foreachloop In Php? by thewebcraft(m): 9:29pm On Jul 23, 2015
Slim1978:


Oops broda ur method also worked Ive learnt something too, thanks bro.
Ok You're Welcome
Re: How Can I Create A Listed Link Using Foreachloop In Php? by Slim1978(m): 7:34am On Jul 24, 2015
thewebcraft:

Ok You're Welcome
Bro, I am still having issues, d listed items are displayed but d cant link to d various pages?
d only option I have is to write this code for each page, and assigning an ID to d link for each page which I think is not Ideal.

<?php foreach($menus as $menu) {
echo "<li>";
echo '<a href="' . $menu["link"] . '">';
echo $menu["menu"];
echo "</a>";
echo "</li>";

}


?>
pls any thought.
Re: How Can I Create A Listed Link Using Foreachloop In Php? by Sammyskills(m): 9:01am On Jul 24, 2015
Based on how you assigned your variables, the best option for you will be:

foreach $menu as $menu
.

I've tried it in my browser and it is working (including the links). If you try it and it doesn't work, try clearing your browser cache and cookies.

But if you still insist that you want to use

foreach $menus as $menu
, then you'll have to add 's' to the $menus array (all of them), i.e.

$menus = array();
$menus[001] = array(
"menu" => "Home",
"link" => "wears/home.php"
);

$menus[002] = array(
"menu" => "About us",
"link" => "wears/about_us.php"
);

$menus[003] = array(
"menu" => "Shirts",
"link" => "wears/shirts.php"
);

$menus[004] = array(
"menu" => "Trousers",
"link" => "wears/trousers.php"
);

$menus[005] = array(
"menu" => "Contact",
"link" => "wears/contact.php"
);


Hope that helps wink
Re: How Can I Create A Listed Link Using Foreachloop In Php? by Slim1978(m): 10:02am On Jul 24, 2015
Sammyskills:
Based on how you assigned your variables, the best option for you will be:

foreach $menu as $menu
.

I've tried it in my browser and it is working (including the links). If you try it and it doesn't work, try clearing your browser cache and cookies.

But if you still insist that you want to use

foreach $menus as $menu
, then you'll have to add 's' to the $menus array (all of them), i.e.

$menus = array();
$menus[001] = array(
"menu" => "Home",
"link" => "wears/home.php"
);

$menus[002] = array(
"menu" => "About us",
"link" => "wears/about_us.php"
);

$menus[003] = array(
"menu" => "Shirts",
"link" => "wears/shirts.php"
);

$menus[004] = array(
"menu" => "Trousers",
"link" => "wears/trousers.php"
);

$menus[005] = array(
"menu" => "Contact",
"link" => "wears/contact.php"
);


Hope that helps wink

Thanks bro, de listed links are displayed but I want dem to go to dier various pages.
like I said d only option I have is to specify d ID with each of d page by repeating dis code which I think is not I deal any thought?
<?php foreach($menus as $menu) {
echo "<li>";
echo '<a href="' . $menu["link"] . '">';
echo $menu["menu"];
echo "</a>";
echo "</li>";

}


?>
Re: How Can I Create A Listed Link Using Foreachloop In Php? by Sammyskills(m): 10:19am On Jul 24, 2015
Slim1978:


Thanks bro, de listed links are displayed but I want dem to go to dier various pages.
like I said d only option I have is to specify d ID with each of d page by repeating dis code which I think is not I deal any thought?
<?php foreach($menus as $menu) {
echo "<li>";
echo '<a href="' . $menu["link"] . '">';
echo $menu["menu"];
echo "</a>";
echo "</li>";

}


?>

"You want the links to go to their various pages?" I don't quite understand what you mean. Can you attach a screenshot?
From what I can decipher, each of the links, links to the address which you specified there (wear/home.php, wears/about-us.php, etc).

Am I missing something?
Re: How Can I Create A Listed Link Using Foreachloop In Php? by dplumptre(m): 2:22pm On Jul 24, 2015
I have looked at the code and the solutions given so far, i feel it should work perfectly well, perhaps the reason you are not being redirected to the pages is because you are not using an absolute path or you have not created those pages yet,

To be sure,create all the pages and view them then compare it with the path that comes out from the foreach loop

you can create a variable for your url path like this below

or better yet make it a constant store in an includes file or something

// this is a demo path
$url_path = "http://localhost/menupage";


// you then concatenate it in the foreach loop like this below

foreach($menus as $menu) {
echo "<li>";
echo '<a href="' .$url_path.'/'.$menu["link"] . '">';
echo $menu["menu"];
echo "</a>";
echo "</li>";

}


I hope this helps you can find some more stuffs on my blog http://overallheuristic.com/blog/
Re: How Can I Create A Listed Link Using Foreachloop In Php? by Slim1978(m): 5:30pm On Jul 24, 2015
dplumptre:
I have looked at the code and the solutions given so far, i feel it should work perfectly well, perhaps the reason you are not being redirected to the pages is because you are not using an absolute path or you have not created those pages yet,

To be sure,create all the pages and view them then compare it with the path that comes out from the foreach loop

you can create a variable for your url path like this below

or better yet make it a constant store in an includes file or something

// this is a demo path
$url_path = "http://localhost/menupage";


// you then concatenate it in the foreach loop like this below

foreach($menus as $menu) {
echo "<li>";
echo '<a href="' .$url_path.'/'.$menu["link"] . '">';
echo $menu["menu"];
echo "</a>";
echo "</li>";

}


I hope this helps you can find some more stuffs on my blog http://overallheuristic.com/blog/


welldone bro, dis is great, it worked everything is in order. one last thing? am new to lagos, is dier any hackerton meetup in Lagos where people can hangout with people of d same profession??
Re: How Can I Create A Listed Link Using Foreachloop In Php? by thewebcraft(m): 8:31am On Jul 29, 2015
Slim1978:



welldone bro, dis is great, it worked everything is in order. one last thing? am new to lagos, is dier any hackerton meetup in Lagos where people can hangout with people of d same profession??
Yeah Mobile Experience Centre Co-creation Hub @Yaba (a.k.a Silicon Valley)
Re: How Can I Create A Listed Link Using Foreachloop In Php? by Sammyskills(m): 8:55am On Jul 29, 2015
thewebcraft:

Yeah Mobile Experience Centre Co-creation Hub @Yaba (a.k.a Silicon Valley)

What are the requirements for joining this "Hub"?
Re: How Can I Create A Listed Link Using Foreachloop In Php? by thewebcraft(m): 10:29am On Jul 29, 2015
Sammyskills:


What are the requirements for joining this "Hub"?
Click Here

(1) (Reply)

Need A Facial Recognition Software For Attendance Written In Python Or Java. Fo / I Wanna Be A Programmer / Crisc 2012 Review Manual

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 42
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.