I have an array like this:
$files = array(
array(
'name' => 'Detailed Brief - www.xyz.com.pdf',
'size' => '1.4MB',
),
array(
'name' => 'Pure WordPress theme.zip',
'size' => '735.9KB',
),
array(
'name' => 'Logotype.jpg',
'size' => '94.7KB',
),
);
How can I display the information as follows:
ul - li
Detailed Brief...- pdf(1.4mb)
Pure Wordpress... - zip(735.kb)
Logotype.jpg-(94,7kb)
Any ideas? I'm trying all the time with foreach but it's not working.
It is simple:
<ul>
<?php foreach ($files as $key => $file): ?>
<li><?php echo $file['name'] . ' - ' . $file['size'] ?></li>
<?php endforeach ?>
</ul>
The other answer is not quite what you're looking for. For each iteration of the loop, you want the filename, the file extension, and the file size:
<ul>
<?php
foreach ($files as $key => $file) {
$f = pathinfo($file['name']);
echo '<li>';
echo $f['filename'].' - '.$f['extension'].'('.$file['size'].')';
echo '</li>';
}
?>
</ul>
Related
I have an array with my navbar items
pagearray.php :
<?php
$pages = array(
array(
"slug" => "/php/site1.php",
"title" => 'Site One',
),
array(
"slug" => "/php/site2.php",
"title" => 'Site Two',
),
array(
"slug" => "/php/site2.php",
"title" => 'Site Three',
),
)
?>
And im trying to build a navigation bar using foreach-loop, where the page you are on would have .active class.
navbar.php
<?php
include "/includes/pagearray.php";
foreach ($pages as $page) {
echo "<li>$page[title] </li>";
}
?>
Is this even possible by using only HTML, CSS and PHP? Am I using wrong technique to build a navbar?
Here is your new navbar.php - this should work. This will add active class to the links (<a> elements)
<?php
include "/includes/pagearray.php";
foreach ($pages as $page) {
echo '<li>' . $page['title'] . '</li>';
}
Not sure if I titled this question correctly. I'm having some trouble looping over a multi-demensional php array to build some HTML nodes. Here is the array I'm looping over:
$locations = array(
'CityName' => array(
array(
'title' => 'Title',
'phone' => '(555) 555-5555',
'address' => '1234 Fake st.',
'city' => 'Ventura',
'state' => 'CA',
'zip' => '93003',
'url' => 'http://www.google.com/'
),
array(
'title' => 'Title',
'phone' => '(555) 555-5555',
'address' => '1234 Fake st.',
'city' => 'Ventura',
'state' => 'CA',
'zip' => '93003',
'url' => 'http://www.google.com/'
),
),
'CityName2' => array(
array(
'title' => 'Title',
'phone' => '(555) 555-5555',
'address' => '1234 Fake st.',
'city' => 'Ventura',
'state' => 'CA',
'zip' => '93003',
'url' => 'http://www.google.com/'
),
array(
'title' => 'Title',
'phone' => '(555) 555-5555',
'address' => '1234 Fake st.',
'city' => 'Ventura',
'state' => 'CA',
'zip' => '93003',
'url' => 'http://www.google.com/'
)
)
);
Keep in mind I may have built this array incorrectly for what I'm trying to do. The HTML output for this loop should be:
<h4>CityName</h4>
<ul>
<li>
<p>Title</p>
<p>1234 Fake St.</p>
<p>Ventura, CA 93003</p>
<p>(555) 555-5555</p>
<p class="link">Visit Website</p>
</li>
<li>
<p>Title</p>
<p>1234 Fake St.</p>
<p>Ventura, CA 93003</p>
<p>(555) 555-5555</p>
<p class="link">Visit Website</p>
</li>
</ul>
<h4>CityName2</h4>
<ul>
...
</ul>
I think what I want to do is to be able to grab the individual pieces of data to plug into my HTML template.. like $location['title'], $location['phone'], etc. The PHP that I currently have will only go as far to loop over and echo out the keys or values from each individual location array.
<?php
// Printing all the keys and values one by one
$locationNames = array_keys($locations);
for($i = 0; $i < count($locations); $i++) {
echo "<h4>" . $locationNames[$i] . "</h4>";
echo "<ul>";
foreach($locations[$locationNames[$i]] as $key => $value) {
foreach($value as $key => $value) {
echo $value;
}
}
echo "</ul>";
}
?>
Use nested foreach loops amd drop the values in to the appropriate places:
<?php foreach ($locations as $location => $ldata) { ?>
<h4><?php echo $location; ?></h4>
<ul>
<?php foreach ($ldata as $attribute) { ?>
<li>
<p><?php echo $attribute['title']; ?></p>
<p><?php echo $attribute['address']; ?></p>
<p><?php echo $attribute['city'] . " ," . $attribute['state'] . " " . $attribute['zip']; ?></p>
<p><?php echo $attribute['phone']; ?></p>
<p class="link">Visit Website</p>
</li>
<? php } ?>
<?php } ?>
You just need nested (foreach) loops:
<?php foreach($locations as $cityname => $location):?>
<h4><?=$cityname?></h4>
<ul>
<?php foreach($location as $place:?>
<li>
<p><?=$place['title']?></p>
<p><?=$place['phone']?></p>
<!-- etc etc-->
</li>
<?php endforeach;?>
</ul>
<?php endforeach;?>
Something like this should work. I won't implement the HTML for you, but you it should be easy to do. This has the advantage that if you have dynamic keys in the inner array, you won't have to know them before hand.
foreach($locations as $key => $value) {
echo $key, PHP_EOL;
$data = $locations[$key];
$length = count($data);
for($i = 0; $i < $length; $i++) {
$values = $data[$i];
foreach($values as $key2 => $value2)
echo "\t", $key2, ": ", $value2, PHP_EOL;
}
}
Just a few tweaks to your code:
<?php
// Printing all the keys and values one by one
$locationNames = array_keys($locations);
for($i = 0; $i < count($locations); $i++) {
echo "<h4>" . $locationNames[$i] . "</h4>";
echo "<ul>";
foreach($locations[$locationNames[$i]] as $key => $value) {
echo "<li>"; // add list open tag <-- tweak #1
foreach($value as $key => $value) {
echo "<p>$value</p>"; // add paragraph tags <-- tweak #2
}
echo "</li>"; // add list close tag <-- tweak #3
}
echo "</ul>";
}
?>
PHP Sandbox example.
I have a multi dimensional array that is printing out exactly how I want it, however, I have become stuck on figuring out how I can construct it into the for each loop that i'm looking for.
Please Note : $handle is a field the client entered in the backend.
<?php
$gp = Mage::getStoreConfig('social_code/social_group/google_field');
$ld = Mage::getStoreConfig('social_code/social_group/linkedin_field');
$tw = Mage::getStoreConfig('social_code/social_group/twitter_field');
$fb = Mage::getStoreConfig('social_code/social_group/facebook_field');
$social_array = array(
"facebook" => array(
'class' => "facebook",
'url' => 'https://www.facebook.com/',
'handle' => $fb
),
"twitter" => array(
'class' => "twitter",
'url' => 'https://www.twitter.com/',
'handle' => $tw
),
"linked-in" => array(
'class' => "linked-in",
'url' => 'http://www.linkedin.com/company/',
'handle' => $ld
),
"google-plus" => array(
'class' => "google-plus",
'url' => 'https://plus.google.com/',
'handle' => $gp
)
);
?>
Now i wish to spit this out as an unordered list so i have the below, but its still not working for me.
<ul>
<?php foreach ($social_array as $name => $group) :?>
<?php foreach ($group as $class => $url) :?>
<li><?php echo ("$group"); ?></li>
<?php endforeach; ?>
<?php endforeach; ?>
</ul>
I would like it so that it loops through all the social array and prins something similar to this
<li><?php echo $name; ?></li>
or so understood better
<li>Facebook</li>
Also if I'm making this over complicated for myself please let me know.
<ul>
<?php foreach ($social_array as $name => $group) :?>
<li><?php echo $name; ?></li>
<?php endforeach; ?>
</ul>
I think this is what you're looking for if I've understood correctly.
<ul>
<?php foreach ($social_array as $name => $group) :?>
<li><a href="<?php echo $group['url'].$group[handle']; ?>" class="<?php echo $group['class']; ?>"><?php echo $name
<?php endforeach; ?>
</ul>
There is no need to inner foreach.
<?php foreach ($social_array as $name => $group) :?>
<li><?php echo $group['class']; ?></li>
<?php endforeach; ?>
See http://3v4l.org/3cH6f for the example working below. Just one foreach.
<?php
$social_array = array(
"facebook" => array(
'class' => "facebook",
'url' => 'https://www.facebook.com/',
'handle' => 'Mage::getStoreConfig(\'social_code/social_group/twitter_field\')'
),
"twitter" => array(
'class' => "facebook",
'url' => 'https://www.facebook.com/',
'handle' => 'Mage::getStoreConfig(\'social_code/social_group/twitter_field\')'
),
"linked-in" => array(
'class' => "facebook",
'url' => 'https://www.facebook.com/',
'handle' => 'Mage::getStoreConfig(\'social_code/social_group/twitter_field\')'
),
"google-plus" => array(
'class' => "facebook",
'url' => 'https://www.facebook.com/',
'handle' => 'Mage::getStoreConfig(\'social_code/social_group/twitter_field\')'
)
);
$html = '<ul>';
foreach ($social_array as $name => $class_array){
$html .= '<li>'. $name. '</li>';
}
$html .= '</ul>';
print $html;
?>
Give this a shot:
<ul>
<?php foreach ($social_array as $name => $properties) :?>
<li><?php echo ucfirst($name); ?></li>
<?php endforeach; ?>
</ul>
In this foreach, the syntax is foreach($array as $key => $value), so here $name is the key from $social_array, and $properties is the array associated with that key.
The thing you are forgetting is that the inner foreach is processing each item i.e. class,ulr,handle one at a time.
You therefore need to store the information you require as you pass over each of the 3 items so it can be used once you have the 2 items you actually need.
So this may help.
<ul>
<?php
foreach ($social_array as $name => $group) :
$class = NULL;
$url = NULL;
$handle = NULL;
foreach ($group as $name => $value) :
switch ($name) :
case 'class' : $class = $value; break;
case 'url' : $url = $value; break;
case 'handle' : $handle = $value; break;
endswitch;
if ( isset($class) AND isset($url) AND isset($handle) ) :
echo '<li>' . $group . '</li>';
$class = NULL;
$url = NULL;
$handle = NULL;
endif;
endforeach;
endforeach;
?>
</ul>
I have this code:
<?php foreach ($galerije as $gal): ?>
<h1><?php echo str_replace('_', ' ', $gal['naziv']) ?></h1>
<p><?php echo word_limiter($gal['opis'], 30) ?></p>
<?php echo count($slike) ?>
<hr>
<?php endforeach ?>
$galerija represent all the galleries in the DB and have id_galerija as the primary key.
$slike represent all the images in the DB and they have one to many relationship with galerija (one gallery can have many images). It is connected with galerija_id (slike.galerija_id = galerija.id_galerija). How can I count number of images for every gallery?
You could do it like this:
$galleries = array(array(
'Galleryname' => 'Picasso',
'Galleryimages' => array(
'1.jpg',
'2.jpg',
'3.jpg',
'4.jpg'
),
array(
'1.jpg',
'2.jpg'
),
array(
'1.jpg',
'2.jpg',
'3.jpg',
'4.jpg',
'5.jpg',
'6.jpg'
),
),
array(
'Galleryname' => 'Matisse',
'Galleryimages' => array(
'1.jpg',
'2.jpg'
),
array(
'1.jpg',
'2.jpg',
'3.jpg'
),
array(
'1.jpg',
'2.jpg',
'3.jpg',
'4.jpg',
'5.jpg',
'6.jpg',
'7.jpg',
'8.jpg'
),
)
);
$counts = array();
foreach ($galleries as $key => $value) {
$counts[$value['Galleryname']] = (count($value, COUNT_RECURSIVE)-count($value));
}
echo '<pre>';
print_r($counts);
echo '</pre>';
Which would end up with:
Array
(
[Picasso] => 12
[Matisse] => 13
)
Is that what you wanted?
wrap it
i is the count
$i = 0;
foreach ($Contents as $item) {
$i++;
$item[number];
}
I have seen many PHP function on how to generate a <ul><li> tag but my array input is quite complicated I guess. It is an array returned from a custom function called xml2assoc
My question is how can I convert the returned xml2assoc array result to a <ul><li> formatted HTML code using PHP.
Thanks.
$tree = array(
0 => array(
'tag' => 'NavigationMode',
'value' => array(
0 => array(
'tag' => 'Title',
'value' => 'Introduction'
),
1 => array(
'tag' => 'NavigationNode',
'value' => array(
0 => array(
'tag' => 'Title',
'value' => 'Sub Intro'
)
)
)
)
),
1 => array(
'tag' => 'NavigationMode',
'value' => array(
0 => array(
'tag' => 'Title',
'value' => 'Module 1'
)
)
)
);
The final output that I need to generate is like this:
<ul>
<li>
Introduction
<ul>
<li>Sub Intro</li>
</ul>
</li>
<li>Module 1</li>
</ul>
If you have XML as input, why not use XSLT to transform it to <ul>?
I guess your input looks something like this (I assume "NavigationMode" is a typo):
<tree>
<NavigationNode>
<title>Introduction</title>
<NavigationNode>
<title>Sub Intro</title>
</NavigationNode>
</NavigationNode>
<NavigationNode>
<title>Module 1</title>
</NavigationNode>
</tree>
With a small XSLT 1.0 stylesheet:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:template match="/tree">
<ul>
<xsl:apply-templates select="NavigationNode" />
</ul>
</xsl:template>
<xsl:template match="NavigationNode">
<li>
<xsl:value-of select="title" />
<xsl:if test="NavigationNode">
<ul>
<xsl:apply-templates select="NavigationNode" />
</ul>
</xsl:if>
</li>
</xsl:template>
</xsl:stylesheet>
This output is produced:
<ul>
<li>
Introduction
<ul>
<li>Sub Intro</li>
</ul>
</li>
<li>Module 1</li>
</ul>
The PHP documentation shows how to use XSLT. It's simple.
Here is a quick PHP implementation for your array structure to get you started:
function create_html_list($nodes)
{
echo '<ul>';
foreach ($nodes as $node) {
$childNodes = $node['value'];
$titleNode = array_shift($childNodes);
echo "<li>", $titleNode['value'];
if (count($childNodes) > 0) {
create_html_list($childNodes);
}
echo "</li>";
}
echo '</ul>';
}
i didn't test it for variations of the demo data ...
<?php
function getTitle($node) {
foreach ($node['value'] as $cnode) {
if ($cnode['tag'] == 'Title') {
return $cnode['value'];
}
}
return 'untitled';
}
function getCNodes($node) {
$cnodes = array();
foreach ($node['value'] as $cnode) {
if ($cnode['tag'] == 'NavigationNode') {
$cnodes[] = $cnode;
}
}
return $cnodes;
}
function runTree($node) {
$title = getTitle($node);
$cnodes = getCNodes($node);
if (count($cnodes) > 0) {
$out = '<li>' . $title . "\n" . '<ul>';
foreach ($cnodes as $cnode) {
$out .= runTree($cnode);
}
$out .= '</ul>' . "\n" . '</li>' . "\n";
return $out;
} else {
return '<li>' . $title . '</li>' . "\n";
}
}
$tree = array(
0 => array(
'tag' => 'NavigationMode',
'value' => array(
0 => array(
'tag' => 'Title',
'value' => 'Introduction'
),
1 => array(
'tag' => 'NavigationNode',
'value' => array(
0 => array(
'tag' => 'Title',
'value' => 'Sub Intro'
)
)
)
)
),
1 => array(
'tag' => 'NavigationMode',
'value' => array(
0 => array(
'tag' => 'Title',
'value' => 'Module 1'
)
)
)
);
echo '<ul>';
foreach ($tree as $node) {
echo runTree($node);
}
echo '</ul>';
?>