How to control number of html elements through php? - php

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!

Related

How to display php variable values in html <li> [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 months ago.
Improve this question
I have got multiple php variables that I want to display in html <ul list. The variables being displayed but not how I want it to be. It seems like one <li is inside of <ul correctly but, the other one is outside of <ul in it's own block and it's own <li even when I am displaying all of the variables inside of single <ul. Also the styling is only being applied to only the first <li element and not to the others.
Here is my html code
<div>
<ul class="product-info-list">
<li> <?php echo $dimensions; ?> </li>
<li> <?php echo $materials; ?> </li>
<p> <?php echo $description; ?> </p>
</ul>
</div>
The source code from page source is
<div class="product-info-details">
<div>
<ul class="product-info-list">
<li> 18" x 10" x 6".</li>
</ul></li>
<li> Burlap,Nylon,Polyester </li>
<p> <p>With the Fusion Backpack strapped on, every trek is an adventure - even a bus ride to work </p>
</ul>
</div>
and the output
The html structure that is getting created after doing what I am doing is,
The page element cant be where you have it. The <p> can not be between the </li> and </ul>. I can not find a suitable authoritative reference right now, but I have made the same mistake myself.
So I have shifted it inside the <li>..</li>
Try this.
<div>
<ul class="product-info-list">
<li> <?php echo $dimensions; ?> </li>
<li> <?php echo $materials; ?>
<p> <?php echo $description; ?> </p></li>
</ul>
</div>
Or maybe you intended this. I have changed the <p> to a list element
<div>
<ul class="product-info-list">
<li> <?php echo $dimensions; ?> </li>
<li> <?php echo $materials; ?> </li>
<li> <?php echo $description; ?> </li>
</ul>
</div>

Simple-html-dom list ordering nested without class

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;
}
}

Wordpress Shortcode not working with WP Codex command and PHP

Below is a php issue I am having, it works in other ways but doesn't work the way I wish too.
Basically I wish to add the WP login and logout commands to the the A-HREF links, but the closing short code doesn't seem to be taken effect and I would like to know why and hopefully fix this problem.
<?php
echo do_shortcode('[not-level-visitors]') .
'<div id="SignUp">
<ul>
<li><img src="http://dev.universitycompare.com/wp-content/themes/blue-and-grey/images/icons/user_icon.png" alt="User Icon My Account University Compare" />
My Account
</li>
<li>
Log Out
</li>
</ul>
</div>' .
do_shortcode('[/not-level-visitors]');
?>
Basically the above code is kind of working, but I just need the closing shortcode to work as it appears in my html and is not being recognised - I have created the above code from this below snippet that I am already using that works completely:
<?php
echo do_shortcode('[level-visitors]
<div id="SignUp">
<ul>
<li>
Sign Up
</li>
<li>
Login
</li>
</ul>
</div>
[/level-visitors]');
?>
The main difference I spotted is the single call vs multiple calls to do_shortcode, I assume it's failing to match the regex.
Maybe this will work:
echo do_shortcode('[not-level-visitors]
<div id="SignUp">
<ul>
<li><img src="http://dev.universitycompare.com/wp-content/themes/blue-and-grey/images/icons/user_icon.png" alt="User Icon My Account University Compare" />
My Account
</li>
<li>
Log Out
</li>
</ul>
</div>[/not-level-visitors]');
It looks like you need to remove the extra opening/closing parentheses and extra do_shortcode call.
<?php
echo do_shortcode('[not-level-visitors]' .
'<div id="SignUp">
<ul>
<li><img src="http://dev.universitycompare.com/wp-content/themes/blue-and-grey/images/icons/user_icon.png" alt="User Icon My Account University Compare" />
My Account
</li>
<li>
Log Out
</li>
</ul>
</div>' .
'[/not-level-visitors]');
?>

How to have the class="selected" depending on what the current page/url is

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');

Wordpress - Adding classes to wp_list_pages

Does anyone know how to edit/change Wordpress's wp_list_pages function in order to add classes to the ul and li items?
I'm trying to implement the new version of jquery.treeview which requires <li class="expandable"> and <ul style="display: none;"> on expandable lists and child ul's.
I've been messing around with this but it aint working too good in that it applies the 'expandable' class to all li's:
$pages = wp_list_pages('title_li=&echo=0' );
$pages = preg_replace('/class="/','class="expandable ', $pages); //note space on end of replacement string
//output
echo $pages;
And here is what the outputted html should look like:
<ul class="treeview" id="tree">
<li>Home</li>
<li class="expandable">Expand 1
<ul style="display: none;">
<li class="expandable">Expand 2_1
<ul style="display: none;">
<li>Expanded 3_1</li>
<li>Expanded 3_2</li>
<li>Expanded 3_3</li>
</ul>
</li>
<li class="expandable"><a href="#" >Expand 2_2</a>
<ul style="display: none;">
<li>Expanded 4_1</li>
<li>Expanded 4_2</li>
<li>Expanded 4_3</li>
</ul>
</li>
</ul>
Hope this makes sense and any help greatly appreciated, S.
I guess you are trying to activate a tree view on the page items. As this would require JavaScript you can simply add the class using JavaScript before initializing the tree view:
$("#tree li").addClass("expandable");
$("#tree").treeview();
If you also want to hide all ul elements you can use jQuery, too (not sure about the correct syntax):
$("#tree ul").hide();
Maybe this Plugin (Classy wp-List) helps. I haven't tried it yet but it says it will let you define a class for each page in the backend.
good luck.

Categories