I tried render data in loop and if extend_tag field have same text group in one container.
I only know hard code like below, loop data base on how many known extend_tag group, but actually the extend_tag numbers is unknown might be tag_ and any digit , any idea how to solve it?
data
[tag] => Array (
[0] => Array (
[id] => 1
[extend_tag] => tag_0
)
[1] => Array (
[id] => 2
[extend_tag] => tag_11
)
[2] => Array (
[id] => 3
[extend_tag] => tag_4
)
)
<ul class="container">
<?php foreach($rows['tag'] as $eachRowsTag) { ?>
<?php if ($eachRowsTag['extend_tag'] == 'tag_0') { ?>
<li>><?php echo $eachRowsTag['id']; ?></li>
<?php } ?>
<?php } ?>
</ul>
<ul class="container">
<?php foreach($rows['tag'] as $eachRowsTag) { ?>
<?php if ($eachRowsTag['extend_tag'] == 'tag_1') { ?>
<li>><?php echo $eachRowsTag['id']; ?></li>
<?php } ?>
<?php } ?>
</ul>
...
Why not group them first, then iterate over the resulting array. Something like the following.
foreach ($tags as $tag) {
$grouped[$tag['extend_tag']][] = $tag;
}
// Now $grouped is something along the lines of:
// [
// 'tag_0' => [
// [ 'id' => 1, 'extend_tag' => 'tag_0'],
// ..
// ],
// ..
// ]
foreach($grouped as $extend_tag => $tags) {
echo "All tags in $extended_tag.";
foreach($tags as $tag) {
echo $tag['id'];
}
}
// For something like:
// All tags in tag_0.
// 1
// 4
// All tags in tag_1.
// ..
Related
I have the below html
<p>text1</p>
<ul>
<li>list-a1</li>
<li>list-a2</li>
<li>list-a3</li>
</ul>
<p>text2</p>
<ul>
<li>list-b1</li>
<li>list-b2</li>
<li>list-b3</li>
</ul>
<p>text3</p>
Does anyone have an idea to parse this html file with php to get this output using complex array
fist one for the tags "p"
and the second for tags "ul" because after above every "p" tag a tag "ul"
Array
(
[0] => Array
(
[value] => text1
(
[il] => list-a1
[il] => list-a2
[il] => list-a3
)
)
[1] => Array
(
[value] => text2
(
[il] => list-b1
[il] => list-b2
[il] => list-b3
)
)
)
I can't use replace or removing all tags cause I use
foreach ($doc->getElementsByTagName('p') as $link)
{
$dont = $link->textContent;
if (strpos($dont, 'document.') === false) {
$links2[] = array(
'value' => $link->textContent, );
}
$er=0;
foreach ($doc->getElementsByTagName('ul') as $link)
{
$dont2 = $link->nodeValue;
//echo $dont2;
if (strpos($dont2, 'favorisContribuer') === false) {
$links3[]= array(
'il' => $link->nodeValue, );
}
You could use the DOMDocument class (http://php.net/manual/en/class.domdocument.php)
You can see an example below.
<?php
$html = '
<p>text1</p>
<ul>
<li>list-a1</li>
<li>list-a2</li>
<li>list-a3</li>
</ul>
<p>text2</p>
<ul>
<li>list-b1</li>
<li>list-b2</li>
<li>list-b3</li>
</ul>
<p>text3</p>
';
$doc = new DOMDocument();
$doc->loadHTML($html);
$textContent = $doc->textContent;
$textContent = trim(preg_replace('/\t+/', '<br>', $textContent));
echo '
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
' . $textContent . '
</body>
</html>
';
?>
However, I would suggest using javascript to find the content and send it to php instead.
I am looking to loop through the array below and then populate a <UL> with 1 random image from each color array. I am having trouble getting my head around how to access the specific color arrays... Do I need a loop? Or would array_rand() be enough? How would I go about this?
$colors = array(
'green' => array(
'images/green1.jpg',
'images/green2.jpg',
'images/green3.jpg',
'images/green4.jpg',
'images/green5.jpg'
),
'red' => array(
'/images/red1.jpg',
'/images/red2.jpg',
'/images/red3.jpg',
'/images/red4.jpg',
'/images/red5.jpg'
),
'blue' => array(
'/images/blue1.jpg',
'/images/blue2.jpg',
'/images/blue3.jpg',
'/images/blue4.jpg',
'/images/blue5.jpg'
),
'purple' => array(
'/images/purple1.jpg',
'/images/purple2.jpg',
'/images/purple3.jpg',
'/images/purple4.jpg',
'/images/purple5.jpg'
)
);
<div>
<span>Colors</span>
<ul>
<li>"1 img from 'green' array would go here"</li>
<li>"1 img from 'red' array would go here"</li>
<li>"1 img from 'blue' array would go here"</li>
<li>"1 img from 'purple' array would go here"</li>
</ul>
</div>
As you mentioned, array_rand() could be used, but you'll need to loop through the colors. For each one, get a random image:
$arr = array();
foreach($colors as $k=>$v){
$arr[] = $v[array_rand($v)];
}
print_r($arr);
Output 1:
Array
(
[0] => images/green3.jpg
[1] => /images/red3.jpg
[2] => /images/blue2.jpg
[3] => /images/purple1.jpg
)
Running again:
Array
(
[0] => images/green5.jpg
[1] => /images/red3.jpg
[2] => /images/blue1.jpg
[3] => /images/purple4.jpg
)
If you want to output it like in the question, it would be something like this:
// div span ul
$arr = array();
foreach($colors as $k=>$v){
echo '<li><img src="' . $v[array_rand($v)] . '"></li>';
}
// /div /ul
Side notes:
The green urls from the array are missing the leading / (or every other color has a spare, I dunno);
This codes does not check if the image exists (file_exists / PHP: How to check if image file exists?)
I would so something along the lines of
foreach ($colors as $color){
//This gets you each color array in turn, in here you can
//use array_rand() to get a random entry from each array.
}
This worked for me:
<?php
$colors = array(
'green' => array(
'images/green1.jpg',
'images/green2.jpg',
'images/green3.jpg',
'images/green4.jpg',
'images/green5.jpg'
),
'red' => array(
'/images/red1.jpg',
'/images/red2.jpg',
'/images/red3.jpg',
'/images/red4.jpg',
'/images/red5.jpg'
),
'blue' => array(
'/images/blue1.jpg',
'/images/blue2.jpg',
'/images/blue3.jpg',
'/images/blue4.jpg',
'/images/blue5.jpg'
),
'purple' => array(
'/images/purple1.jpg',
'/images/purple2.jpg',
'/images/purple3.jpg',
'/images/purple4.jpg',
'/images/purple5.jpg'
)
);
?>
<div>
<span>Colors</span>
<ul>
<?php
foreach ($colors as $key=>$value){
echo '<li>'.$value[array_rand($value,1)]."</li>";
}
?>
</ul>
</div>
foreach ($colors as $color){
$image = array_rand($color);
echo '<li>' . $color[$image] . '</li>';
}
I have a JSON file that contain this:
"Menus": [{
"MenuId": "1",
"MenuName": "Perencanaan dan Pengadaan",
"MenuParent": "",
"MenuLink": ""
}, {
"MenuId": "1-1",
"MenuName": "RKA / DPA",
"MenuParent": "1",
"MenuLink": ""
}, {
"MenuId": "1-1-1",
"MenuName": "Daftar RKA / DPA",
"MenuParent": "1-1",
"MenuLink": "rkbu"
},
I want to put that data into unordered list dynamically. So the output I want is like this (with 3 level list):
Perencanaan dan Pengadaan
RKA / DPA
Daftar RKA / DPA
I have tried this code:
echo "<ul>";
foreach($get_data['Menus'] as $node){
if(strlen($node['MenuId']) == 1){
echo "<li>" . $node['MenuName'];
echo "</li>";
}
echo "<ul>";
if(strlen($node['MenuId']) == 3){
echo "<li>".$node['MenuName']."</li>";
}
if(strlen($node['MenuId']) == 5){
echo "<ul>";
echo "<li>".$node['MenuName']."</li>";
echo "</ul>";
}
echo "</ul>";
}
echo "</ul>";
But I find that it is not dynamic because it depends on string length. I've read that the best method is using recursive method. But I cannot find the recursive pattern of my JSON file. Can anybody help me find the solution? Thanks
I don't think it is possible to make recursive calls directly on your flat JSON data.
I suggest you first convert your flat data to a multidimensional array and afterwards recursively generate your menu.
I took parts of the code from here: Dynamically creating/inserting into an associative array in PHP
$get_data = array(
array(
"MenuId" => "1",
"MenuName" => "Perencanaan dan Pengadaan",
"MenuParent" => "",
"MenuLink" => ""
),
array(
"MenuId" => "1-1",
"MenuName" => "RKA / DPA",
"MenuParent" => "1",
"MenuLink" => ""
),
array(
"MenuId" => "1-1-1",
"MenuName" => "Daftar RKA / DPA",
"MenuParent" => "1-1",
"MenuLink" => "rkbu"
)
);
function insert_into(&$array, array $keys, $value) {
$last = array_pop($keys);
foreach($keys as $key) {
if(!array_key_exists($key, $array) ||
array_key_exists($key, $array) && !is_array($array[$key])) {
$array[$key]['items'] = array();
}
$array = &$array[$key]['items'];
}
$array[$last]['value'] = $value;
}
function create_menu($menuItems) {
$content = '<ul>';
foreach($menuItems as $item) {
$content .= '<li>' . $item['value'];
if(isset($item['items']) && count($item['items'])) {
$content .= create_menu($item['items']);
}
$content .= '</li>';
}
$content .= '</ul>';
return $content;
}
$menuItems = array();
foreach($get_data as $item) {
$levels = explode('-', $item['MenuId']);
insert_into($menuItems, $levels, $item['MenuName']);
}
print_r($menuItems);
print create_menu($menuItems);
DEMO: http://3v4l.org/dRK4f
Output:
Array (
[1] => Array (
[value] => Perencanaan dan Pengadaan
[items] => Array (
[1] => Array (
[value] => RKA / DPA
[items] => Array (
[1] => Array (
[value] => Daftar RKA / DPA
)
)
)
)
)
)
<ul>
<li>Perencanaan dan Pengadaan
<ul>
<li>RKA / DPA
<ul>
<li>Daftar RKA / DPA</li>
</ul>
</li>
</ul>
</li>
</ul>
I Have an array called $pages content is as follows:
Array
(
[01-about-us] => Page Object
(
[_uri] => about-us
[_menuItem] => 01
[_visable] => 1
)
[02-contact] => Page Object
(
[_uri] => contact
[_menuItem] => 02
[_visable] => 1
)
[_sitemap] => Page Object
(
[_uri] => sitemap
[_menuItem] =>
[_visable] =>
)
[home] => Page Object
(
[_uri] => home
[_menuItem] =>
[_visable] => 1
)
)
is there an easy way to loop through and get page objects by there properties ie:
<?php foreach($pages->_visible() AS $p): ?>
<li> page </li>
<?php endforeach ?>
No. You will have to use an if:
foreach ($pages as $page) {
if ($page->_visible == 1) {
echo "<li>page</li>";
}
}
(Note also you misspelt visible in the array, perhaps a typo?)
Or you can utilize PHP's array_filter function:
$pagesVisible = array_filter($pages, function($page) {
return (bool) $page->_visible;
});
foreach ($pagesVisible as $key => $page) {
print '<li>' . $key . '</li>';
}
Or shorthand it to:
$filter = function($page) {
return (bool) $page->_visible;
};
foreach (array_filter($pages, $filter) as $key => $page) {
print '<li>' . $key . '</li>';
}
You just need to loop through the pages array and inside the loop access the object properties like:
<?php foreach($pages as $k => $p): ?>
<?php if ($p->_visable === 1): ?>
<li><?php echo $k; ?></li>
<?php endif; ?>
<?php endforeach; ?>
Please note that visable is misspelled but thats how it is in your question
I want to create a dynamic menu in PHP and based on what page they are on the menu will have different styling. I have this but it's not how I want it to be :(
This is the array I have, containing info from the database
Array(
[Home] => Array
(
[url] => Home
[name] => Home
[is_home] => 1
)
[About] => Array
(
[url] => About
[name] => About
[is_home] => 0
)
[Contact] => Array
(
[url] => Contact.php
[name] => Contact
[is_home] => 0
)
)
This is what I currently have,
if(isset($_GET["p"])) {
if(in_array($page_name, $navigation[$page_name])) {
$navigation[$page_name]["name"] = "<span>{$navigation[$page_name]["name"]}</span>";
}
}
foreach ($navigation as $nav) {
echo "<li>{$nav["name"]}</li>";
}
This is how the page_name variable looks
$page_name = current(explode(".", ucfirst(strtolower($_GET["p"]))));
As you can see this inserts the span tags in the navigation menu name so this works but that's not how I want it to be. I want to add class="active" the the list item that is the current page. I just don't know how to do it
I hope you understand what I mean and sorry for any messy indentation that occurred when pasting the code in here.
//Edit
The fetch and array code
$mysql->query("SELECT `page_name`, `is_home` FROM `pages` ORDER BY `order` ASC");
$navigation_items = array();
while ($mysql->fetch($row)){
$navigation_items[] = array(
"url" => $row["page_name"],
"name" => current(explode(".", $row["page_name"])),
"is_home" => $row["is_home"]
);
}
return $navigation_items;
First of all the array you provided is reformatted which means you changed the indexes into page names which isn't necessary.
This is how you can achieve what you want:
<?php
$menu = $Db->fetchAll("SELECT * FROM `menu`"); //or whatever method you're using to get data from the database.
$current = null;
if(isset($_GET['p'])) {
$current = current(explode(".", ucfirst(strtolower($_GET["p"]))));
}
$navigation = '';
for($i=0;$i<count($menu);$i++) {
$url = ucfirst(strtolower($menu[$i]['url']));
if($current == $url)
$class = ' class="active"';
else
$class = '';
$name = '<span'.$class.'>'.$menu[$i]['name'].'</span>';
$navigation .= "<li><a href='{$url}'>{$name}</a></li>";
}
?>