I have a php code that display all categories of my store, the only problem with this code is give me this structure: ul > li > ul > li. I want to be able to change in css for example the Parent Category to have the color red and Child Category to have the color blue.
I try to use something like:
.sidebar.sidebar-additional ul li:first-child {color: red;}
but is not okay, both texts Parent Category and Child Category have the same red color.
What I can do be able to select both texts separately?
function categoryLoop($id){
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$categories = $objectManager->create('Magento\Catalog\Model\Category')->load($id);
if($categories->hasChildren()){
echo '<ul>';
$subcategories = explode(',', $categories->getChildren());
foreach ($subcategories as $category) {
$subcategory = $objectManager->create('Magento\Catalog\Model\Category')->load($category);
echo '<li>';
echo $subcategory->getName();
echo "</li>";
if($subcategory->hasChildren()){ categoryLoop($category); }
}
echo "</ul>";
}
}
The html output is:
<ul>
<li>Parent Category</li>
<ul>
<li>Child Category 1</li>
<li>Child Category 2</li>
<li>Child Category 3</li>
</ul>
</li>
</ul>
Thank you
There are two ways to get this done:
1. CSS
Style the parent as red and the children to blue.
.container > ul > li {
color: blue;
}
.container > ul > li > ul > li {
color: red;
}
2. PHP
Output the parent with some predefined class, and children also with a different class:
<?php
$output = "";
foreach($groups as $group){
$output .= "<ul class='parent'>"; // <---- add class here
foreach($parent as $key => $category){
$children = get_children($key);
$output .= "<li>" . $category . "</li>";
if(count($children) > 0){
$output .= "<ul class='child'>"; // <---- add class here
foreach($children as $key2 => category2){
$output .= "<li>" . $category2 . "</li>";
}
$output .= "</ul>";
}
}
}
Then with CSS:
.parent {
color: blue;
}
.child {
color: red;
}
The code you provided should change like this:
function categoryLoop($id, $is_sub = false){
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$categories = $objectManager->create('Magento\Catalog\Model\Category')->load($id);
if($categories->hasChildren()){
echo '<ul' . (($is_sub) ? ' class="category_children"' : '') . '>';
$subcategories = explode(',', $categories->getChildren());
foreach ($subcategories as $category) {
$subcategory = $objectManager->create('Magento\Catalog\Model\Category')->load($category);
echo '<li>';
echo $subcategory->getName();
echo "</li>";
if($subcategory->hasChildren()){ categoryLoop($category, true); }
}
echo "</ul>";
}
}
And then:
.sidebar.sidebar-additional ul li {
color: blue;
}
.sidebar.sidebar-additional ul.category_children li {
color: red;
}
You can set all li to red, and then target the li you want to be blue, by specifically selecting those that are nested within two ul
ul li {
color: red;
}
ul ul li {
color: blue;
}
<ul>
<li>Parent Category
<ul>
<li>Child Category 1</li>
<li>Child Category 2</li>
<li>Child Category 3</li>
</ul>
</li>
</ul>
you can use the direct child selector ul > li which selects only the direct child and not the lower childs
ul > li {
/*the first level*/
}
ul > li > ul > li{
/*the second level*/
}
Alternatively:
function categoryLoop($id){
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$categories = $objectManager->create('Magento\Catalog\Model\Category')->load($id);
if($categories->hasChildren()){
echo '<ul class="second_level">';
$subcategories = explode(',', $categories->getChildren());
foreach ($subcategories as $category) {
$subcategory = $objectManager->create('Magento\Catalog\Model\Category')->load($category);
echo '<li>';
echo $subcategory->getName();
echo "</li>";
if($subcategory->hasChildren()){ categoryLoop($category); }
}
echo "</ul>";
}
}
CSS
ul > li {color: red}
ul.second_level > li {color: blue}
Related
I have this recursive function:
function recursive($arrays, $out) {
if (is_array($arrays)){
//$out .= "<ul>";
foreach($arrays as $parent => $data) {
//if parent is empty
if ($parent === '') {
$out = recursive($data, $out);
continue;
}
$out .= "<li>";
if (is_array($data)){
$out .= ' <a href="#" class="dropdown-toggle active" data-toggle="dropdown">';$out .= $parent;
}
else
{ $directory =explode("#", $parent)[0];$paname = explode("#", $parent[1]; $link = 'http://127.0.0.1/ocos/index.php?module='.$directory.'/'.$paname;
$out .= '<a href="'.$link.'">'; $out .= $data;
}
if (is_array($data)){
$out .= '<b class="caret"></b></a>';
}
else
{
$out .= "</a>";
}
if (is_array($data)){
$out .= "<ul class='dropdown-menu'>";
$out = recursive($data, $out);
$out .= "</ul></li>";
}
else
{
$out .= "</li>";
}
}
}
return $out;
}
In the menu bar I have a dropdown menu with parent calledoperation and with a child page called aro and aro have another child page called aro document list so all I wanna is that when I'm in aro document list make the parent operation active with green color to make the user know in which part of the menu he is in. Thank u in advance
I think you're making it too complicated. Is the below what you need? When you click on a,b or c the parent will get a green background.
$("li").on("click", function() {
var grandparent = $(this).parent("ul").parent("li");
if (grandparent.length > 0) {
grandparent.css("background", "green");
}
});
ul {
padding: 0;
list-style: none;
background: white;
}
li {
cursor: pointer;
}
.operations {
display: flex;
}
.operations li {
width: 25px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="operations">
<li>1</li>
<li>2</li>
<li>3
<ul class="aro">
<li>a</li>
<li>b</li>
<li>c</li>
</ul>
</li>
</ul>
I want make a pagination for my articles..
Here is class:
<?php
class Mynews {
public $conn;
public function __construct() {
$this->conn = mysqli_connect("localhost", "root", "", "mynews");
}
public function readAllarticles() {
$sql = "SELECT * FROM articles WHERE status='publish'";
$query = mysqli_query($this->conn, $sql);
return mysqli_fetch_all($query, MYSQLI_ASSOC);
}
}
$obj = new Mynews;
?>
display post: <?php
include('includes/crud.php');
foreach ($obj->readAllarticles() as $art) {
extract($art);
?>
<h3><?php echo $title; ?></h3>
<p>
<?php
$post = explode(" ", $content);
$slice = array_slice($post, 0, 10);
echo implode(" ", $slice), '...';
?>
</p>
<?php
}
?>
Now i want a function my class to make a pagination... and sorry for my bad eng.. :(
You use it:
<?php
class Pagination{
function Paginate($values,$per_page){
$total_values = count($values);
if(isset($_GET['page'])){
$current_page = $_GET['page'];
}else{
$current_page = 1;
}
$counts = ceil($total_values / $per_page);
$param1 = ($current_page - 1) * $per_page;
$this->data = array_slice($values,$param1,$per_page);
for($x=1; $x<= $counts; $x++){
$numbers[] = $x;
}
return $numbers;
}
function fetchResult(){
$resultsValues = $this->data;
return $resultsValues;
}
}
// Sample Usage
$pag = new Pagination();
$data = array("Hello","Rex","Prosper","Adrivan","Hehe");
$numbers = $pag->Paginate($data,2);
$result = $pag->fetchResult();
foreach($result as $r){
echo '<div>'.$r.'</div>';
}
foreach($numbers as $num){
echo ''.$num.'';
}
?>
I use this and it works for me
PHP
function pagination_one($webpage, $total_pages,$page){
// Maximum number of links per page. If exceeded, google style pagination is generated
$max_links = 6;
$h=1;
if($page>$max_links){
$h=(($h+$page)-$max_links);
}
if($page>=1){
$max_links = $max_links+($page-1);
}
if($max_links>$total_pages){
$max_links=$total_pages+1;
}
echo '<div class="page_numbers">
<ul>';
if($page>"1"){
echo '<li class="current">First</li>
<li class="current">Prev</li> ';
}
if($total_pages!=1){
for ($i=$h;$i<$max_links;$i++){
if($i==$page){
echo '<li><a class="current">'.$i.'</a></li>';
}
else{
echo '<li>'.$i.' </li>';
}
}
}
if(($page >="1")&&($page!=$total_pages)){
echo '<li class="current">Next</li>
<li class="current">Last</li>';
}
echo '</ul> </div>';
}
function pagination_one($webpage, $total_pages,$page){
// Maximum number of links per page. If exceeded, google style pagination is generated
$max_links = 6;
$h=1;
if($page>$max_links){
$h=(($h+$page)-$max_links);
}
if($page>=1){
$max_links = $max_links+($page-1);
}
if($max_links>$total_pages){
$max_links=$total_pages+1;
}
echo '<div class="page_numbers">
<ul>';
if($page>"1"){
echo '<li class="current">First</li>
<li class="current">Prev</li> ';
}
if($total_pages!=1){
for ($i=$h;$i<$max_links;$i++){
if($i==$page){
echo '<li><a class="current">'.$i.'</a></li>';
}
else{
echo '<li>'.$i.' </li>';
}
}
}
if(($page >="1")&&($page!=$total_pages)){
echo '<li class="current">Next</li>
<li class="current">Last</li>';
}
echo '</ul> </div>';
}
// get the pagenum. If it doesn't exist, set it to 1
if(isset($_GET['pagenum']) ? $page = $_GET['pagenum']:$page = 1);
// set the number of entries to appear on the page
$entries_per_page = 6;
// total pages is rounded up to nearest integer
$total_pages = ceil($getresult/$entries_per_page);
// offset is used by SQL query in the LIMIT
$offset = (($page * $entries_per_page) - $entries_per_page);
$sql = "SELECT * FROM articles WHERE status='publish' LIMIT $offset,$entries_per_page";
// do your query results
pagination_one('articles.php', $total_pages,$page);
and CSS
.page_numbers {
width:100%;
background:#fff9f0;
overflow:hidden;
position:relative;
padding:50px 0;
}
.page_numbers ul, .pagenums ul {
clear:left;
float:left;
list-style:none;
margin:0;
padding:0;
position:relative;
left:50%;
text-align:center;
}
.page_numbers ul li,.pagenums ul li {
display:block;
float:left;
list-style:none;
margin:1px;
padding:0;
position:relative;
right:50%;
background: #a8a189;
width:25px;
}
.page_numbers ul li a, .pagenums ul li a {
display:block;
background: #fff;
border: 1px solid #a8a189;
padding:3px 6px;
text-decoration: none;
color: #7a7564;
font:bold 11px arial, verdana,sans-serif;
}
.page_numbers li.current,
.pagenums li.current{
width:50px;
}
.page_numbers a.current, .page_numbers li a:hover,
.pagenums a.current, .pagenums li a:hover {
background: #a8a189;
color: #fff;
}
I created three columns and three rows using li; I want to display one member in every li order by id.
<li> // Style: margin-bottom:10px;
Show first member result here...
</li>
<li class="middle_li"> // Style: margin:0px 10px 10px;
Show second member result here...
</li>
<li> // Style: margin-bottom:10px;
Show third member result here...
</li>
<li> // Style: margin-bottom:10px;
Show first member result here...
</li>
<li class="middle_li"> // Style: margin:0px 10px 10px;
Show second member result here...
</li>
<li> // Style: margin-bottom:10px;
Show third member result here...
</li>
<li> // Style: margin-bottom:10px;
Show first member result here...
</li>
<li class="middle_li"> // Style: margin:0px 10px 10px;
Show second member result here...
</li>
<li> // Style: margin-bottom:10px;
Show third member result here...
</li>
My PHP code:
<?php
require 'initialize.inc.php'; // Classes: connection & members
echo '<ul>';
$members = $member->display_members();
foreach ($members as $member)
{
$html_ouput = '<li>';
$html_ouput .= 'Show member result here...';
$html_ouput .= '</li>';
echo $html_output;
}
echo '</ul>';
?>
This php code display nine results. Three columns three rows but how can I give every second li row a class middle? Any modification in my php code? I don't want to use javascript.
You need to concatenate the string to output
try this:
<?php
require 'initialize.inc.php'; // Classes: connection & members
echo '<ul>';
$members = $member->display_members();
$i = 0;
foreach ($members as $member)
{
$html_ouput = '';
$i++;
if($i == 1)
$html_ouput .= '<li>';
else{
$i = 0;
$html_ouput .= '<li class="middle">';
}
$html_ouput .= 'Show member result here...';
$html_ouput .= '</li>';
echo $html_output;
}
echo '</ul>';
?>
What about this:
<?php
require 'initialize.inc.php'; // Classes: connection & members
echo '<ul>';
$members = $member->display_members();
$i = 0;
foreach ($members as $member)
{
$html_ouput = ($i % 3 == 1 ? '<li class="middle">' : '<li>');
$html_ouput .= 'Show member result here...';
$html_ouput .= '</li>';
echo $html_output;
$i++;
}
echo '</ul>';
?>
Add a counter in the for each loop and test to see if the counter is divisible by 2... If it is then its an even number. If it is an even number add the class="middle".
i'm developing a php project in which i'm getting the list from the database and i need to display this data in html in 3 columns and 10 rows and add a button "more" in the end
<?php foreach ($items as $item) { ?>
<ul>
<li><a href="<?php echo $item['href']; ?>" >
<?php echo $item['name']; ?></a></li>
</ul>
<?php } ?>
I want it to be displayed as-
item1 item2 item3
item4 item5 item6
.
.
.
.
Maximum 10 rows
last row will be
item item "more" - only if there are more items in the list
"more" is just a link which will take me to some other page
Thanks for any help.
Add display:inline to li and use pseudo class to break it
ul{
margin:0
}
li{
display:inline
}
li:nth-child(3n):after {
content:"\A";
white-space:pre;
}
Script (To display more link)
$('#moreli').toggle($("li").size() > 6);
This enables the more link after 6 list items, you can change the count in the script.
Note: Reduce the list items to check the effect
DEMO UPDATED
<style>
.row {
width:100%;
float:left;
}
.item {
width:100px;
float:left;
}
</style>
<?php
//*** your array of items
$items = array("item1", "item2", "item3", "item4","item5", "item6", "item7", "item8", "item9", "item10");
$numItems = sizeof($items);
//*** max number of rows
$maxRows = 10;
$maxItems = 3 * $maxRows;
echo '<div class="row">';
for ($i=0; $i<$numItems;$i++) {
echo '<div class="item">'.$items[$i].'</div>';
if (($i+1) % 3 == 0) {
//*** if divisible by 3, close row
echo '</div><div class="row">';
}
if ($i == $numItems) {
//*** last item reached, close div
echo '</div>';
}
if ($i+1 >= $maxItems ) {
//*** max 10 row, add more button.
echo '</div><input type="submit" value="Add More">';
return;
}
}
?>
Try to use this:
<?php
$count=0;
foreach ($items as $item) { ?>
<ul class="col3">
<li>
<?php if($count > 9) { ?>
More</li></ul>
<?php break;
} else{ ?>
<a href="<?php echo $item['href']; ?>" >
<?php echo $item['name'];
$count=$count+1;
?></a>
<?php } ?>
</li>
</ul>
<?php } ?>
and following css for it
.col3
{
margin-left:-3%;
}
.cols3 li
{
width:30%;
margin-left:3%;
display:inline-block;
vertical-align:top;
}
.cols3 li a
{
display:block;
}
i am working on one project, where i need to display products from mysql table using php.
My design is like that, 5 products display in one row and so on.
So if there is 7 products in table, it should be 2 rows.
first row 5 products
second row 2 products
I am using <UL> and <LI> to display product and row like
<UL>
<li>first</li>
<li>2nd</li>
<li>3rd</li>
<li>4th</li>
<li>5th</li>
</ul>
<UL>
<li>6th</li>
<li>7th</li>
</ul>
I am wondering how to use foreach loop to get code like above.
If i use forloop with 5 increment i am unable to get products from table and display their name.
Thanks
echo '<ul>';
foreach ($yourResultSet as $index=>$data) {
echo '<li>' . YourOutput . '</li>';
if ($index % 5 == 0) {
echo "</ul><ul>";
}
}
echo '</ul>';
<ul>
<?php $i = 0; ?>
<?php while($row = mysql_fetch_array($result)): /* Used to loop through a resultset*/ ?>
echo "<li>".$row['data'].</li>";
<?php
$i++; // Increment by one every loop
if($i == 5){ // We've hit the fifth iteration
echo '</ul><ul>'; // Close the UL and open a new UL
$i = 0; // Set $i back to 0 and continue looping
}
endwhile;
?>
</ul>
Try that.
your best way of achieving this is to have your <li>'s have a specific width so that five of them fit in the parent container and that the following two or however many you have will drop to the next line.
<style>
#container {
width: 350px;
background: red;
padding:0;
margin: 0;
}
#list li
{
display: inline-block;
width: 50px;
height: 50px;
list-style-type: none;
background:blue;
margin: 2px;
padding: 0;
}
</style>
<div id="container">
<ul id="list">
<?php
while($row = mysql_fetch_array($result))
{
echo '<li>' . $row['data'] . '</li>';
}
?>
</ul>
</div>
You'll need to play with the sizes a bit but everything should work, and it you have 11 products this should do 2 rows of 5 and a third row of 1 product.