I was trying to get the same result in two places with this code by including it with include(""); but after the first include goes right the second one gives me the last element of the array I made, the array is this one:
<?php
$aMenu = array(
array(
"title" => "Home",
"page" => "home",
),
array(
"title" => "Over mijzelf",
"page" => "mijzelf",
),
array(
"title" => "PC Games",
"page" => "games",
),
array(
"title" => "Video's maken",
"page" => "videos",
),
array(
"title" => "Basketball",
"page" => "basketball",
),
array(
"title" => "Fitness",
"page" => "fitness"
),
array(
"title" => "Toekomst",
"page" => "toekomst"
),
);
I call it here
foreach($aMenu as $aMenu) {
$sClass = '';
if ($aMenu["page"] == $_GET['page']) {
$sClass = 'class = "active" ';
}
/*echo $aMenu["title"];
echo $aMenu["page"];*/
echo '
<ul class=" nav nav-pills nav-stacked">
<li class="'.$sClass.'" role="presentation" >'.$aMenu["title"].'</li>
</ul>';
}
You overwrite your array while iterating:
foreach($aMenu as $aMenu)
Write something like this
foreach($aMenu as $entry)
so your iteration variable won't overwrite the array itself.
Related
I created this code, but obviously it's not working. I feel there is a mistake somewhere in the logic but I don't see it.
`<?php
$languages = array(
"en" => array("name" => "English", "selector" => "/en/", "image" => "en.png"),
"fr" => array("name" => "French", "selector" => "/fr/", "image" => "fr.png"),
"de" => array("name" => "German", "selector" => "/de/", "image" => "de.png"),
"it" => array("name" => "Italian", "selector" => "/it/", "image" => "it.png"),
"si" => array("name" => "Slovene", "selector" => "/si/", "image" => "si.png"),
"es" => array("name" => "Spain", "selector" => "/es/", "image" => "es.png")
);
$current_url = $_SERVER['REQUEST_URI'];
$current_language = "";
foreach ($languages as $code => $language) {
if (strpos($current_url, $language['selector']) === 0) {
$current_language = $code;
break;
}
}
?>
<div id="language-menu">
<ul>
<?php
foreach($languages as $code => $language){
$new_url = str_replace($languages[$current_language]['selector'], $language['selector'], $current_url);
if($code === $current_language){
echo '<li class="active">'.$language['name'].'</li>';
} else {
echo '<li>'.$language['name'].'</li>';
}
}
?>
</ul>
</div>`
I expect to have a browsing between different version of the same page, where I don't know the url and the name of the page itself
I have a problem which I cant figure out. I have an array $navItems. In there I have multiple arrays with the items.
$navItems = array(
array(
"url" => 'index.php',
"title" => 'Home',
"id" => 1
),
array(
"url" => "pages/projects.php",
"title" => "Projects",
"id" => 2
),
array(
"url" => "pages/about.php",
"title" => "About",
"id" => 3
),
array(
"url" => "pages/contact.php",
"title" => "Contact",
"id" => 4
),
array(
"url" => "pages/login.php",
"title" => "Login",
"id" => 5
),
);
// Database conectie en query
$dbh = getDbConnection();
$sql = "SELECT * FROM pages";
$pages = $dbh->query($sql);
// Navitems
$navItems = array();
foreach($pages as $singe_page){
$page_title = $singe_page['title'];
$page_url =$singe_page['url'];
$page_id = $singe_page['id'];
$pagina = array(
'title' => $page_title,
'url' => $page_url,
'id' => $page_id
);
array_push($navItems, $pagina);
}
//This is the code on my array.php page. And it loads that in on the head.php
But now I want to add all the page arrays with an foreach loop. Because I have the pages saved in my database. And now I'm trying to pull the pages out my database and put them in the array.
But I just don't know anymore...
I just want for every record in my database it to throw that in an array and that array in the $navItems array.
Nevermind. When I was typing this I changed a little bit of code. And now it works. I added the array_push($navItems, $pagina) in the foreach loop. And it works.
I want to turn the following array into a nested list in html.
$aMenu = array(
array("name" => "Page 1",
"url" => "http://www.microsoft.se",
"subpages" => array(
array("name" => "Subpage 1.1", "url" => "http://www.reddit.com"),
array("name" => "Subpage 1.2", "url" => "http://www.google.se"),
array("name" => "Subpage 1.3", "url" => "http://www.cnn.com"),
)
),
array("name" => "Page 2", "url" => "http://www.facebook.com"),
array("name" => "Page 3",
"url" => "http://www.bbc.co.uk",
"subpages" => array(
array("name" => "Subpage 3.1", "url" => "http://www.jamesedition.com"),
array("name" => "Subpage 3.2", "url" => "http://www.huffpost.com"),
array("name" => "Subpage 3.3",
"url" => "http://www.lagunitas.com",
"subpages" => array(
array("name" => "Subpage 3.3.1", "url" => "http://www.burton.com"),
array("name" => "Subpage 3.3.2", "url" => "http://www.hm.com"),
array("name" => "Subpage 3.3.3", "url" => "http://www.apple.com"),
),
),
),
),
array("name" => "Page 4", "url" => "http://www.instagram.com"),
array("name" => "Page 5", "url" => "http://www.flickr.com"),
);
Desired output:
Page 1
Subpage 1.1
Subpage 1.2
Subpage 1.3
Page 2
Page 3
Subpage 3.1
Subpage 3.2
Subpage 3.3
Subpage 3.3.1
Subpage 3.3.2
Subpage 3.3.3
Page 4
Page 5
I know how to do this manually and I know how to loop through single levels of the array but I cannot figure out how to loop through it all and output the results into nested lists. I have seen separate solutions to these problems but I can't seem to make them work together. Any help would be appreciated.
PHP speudo-code:
function list2html($list)
{
$html = '';
foreach ($list as $item)
{
$html .= '<li>' . $item['name'];
if ( !empty($item['subpages']) ) // recurse here
$html .= list2html($item['subpages']);
$html .= '</li>';
}
return '<ul>' . $html . '</ul>';
}
Use like this:
$html_output = list2html($aMenu);
Note urls are missing you will have to modify the function to add them
Note2 Recursion is not needed, one can iterate (or simulate recursion with iteration) and it will be faster but more complex code
You have to make recursive function, which will go trough the one level of array and call it self for every array found as the parameter.
I have a function common to sites based in different locations. The two-dimensional array looks something like this:
$sites = array(
'UK' => array(
"sitecode" => "AUTest",
"domain" => ".com.au",
"label" => "Australia",
),
'DE' => array(
"sitecode" => "DETest",
"domain" => ".de",
"label" => "Germany"
),
'CA' => array(
"sitecode" => "CATest",
"domain" => ".ca",
"label" => "Canada"
)
);
I'd like to display a list of all countries on each site, but always display the current country at the top of the list, with the others listed in their natural order, e.g.
In Australia, I want to see:
<li class="foo">Australia</li>
<li>Germany</li>
<li>Canada</li>
In Germany:
<li class="foo">Germany</li>
<li>Australia</li>
<li>Canada</li>
In Canada:
<li class="foo">Canada</li>
<li>Australia</li>
<li>Germany</li>
If I have something like this:
foreach ($sites as $key => $list) {
if (My_External_Variable == $list['sitecode']) {
echo '<li><a class="foo" href="http://www.example' . $list['domain'] .'/">' . $list['label'] . '</a>';
}
else {
echo '<li>' . $list['label'] . '</li>';
}
}
I can find the current country site I'm viewing and, for instance, apply a css class, but as it is, it's always going to display in it's index order.
I've looked at a lot of sort man pages, but have to confess I'm not a clever man. I think what I need to do is dynamically change the index based on the country site I'm currently viewing so that I can place it first, then iterate through the remaining. I'm sure it can be done, but am stuck trying to make it so.
I'm hoping someone can point me in the right direction.
you can save the output to variable and the selected to variable and after the loop chain them together so the first one will be the selected
$list = ''; $selected = '';
foreach ($sites as $key => $list) {
if (My_External_Variable == $list['sitecode']) {
$selected = '<li><a class="foo" href="http://www.example' . $list['domain'] .'/">' . $list['label'] . '</a>';
}
else {
$list .= '<li>' . $list['label'] . '</li>';
}
}
echo $selected . $list;
Why dont use another array to describe the order?
<?php
$sites = array(
'UK' => array(
"sitecode" => "AUTest",
"domain" => ".com.au",
"label" => "Australia",
),
'DE' => array(
"sitecode" => "DETest",
"domain" => ".de",
"label" => "Germany"
),
'CA' => array(
"sitecode" => "CATest",
"domain" => ".ca",
"label" => "Canada"
)
);
$sites_order = array(
"UK" => array("UK", "DE", "CA"),
"DE" => array("DE", "UK", "CA"),
"CA" => array("CA", "UK", "DE")
);
$selected_language = "DE";
foreach($sites_order[$selected_language] as $language) {
printf("<pre>%s</pre>",$sites[$language]["label"]);
}
?>
I am writing an application which interfaces with an external API. We have five API clients (in order to process more requests - which is allowed under the API T&C) which are stored in an array. We have another array of objects which we perform an action on using foreach.
For example:
$clients = array(
array(
"key" => "somekey",
"secret" => "somesecret"
),
array(
"key" => "somekey",
"secret" => "somesecret"
),
array(
"key" => "somekey",
"secret" => "somesecret"
)
);
Next we have an array of objects to be processed:
$objects = array(
array(
"info" => "someinfo",
"url" => "someurl",
"id" => 1234,
"more" => "more"
),
array(
"info" => "someinfo",
"url" => "someurl",
"id" => 1234,
"more" => "more"
),
array(
"info" => "someinfo",
"url" => "someurl",
"id" => 1234,
"more" => "more"
)
);
So to process them with one API key we would do something like:
foreach($objects as $object){
$class->setAPIKey($clients[0]);
$result = $class->process($object);
}
In order to process them with multiple keys we have tried this:
$key = 0;
foreach($objects as $object){
$class->setAPIKey($clients[$key]);
$result = $class->process($object);
if($key + 1 == sizeof($clients)){
$key = 0;
} else {
$key++;
}
}
This works, but seems a little inefficient. Is there a quicker/smaller way to do the same thing?
You can try it like this:
foreach($objects as $key => $object){
$class->setAPIKey($clients[$key]);
$result = $class->process($object);
}