Foreach limit to word - php

Im new here , looking for solution for this part of code to just make loop when title is = style or colour .
foreach ($pp_fileds as $f) {
if ( !empty($f['title']) and !empty($f["id"]) and array_key_exists("owlabpfl_".$f["id"] , $owlabpfl_meta) ){
echo '
<li>
<div class="list-label">'.__($f['title'],'toranj').'</div>
<div class="list-des">'.$owlabpfl_meta["owlabpfl_".$f["id"]][0].'</div><div class="list-des"><img src="http://factorymarco.ie/assets/'.__($f['title'],'toranj').'/'.$owlabpfl_meta["owlabpfl_".$f["id"]][0].'.jpg" alt="'.$owlabpfl_meta["owlabpfl_".$f["id"]][0].'"></div>
</li>
';
}
}

Related

Dynamically build html menu (and sub-menus) using PHP

I want to look for any html files in a specific folder, and build up my menu and sub-menus based on the results:
For example my project files may be : 'project1_qa_qa2.html',..., 'project2_dev_dev1.html', etc... And the explode() should return 'project1', 'qa', 'qa2' ; ... ; 'project2', 'dev', 'dev1' ; ...
I need to loop through each elements {project ( 1 or 2), department (qa or dev) , and id (dev1, qa2, ..)} to build my menu / sub-menus in a dynamic way.
I got a base source from : http://callmenick.com/_development/slide-down-menu/
I added my hardcoded sample code, to show what I'd wanted it to be like.
<?php
$cdir = scandir('./projects');
foreach ($cdir as $filename){
if(preg_match('/(\w+).html/',$filename,$project_name)){
$projects_details = explode('_',$project_name[1]);
}
?>
<div class="container">
<nav>
<ul class="content clearfix">
<li>Welcome</li>
<li class="dropdown">
Projects
<ul class="sub-menu" style="display: none;">
<li>Project1</li>
<li class="dropdown">
Project2
<ul class="sub-menu" style="display: none;">
<li class="dropdown">
Quality Insurance
<ul class="sub-menu" style="display: none;">
<li>QA1</li>
<li>QA2</li>
</ul>
</li>
<li class="dropdown">
Development
<ul class="sub-menu" style="display: none;">
<li>Dev1</li>
<li>Dev2</li>
</ul>
</li>
</ul>
</li>
</ul>
</ul>
</nav>
</div>
This is just to show you which code I've used and point you in the right direction. For scanning the directory:
function getFromDir( $dir ) {
$cdir = scandir( $dir );
$result = array();
foreach( $cdir as $key => $value ) {
if( !in_array( $value, array('.', '..') ) ) {
if( is_dir( $dir . DIRECTORY_SEPARATOR . $value ) ) {
$result[$value] = getFromDir($dir . DIRECTORY_SEPARATOR . $value);
} else {
$result[] = $value;
}
}
}
return $result;
}
As shown in the comment for creating the html:
function outputMenu(array $array, $baseUrl = '/') {
$html = '';
foreach( $array as $key => $item ) {
if( is_array( $item ) ) {
$html .= '<li>'.$key.'<ul>';
$html .= outputMenu($item, $baseUrl.$key.'/');
$html .= '</ul></li>';
} else {
$html .= '<li>'.ucfirst(substr($item, 0, -4)).'</li>';
}
}
return $html;
}
echo outputMenu(getFromDir('./projects'));

how to get data to html <ul> <li> </li> </ul> list from mysql database with php

I have a video player and want to populate video on it from mysql database, here is my code.
<ul id="playlist1" style="display:none;">
<li data-thumb-source="assets/img/ddd.jpg" data-video-source="assets/video/ddd.mp4" data-poster-source="assets/img/ddd.jpg" data-downloadable="yes">
<div data-video-short-description="">
<div>
<p class="minimalDarkThumbnailTitle">ddd</p>
<p class="minimalDarkThumbnailDesc">dddd.</p>
</div>
</div>
<div data-video-long-description="">
<div>
<p class="minimalDarkVideoTitleDesc">dddd</p>
<p class="minimalDarkVideoMainDesc">ddd</p>
<p>For more information about this please follow this link</p>
</div>
</div>
</li>
</ul>
My MySQL database has the following columns
id
data-thumb-source
data-video-source
data-poster-source
minimalDarkThumbnailTitle
minimalDarkThumbnailDesc
My PHP code to get the data from the database:
<?php
$result= mysql_query("select * from video order by id DESC" ) or die (mysql_error());
while ($row= mysql_fetch_array ($result) ){
$id=$row['id'];
?>
However, my video player is blank because it has nothing to play.
Edit: I used this code to display my value
<ul>
<?php
$sql = "SELECT * FROM `mytable`";
foreach ($db->query($sql) as $row) {
$li = '<li> data-thumb-source="' .$row['data-thumb-source']. '"';
$li .= ' class="playlistItem" data-type="local"';
$li .= ' data-video-source="' .$row['data-video-source']. '"';
$li .= ' minimalDarkThumbnailTitle="' .$row['minimalDarkThumbnailTitle']. '"';
$li .= ' minimalDarkThumbnailDesc="' .$row['minimalDarkThumbnailDesc']. '"';
echo $li;
}
$db = null;
?>
</ul>
This did not make a difference though.
You closed your opening li tag before defining the attributes.
<ul>
<?php
$sql = "SELECT * FROM `mytable`";
foreach ($db->query($sql) as $row) {
$li = '<li data-thumb-source="' .$row['data-thumb-source']. '"';
$li .= ' class="playlistItem" data-type="local"';
$li .= ' data-video-source="' .$row['data-video-source']. '"';
$li .= ' minimalDarkThumbnailTitle="' .$row['minimalDarkThumbnailTitle']. '"';
$li .= ' minimalDarkThumbnailDesc="' .$row['minimalDarkThumbnailDesc']. '"';
echo $li . '>descriptions</li>';
}
$db = null;
?>
</ul>

XPath multiple query, parsing based on class and in order

Current Situation :
I'm trying to parse a DomDocument with XPath, the result should be an array with Categories and Subcategories .
The problem is, the person that made the HTML did not structure the info with the subcategories in the main categories, they are just delimited by pure css .
The html loos like this :
<div class="menu_item">Main Category AC</div>
<div class="submenu_div">
<a href="http://www.link.com/313">
<div class="sub_item">
<h3>Sub Categ A</h3>
</div>
</a>
<a href="http://www.link.com/475">
<div class="sub_item">
<h3>Sub Categ B</h3>
</div>
</a>
<a href="http://www.link.com/321">
<div class="sub_item">
<h3>Sub Categ C</h3>
</div>
</a>
</div>
<div class="menu_item">Main Category BC</div>
<div class="submenu_div">
<a href="http://www.link.com/313">
<div class="sub_item">
<h3>Sub Categ X</h3>
</div>
</a>
<a href="http://www.link.com/475">
<div class="sub_item">
<h3>Sub Categ Y</h3>
</div>
</a>
<a href="http://www.link.com/321">
<div class="sub_item">
<h3>Sub Categ Z</h3>
</div>
</a>
</div>
Now, with this php I can extract de categories and subcategories, but it's just a list, I don't know what subcategory is in what category, and I'm stuck .
How can I use Xpath to do extract the main category subcategories and assign a parent to every subcategory ?
$doc = new DomDocument;
#$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
foreach( $xpath->query('//div[#class="menu_item"]|//div[#class="submenu_div"]/a/div/h3') as $e ) {
echo $e->nodeValue, "<br />\n";
}
This is a sketch for a solution using XPath. The outer loop looks for the categories and prints them. It also keeps track of the position of the outer div in variable $i. The inner loop constructs another XPath that selects the $i'th div tag, then goes to the following sibling and finally descends to the subcategory text.
Note that you still have to store this data into an appropriate data structure. I'm not familiar with PHP so I cannot help you a lot there.
$i = 0;
foreach( $xpath->query('//div[#class="menu_item"]/text()') as $category ) {
$i = $i + 1;
echo "Category: " . $category->nodeValue . "\n";
foreach ( $xpath->query('//div[#class="menu_item"][' . $i . ']/following-sibling::div[1][#class="submenu_div"]/a/div/h3/text()') as $subcategory) {
echo " Subcategory: " . $subcategory->nodeValue . "\n";
}
}
Based on the answer above, I have made some modifications to also include a for loop and also get the link :
for ($i = 0; $i <= 25; $i++) {
foreach( $xpath->query('//div[#class="menu_item"]['.$i.']/text()') as $category ) {
echo $i . " Category: " . $category->nodeValue . "<br/>\n";
foreach ( $xpath->query('//div[#class="menu_item"][' . $i . ']/following-sibling::div[1][#class="submenu_div"]/a') as $subcategory) {
echo '-----'. $i . " Subcategory: " . $subcategory->nodeValue . "<br/>\n";
echo '-----'. $i . " Link: " . $subcategory->getAttribute("href") . "<br/>\n";
}
echo "<br/>";
}
}
thanks again Marcus Rickert !

PHP: wrap each group of words starting with the same letter within a div

I have this loop that sorts a set of results alphabetically and shows a <span> with the current letter, but I can't find a way to wrap each subset within a div.
<?php
$previousLetter = null;
foreach($allBrands as $brand) {
$firstLetter = strtolower($brand->name[0]);
if ( $previousLetter != $firstLetter ) {
echo '<span class="designer-first-letter">'. $firstLetter .'</span>';
$previousLetter = $firstLetter;
}
echo '<p>'.$brand->name.'</p>';
}
I would like something like this
<div>
<span>A</span>
<p>Aword</p>
<p>Aword2</p>
<p>Aword3</p>
<p>...</p>
</div>
<div>
<span>B</span>
<p>Bword</p>
<p>Bword2</p>
<p>Bword3</p>
<p>...</p>
</div>
<div>
<span>C</span>
<p>Cword</p>
<p>Cword2</p>
<p>Cword3</p>
<p>...</p>
</div>
...
Right now what I get is
<span>A</span>
<p>Aword</p>
<p>Aword2</p>
<p>Aword3</p>
<p>...</p>
<span>B</span>
<p>Bword</p>
<p>Bword2</p>
<p>Bword3</p>
<p>...</p>
<span>C</span>
<p>Cword</p>
<p>Cword2</p>
<p>Cword3</p>
<p>...</p>
...
What about pre-grouping the brands before you loop through
$brands = Array("Aword", "Aword2", "Aword3", "BWord", "Bword2");
$groups = Array();
foreach($brands as $brand) {
$startsWith = strtolower($brand[0]);
if( array_key_exists($startsWith, $groups))
array_push($groups[$startsWith], $brand);
else
{
$groups[$startsWith] = Array($brand);
}
}
ksort($groups);
foreach($groups as $key => $value ) {
?>
<div>
<span><?php echo strtoupper($key) ?></span>
<?php foreach($value as $brand) { ?>
<p><?php echo $brand?></p>
<?php } ?>
</div>
<?php
}
And, ksort to make them alphabetical

simplehtmldom return the content of 2 divs inside another

here is my html :
<div id="main">
<div id="child1">
child1
link1
</div>
<div id="child2">
child2
link2
</div>
</div>
I am trying to return (echo in php) child1 and child2 as links
this is part of a HUGE file so I need to loop through it.
this is what I have so far but its not working :
$linkObjs = $html->find('#main');
foreach ($linkObjs as $linkObj) {
$title = trim($linkObj->fildchild()->plaintext);
$link = trim($linkObj->fildchild()->href);
echo '<p class="titro" ><a href="' . $link . '" >' . $title . '</a></p>';
}
Not sure exactly which part of the elements you needed so here's everything dissected.
// Find all divs in #main
foreach ($html -> find('#main div') as $div)
{
// Find plain text in div
foreach ($div -> find('text') as $text)
{
echo $text;
}
// Find <a> tags and href
foreach ($div -> find('a') as $a)
{
echo $a -> href;
}
}

Categories