i am literally stuck with getting these ul/li elements in one row.
Only first li has class, others are in another ul but without class or id. i tried multiple searches and loops, but can't get it right.
Below results returns too many rows where elements don't fit together.
Can someone help me?
idealy the result from below example would be:
'part', 'desc', 'qty'
'106128-001','LBL, BLNK,THERMAL PRINT','1'
'106128-004','LBL, BLNK THERMAL,RECT 2.40x4','1'
HTML example
<div id='tab1' class="section tab" data-position="auto" style="max-height:none;">
<ul class="links">
<li id="innertab1">106128-001</li>
</ul>
<ul id="combom0" class="tab1 cols2 compare">
<li style="border: none;">
<div>
<ul>
<li><strong>Description: </strong>LBL, BLNK,THERMAL PRINT</li>
<li><strong>Qty: </strong>1</li>
</ul>
</div>
</li>
</ul>
<ul class="links">
<li id="innertab1">106128-004</li>
</ul>
<ul id="combom1" class="tab1 cols2 compare">
<li style="border: none;">
<div>
<ul>
<li><strong>Description: </strong>LBL, BLNK THERMAL,RECT 2.40x4</li>
<li><strong>Qty: </strong>1</li>
</ul>
</div>
</li>
</ul>
My poor attempt with trying to count rows:
$search = $html->find('div[id=tab1]',0);
$i=0;
$alles=array();
foreach ($search->find('* li') as $test){
if(!empty($test->innertext)){
if($i>3){$i=0;}
$i++;
if($i==1){
echo $alles_li['part'] = strip_tags($test->innertext);
}
if($i==2){
// don't need this, bad stripping
strip_tags($test->innertext);
}
if($i==3){
$alles_li['desc'] = strip_tags($test->innertext);
}
if($i==4){
$alles_li['qty'] = strip_tags($test->innertext);
}
}
// if less than 3 elements don't add
if(count($alles_li)==3){
$alles[]=$alles_li;
}
}
Related
In a typical HTML as
<ol>
<li>
<span>parent</span>
<ul>
<li><span>nested 1</span></li>
<li><span>nested 2</span></li>
</ul>
</li>
</ol>
I try to get the contents of <li> elements but I need to get the parent and those nested under ul separately.
If go as
$ols = $doc->getElementsByTagName('ol');
foreach($ols as $ol){
$lis = $ol->getElementsByTagName('li');
// here I need li immediately under <ol>
}
$lis is all li elements including both parent and nested ones.
How can I get li elements one level under ol by ignoring deeper levels?
There are two approaches to this, the first is how you are working with getElementsByTagName(), the idea would be just to pick out the first <li> tag and assume that it is the correct one...
$ols = $doc->getElementsByTagName('ol');
foreach($ols as $ol){
$lis = $ol->getElementsByTagName('li')[0];
echo $doc->saveHTML($lis).PHP_EOL;
}
This echoes...
<li>
<span>parent</span>
<ul>
<li><span>nested 1</span></li>
<li><span>nested 2</span></li>
</ul>
</li>
which should work - BUT is not exact enough at times.
The other method would be to use XPath, where you can specify the levels of the document tags you want to retrieve. This uses //ol/li, which is any <ol> tag with an immediate descendant <li> tag.
$xp = new DOMXPath($doc);
$lis = $xp->query("//ol/li");
foreach ( $lis as $li ) {
echo $doc->saveHTML($li);
}
this also gives...
<li>
<span>parent</span>
<ul>
<li><span>nested 1</span></li>
<li><span>nested 2</span></li>
</ul>
</li>
I am working on a HTML code as shown below in which I want to control li tags through php code.
HTML Code:
<li class="vidlist-main__item cf">
</li>
<li class="vidlist-main__item cf">
</li>
<li class="vidlist-main__item cf">
</li>
<li class="vidlist-main__item cf">
</li>
php code:
<input type="text" name="no_articles_EN" style="width: 77.69px;height: 22px;" value="<?php if($data->{"no_articles_EN"}<>''){echo $data->{"no_articles_EN"};} ?>"> // Line#A
<input type="text" name="no_articles_FR" style="width: 77.69px;height: 22px;" value="<?php if($data->{"no_articles_FR"}<>''){echo $data->{"no_articles_FR"};} ?>"> // Line#B
At Line#A on php code above, let say if the value entered is 3(no_articles_EN), then li tags should be 3 as shown below:
HTML Code:
<li class="vidlist-main__item cf">
</li>
<li class="vidlist-main__item cf">
</li>
<li class="vidlist-main__item cf">
</li>
And if the value entered 2(no_articles_EN) then li tags should be 2 as shown below:
HTML Code:
<li class="vidlist-main__item cf">
</li>
<li class="vidlist-main__item cf">
</li>
Problem Statement:
I am wondering what changes I should make in the HTML code above so if no_articles_EN is 3/4/5 then li tags should be vice versa. I came with the following logic but I think I need to more in it.
<?php
if($data->{"no_articles_EN"}) // Let say when no_articles_EN is 3 then li tags should be 3.
{
?>
<li class="vidlist-main__item cf">
</li>
<li class="vidlist-main__item cf">
</li>
<li class="vidlist-main__item cf">
</li>
<?php
}
?>
If $data->no_articles_EN is an integer equal to the number of items you want. Which is what it appears to be you can use a simple for loop.
if the value entered is 3(no_articles_EN), then li tags should be 3
if the value entered 2(no_articles_EN) then li tags should be 2 as shown below:
I prefer the ALT PHP syntax when working with mixed content (PHP/HTML) it's just cleaner. But if you prefer the {...} by all means use that. The alt syntax uses a : and end{command}; instead of the {}. It's just easier to move the code around like this, easier to find the end tags etc.
<?php if($data->no_articles_EN): ?> // Let say when no_articles_EN is 3 then li
<?php for($i=0; $i<$data->no_articles_EN; ++$i): ?>
<li class="vidlist-main__item cf"></li>
<?php endFor; ?>
<?php endif; ?>
You shouldn't use the variable property syntax $data->{"no_articles_EN"} unless you really need it, as it reduces readability and adds complexity when it's not needed. That is mainly for things like this $data->{"no_articles_".$language} where you could have part of the property name as a variable.
Cheers!
I have a simple PHP Array called $gatherArr that looks like this:
$gatherArr = [
["catCode"=>"ac", "catName"=>"Armchair"],
["catCode"=>"sf", "catName"=>"Sofa"],
["catCode"=>"st", "catName"=>"Side Table"],
["catCode"=>"ct", "catName"=>"Coffee Table"],
["catCode"=>"dt", "catName"=>"Table"],
["catCode"=>"cs", "catName"=>"Chair / Stool"],
["catCode"=>"sb", "catName"=>"Side Board"],
];
I want to build a Menu bar with PHP code and get the dropdown list from the $gatherArr. This is my HTML code below.
<div class="collapse navbar-collapse navHeaderCollapse">
<ul class="nav navbar-nav navbar-right">
<li class="active">About</li>
<li>News</li>
<li class="dropdown">
Collection <b class="caret"></b>
<ul class="dropdown-menu">
<?php
include './productData.php';
foreach ($gatherArr as $name => $detail) {
echo" {$detail}<br />";
}
?>
</ul>
</li>
<li>Ordering</li>
<li>Contact</li>
</ul>
</div>
I get lines of errors in displaying in the drop down menu bar. I have also tried change the echo" {$detail}<br />"; To $name but I got numbers from 0 - 6 in the menu bar
Thanks!
{$detail}
is array, You can't print or echo arrays. You can, however, print a specific element of it like:
{$detail['catCode']}
or
{$detail['catName']}
This is my first post so forgive as I am just new in the world of web development.
Normally, when I try to make a website, I create a file called header.html and footer.html so that I only change data once in all of the pages rather than having multiple same headers on many html files. And include them all in a php file together with the content and the php codes that comes per page.
Now my problem is because I only have 1 header, the css is designed in a way that whatever the current menu/tab is, it will be marked as "selected" so that its obvious to the user what page they are currently in.
My question is how do I solve this problem:
1.) To have the class="selected" depending on what the current page/url is.
<!--Menu Starts-->
<div class="menu">
<div id="smoothmenu" class="ddsmoothmenu">
<ul>
<li>Home</li>
<li>About </li>
<li>Services </li>
<li>Features</li>
<li>Support
<ul>
<li>Support 1</li>
<li>Support 2</li>
</ul>
</li>
</ul>
</div>
</div>
<!-- Menu Ends--!>
Thank You :)
If you're looking for a non-javascript / php approach...
First you need to determine which nav-link should be set as active and then add the selected class. The code would look something like this
HTML within php file
Call a php function inline within the hyperlink <a> markup passing in the links destination request uri
<ul>
<li><a href="index.php" <?=echoSelectedClassIfRequestMatches("index")?>>Home</a></li>
<li><a href="about.php" <?=echoSelectedClassIfRequestMatches("about")?>>About</a> </li>
<li><a href="services.php" <?=echoSelectedClassIfRequestMatches("services")?>>Services</a> </li>
<li><a href="features.php" <?=echoSelectedClassIfRequestMatches("features")?>>Features</a></li>
<li>Support
<ul>
<li><a href="support1.php" <?=echoSelectedClassIfRequestMatches("support1")?>>Support 1</a></li>
<li><a href="support2.php" <?=echoSelectedClassIfRequestMatches("support2")?>>Support 2</a></li>
</ul>
</li>
</ul>
PHP function
The php function simply needs to compare the passed in request uri and if it matches the current page being rendered output the selected class
<?php
function echoSelectedClassIfRequestMatches($requestUri)
{
$current_file_name = basename($_SERVER['REQUEST_URI'], ".php");
if ($current_file_name == $requestUri)
echo 'class="selected"';
}
?>
You could ID each link and use JavaScript/Jquery to add the selected class to the appropriate link.
<!--Menu Starts-->
<div class="menu">
<div id="smoothmenu" class="ddsmoothmenu">
<ul>
<li id="home-page">Home</li>
<li id="about-page">About </li>
<li id="services-page">Services </li>
<li id="features-page">Features</li>
<li id="support-page">Support
<ul>
<li id="support1-page">Support 1</li>
<li id="support2-page">Support 2</li>
</ul>
</li>
</ul>
</div>
</div>
<!-- Menu Ends--!>
On your content page use jQuery to do something like:
$(document).ready(function(){
$("#features-page").addClass("selected");
});
Another method you could use is:
Add class element based on the name of the page
Give each link a separate id then use jQuery on the individual pages.
<li>Home</li>
<li>About </li>
<li>Services </li>
<li>Features</li>
<li>Support
<ul>
<li>Support 1</li>
<li>Support 2</li>
</ul>
</li>
On the services page:
$(document).ready(function(){
$("#services").addClass("selected");
});
Or even better as robertc pointed out in the comments, there is no need to even bother with the id's just make the jquery this:
$(document).ready(function(){
$("[href='services.php']").addClass("selected");
});
One variant on Chris's approach is to output a particular class to identify the page, for example on the body element, and then use fixed classes on the menu items, and a CSS rule that targets them matching. For example, this page:
<DOCTYPE HTML>
<head>
<title>I'm the about page</title>
<style type="text/css">
.about .about,
.index .index,
.services .services,
.features .features {
font-weight: bold;
}
</style>
</head>
<body class="<?php echo basename(__FILE__, ".php"); ?>">
This is a menu:
<ul>
<li>Home</li>
<li>About </li>
<li>Services </li>
<li>Features</li>
</ul>
</body>
...is pretty light on dynamic code, but should achieve the objective; if you save it as "about.php", then the About link will be bold, but if you save it as "services.php", then the Services link will be bold, etc.
If your code structure suits it, you might be able to simply hardcode the page's body class in the page's template file, rather than using any dynamic code for it. This approach effectively gives you a way of moving the "logic" for the menu system out of the menu code, which will always remain the same for every page, and up to a higher level.
As an added bonus, you can now use pure CSS to target other things based on the page you're on. For example, you could turn all the h1 elements on the index.php page red just using more CSS:
.index h1 { color: red; }
You can do it from simple if and PHP page / basename() function..
<!--Menu Starts-->
<div class="menu">
<div id="smoothmenu" class="ddsmoothmenu">
<ul>
<li><a href="index.php" <?php if (basename($_SERVER['PHP_SELF']) == "index.php") { ?> class="selected" <?php } ?>>Home</a></li>
<li><a href="about.php" <?php if (basename($_SERVER['PHP_SELF']) == "about.php") { ?> class="selected" <?php } ?>>About</a> </li>
<li><a href="services.php" <?php if (basename($_SERVER['PHP_SELF']) == "services.php") { ?> class="selected" <?php } ?>>Services</a> </li>
<li><a href="features.php" <?php if (basename($_SERVER['PHP_SELF']) == "features.php") { ?> class="selected" <?php } ?>>Features</a></li>
</ul>
</div>
</div>
Sorry for my bad English, however may be it could help. You can use jQuery for this task. For this you need to match the page url to the anchor of menu and then add class selected to it. for example the jQuery code would be
jQuery('[href='+currentURL+']').addClass('selected');
<?php $activesidebar[$currentsidebar]="id=isactive";?>
<div class="span3">
<div class="well sidebar-nav hidden-phone">
<ul class="nav nav-list">
<li class="nav-header" <?php echo $activesidebar[1] ?>>Marketing Services</li>
<li>Marketing Technology</li>
<li>Generate More Sales</li>
<li>Direct Email Marketing</li>
<li class="nav-header" <?php echo $activesidebar[2] ?>>Advertising Services</li>
<li>Traditional Medias</li>
<li>Online & Social Medias</li>
<li>Media Planing & Purchasing</li>
<li class="nav-header" <?php echo $activesidebar[3] ?>>Technology Services</li>
<li>Managed Websites</li>
<li>Managed Web Servers</li>
<li>Managed Databases</li>
<li class="nav-header" <?php echo $activesidebar[4] ?>>About Us</li>
<li>Contact Us</li>
</ul>
</div>
This is added to the current page I want to add this on.
<?php $currentsidebar =2; include('module-sidebar-navigation.php');?>
I had programmed this menu individually on each page, but to make my website dynamic I used one file and use php includes to load the file. I can get the menu to highlight on the current page assigning an id="isactive", how can I assign id="notactive" to the other 3 menu items that are not active on that page. Is there an else or elseif I have to include?
Reset the array before setting your preferred index.
for ($i = 0; $i < 4; $i++) {
$activesidebar[$i] = "class=\"noactive\"";
}
Notes:
PHP array indices start from 0, not 1.
ID's are meant to be unique (ie, there cannot be 2 of the same ID). Use a classname instade.