This is my current array that displays list items in a unordered list.
function get_bottle_colors() {
if(empty($_GET['cap_id'])) return false;
$constructor_img = get_post_meta($_GET['cap_id'], 'product_constructor_image', true);
if(is_array($constructor_img) && count($constructor_img)>0 && !empty($constructor_img[0]['title'])){
$output = '<label>Bottle Color</label><ul>';
foreach ($constructor_img as $key => $image) {
if(empty($image['image'])) continue;
$output .= '<li><a href="'.$image['image'].'" data-width="'.$img_size[0].'" data-height="'.$img_size[1].'"';
$output .= '</a></li>';
}
$output .= '</ul>';
}else{
$output = '<label>Bottle Color</label><ul></ul>';
}
echo $output;
die();
}
In total there will be up to 16 list items generated by this. I need each list item to have its own class eg: list class="red", list class="green", etc. Any idea how i go about achieving this?
Found the solution thanks to Anant. Had to declare the class array like below.
function get_bottle_colors() {
if(empty($_GET['cap_id'])) return false;
$constructor_img = get_post_meta($_GET['cap_id'], 'product_constructor_image', true);
if(is_array($constructor_img) && count($constructor_img)>0 && !empty($constructor_img[0]['title'])){
$output = '<label>Bottle Color</label><ul>';
$i = 0;
$class_array = array("a","b","c","d","e","f","g","h","i","j","k","l","n","m","n","o","p");
foreach ($constructor_img as $key => $image) {
if(empty($image['image'])) continue;
$category = 9;
$img_size = getimagesize($image['image']);
$output .= '<li class= "'.$class_array[$i].'"><a href="'.$image['image'].'" data-width="'.$img_size[0].'"
data-height="'.$img_size[1].'"';
$output .= 'data-id="'.$_GET['cap_id'].'_'.$key.'" data-part="#constructor-bottles" class="sub-caps">'.$image['title'];
$output .= '</a></li>';
$i++;
}
$output .= '</ul>';
}else{
$output = '
<label>Bottle Color</label><ul></ul>'; } echo $output; die(); }
Related
I have a php plugin that explodes one line into several lines.
$graph_lines = explode( ";", $content );
$output= '';
$output .= '<ul class="lpd-bullet-list';
if($image){
$output .= ' lpd-bl-custom-icon';
}
if($style){
$output .= ' '.esc_attr($style);
}
$output .= '">';
foreach ($graph_lines as $line) {
if($line){
$output .= '<li>';
$output .= $image;
$output .= $line;
$output .= ';';
$output .= '</li>';
}
}
$output .= '</ul>';
echo $output;
After each line in the end it set ; Thats OK, but how to set . for last element? Is it possible?
Mey be i need to use end() function or another?
Help me please!
I am not very sure about your output but yes you can use end function like this:
$last_index = end(array_keys($graph_lines));
foreach ($graph_lines as $index => $line) {
if ($index == $last_index) {
// last index
} else {
// perform other tasks
}
}
Alternatively, you can use like this:
foreach ($graph_lines as $line) {
if($line){
$output .= '<li>';
$output .= $image;
$output .= $line;
$output .= ';';
if (next($graph_lines)==false) $output .= '.';//not sure where you put it.
$output .= '</li>';
}
}
You can get the number of line sizeof($graph_lines) and create a counter in your foreach.
Exemple :
$numberofline = sizeof($graph_lines);
$i=0;
foreach ($graph_lines as $line) {
if($line){
$i++;
$endofline=";";
if($i==$numberofline)
{
$endofline=".";
}
$output .= '<li>';
$output .= $image;
$output .= $line;
$output .= $endofline;
$output .= '</li>';
}
}
$content = "a;b;c;d;s;r;t;g";
$content = rtrim($content, ";");
$content = ltrim($content, ";");
$graph_lines = explode(";", $content);
$output = '';
$output .= '<ul class="lpd-bullet-list">';
$last = count($graph_lines) - 1;
$i = 0;
foreach ($graph_lines as $line) {
if ($line) {
$output .= '<li>';
$output .= $line;
$output .= ($last == $i)? '.' : ";";
$output .= '</li>';
$i++;
}
}
$output .= '</ul>';
echo $output;
You can use an shorthand IF to check if the current line is matching the last element in the array.
foreach ($graph_lines as $line) {
if($line){
$output .= '<li>';
$output .= $image;
$output .= $line;
$output .= ( $line === end ( $graph_lines ) ) ? '.' : ';';
$output .= '</li>';
}
}
An other way is to set a counter and check if the counter is equal to the count() of $graph_lines.
$counter = 1;
$total_items = count ( $graph_lines );
foreach ( $graph_lines as $line ) {
// do your stuff.
$output .= ( $counter === $total_items ) ? '.' : ';';
$counter++;
}
Count the array. and check that with the $i.
$count = count($graph_lines);
$i = 0;
foreach ($graph_lines as $line) {
if ($line) {
if ($i == $count) {
$output .= '<li>';
$output .= $image;
$output .= $line;
$output .= '.';
$output .= '</li>';
} else {
$output .= '<li>';
$output .= $image;
$output .= $line;
$output .= ';';
$output .= '</li>';
}
}
$i++;
}
I want to generate a multilevel hierarchical select option that is indented right as we move from parent to child. I tried using recursion in PHP(CI) for this but can't generate what I needed.
This is the function I tried to generate the select option.
<?php
$category_list = $this->helper_model->get_category();
/* $category list contains
[{"id":"1","name":"Information Technology","parent_id":"0"},
{"id":"2","name":"Programming","parent_id":"1"},
{"id":"3","name":"Civil","parent_id":"5"},
{"id":"4","name":"Networking","parent_id":"1"},
{"id":"5","name":"Engineering","parent_id":"0"},
{"id":"6","name":"Electrical","parent_id":"5"},
{"id":"11","name":"Hardware","parent_id":"1"},
{"id":"12","name":"Car Driver","parent_id":"13"},
{"id":"13","name":"Driver","parent_id":"0"},
{"id":"14","name":"java","parent_id":"2"},
{"id":"15","name":"javascript","parent_id":"2"}]
*/
$select_option = $this->multilevel_select($category_list);
echo '<select name = "subCategory">
<option>Select Category</option>' . $select_option;
function multilevel_select($array,$parent_id = 0,$parents = array()) {
static $i=0;
if($parent_id==0)
{
foreach ($array as $element) {
if (($element['parent_id'] != 0) && !in_array($element['parent_id'],$parents)) {
$parents[] = $element['parent_id'];
}
}
}
$menu_html = '';
foreach($array as $element){
if($element['parent_id']==$parent_id){
$menu_html .= '<option>';
for($j=0; $j<$i; $j++) {
$menu_html .= '—';
}
$menu_html .= $element['name'].'</option>';
if(in_array($element['id'], $parents)){
$i++;
$menu_html .= $this->multilevel_select($array, $element['id'], $parents);
}
}
}
$i=0;
return $menu_html;
}
?>
Can anyone help me get through this?
I have tried to create a function to handle text from the database to be publish with automatic formating.
If there is a \n : it should be converted to <p>...</p>
If there is a - : it should also add the list tags
My problem is that I cant really figure out how to add the <ul> and </ul> tags.
function nl2p($string){
$string = explode("\n", $string);
$paragraphs = '';
foreach ($string as $line) {
if (trim($line)) {
if (substr($line,0,1) == '-'){
$paragraphs .= '<li>' . substr($line,1) . '</li>'."\r\n";
} else {
$paragraphs .= '<p>' . $line . '</p>'."\r\n";
}
}
}
return $paragraphs;
}
Use a variable ($ul in my example):
function nl2p($string){
$string = explode("\n", $string);
$paragraphs = '';
$ul = 0;
foreach ($string as $line) {
if (trim($line)) {
if (substr($line,0,1) == '-'){
if($ul == 0){
$paragraphs .= "<ul>\r\n";
$ul = 1;
}
$paragraphs .= '<li>' . substr($line,1) . '</li>'."\r\n";
} else {
if($ul == 1){
$paragraphs .= "</ul>\r\n";
$ul = 0;
}
$paragraphs .= '<p>' . $line . '</p>'."\r\n";
}
}
}
return $paragraphs;
}
Collect all continuous <li> elements with text in a string and then enclose this string in a <ul> and </ul>.
I would search for the first character to be a dash and, if the previous line did not start with a dash, add the <ul> there. Then wrap the line in li tags and the same check at the end - if the next line does not start with a dash, then add an </ul>.
Then, as a default action, wrap the line in a paragraph.
$string = "this is a string.
New line 1.
New line 2.
- List item
- Another list item
Some more lines of text";
function nl2p($input) {
$lines = explode("\r\n",$input);
$return = '';
foreach($lines as $key => $line) {
if(strpos($line,'-') === 0) {
if(array_key_exists($key-1,$lines) AND strpos($lines[$key-1],'-') === FALSE) {
$return .= '<ul>' . "\r\n";
}
$return .= '<li>' . $line . '</li>' . "\r\n";
if(array_key_exists($key+1,$lines) AND strpos($lines[$key+1],'-') !== 0) {
$return .= '</ul>' . "\r\n";
}
continue;
}
$return .= '<p>' . $line . '</p>' . "\r\n";
}
return $return;
}
var_dump(nl2p($string));
/*
<p>this is a string.</p>
<p>New line 1.</p>
<p>New line 2.</p>
<ul>
<li>- List item</li>
<li>- Another list item</li>
</ul>
<p>Some more lines of text</p>
*/
function nl2p($input) {
if(strpos($input, "\n")) {
$slash = explode("\n",$input);
$newPara = '';
foreach($slash as $slashval) {
$slashval = '#'.$slashval;
if(strpos($slashval,"-")) {
$slashval = substr($slashval, 1);
$hypen = explode("-",$slashval);
$newPara .= '<ul>';
foreach($hypen as $hypenval) {
if(!empty($hypenval)) {
$newPara .= '<li>'.$hypenval.'</li>';
}
}
$newPara .= '</ul>';
} else {
$slashval = substr($slashval, 1);
$newPara .= '<p>'.$slashval.'</p>';
}
}
return $newPara;
} else {
$slashval = $input;
if(strpos($slashval,"-")) {
$hypen = explode("-",$slashval);
$newPara .= '<ul>';
foreach($hypen as $cnt => $hypenval) {
if($cnt == 0) {
$start = $hypenval;
} else {
if(!empty($hypenval)) {
$newPara .= '<li>'.$hypenval.'</li>';
}
}
}
$newPara .= '</ul>';
$newPara = $start.$newPara;
} else {
$slashval = '#'.$input;
if(strpos($slashval,"-")) {
$slashval = substr($slashval, 1);
$hypen = explode("-",$slashval);
$newPara .= '<ul>';
foreach($hypen as $hypenval) {
if(!empty($hypenval)) {
$newPara .= '<li>'.$hypenval.'</li>';
}
}
$newPara .= '</ul>';
}
}
return $newPara;
}
return $input;
}
I am trying to make nested categories in Laravel. I have created following models: Category.php, ItemsHelper.php (this one for displaying nested categories).
ItemsHelper.php:
class ItemsHelper {
private $items;
public function __construct($items) {
$this->items = $items;
}
public function htmlList() {
return $this->htmlFromArray($this->itemArray());
}
private function itemArray() {
$result = [];
foreach($this->items as $item) {
if ($item->parent_id == 0) {
$result[$item->title] = $this->itemWithChildren($item);
}
}
return $result;
}
private function childrenOf($item) {
$result = [];
foreach($this->items as $i) {
if ($i->parent_id == $item->id) {
$result[] = $i;
}
}
return $result;
}
private function itemWithChildren($item) {
$result = [];
$children = $this->childrenOf($item);
foreach ($children as $child) {
$result[$child->title] = $this->itemWithChildren($child);
}
return $result;
}
private function htmlFromArray($array) {
$html = '';
foreach($array as $k => $v) {
$html .= "<ul>";
$html .= "<li>" . $k . "</li>";
if(count($v) > 0) {
$html .= $this->htmlFromArray($v);
}
$html .= "</ul>";
}
return $html;
}
}
Then i am printing nested list of data in my index.blade.php:
{{ $itemsHelper->htmlList() }}
However this prints only plain titles and i need to convert them in to a links to my category, e.g. htmlFromArray() method:
$html .= "<li>" . $k . "</li>";
method itemArray():
$result[$item->title] = $this->itemWithChildren($item);
How can i add to this array id of each item?
You should probably change methods itemArray, itemWithChildren and htmlFromArray this way:
private function itemArray()
{
$result = [];
foreach ($this->items as $item) {
if ($item->parent_id == 0) {
$record['title'] = $item->title;
$record['id'] = $item->id;
$record['children'] = $this->itemWithChildren($item);
$result[] = $record;
}
}
return $result;
}
private function itemWithChildren($item)
{
$result = [];
$children = $this->childrenOf($item);
foreach ($children as $child) {
$record['title'] = $child->title;
$record['id'] = $child->id;
$record['children'] = $this->itemWithChildren($child);
$result[] = $record;
}
return $result;
}
private function htmlFromArray($array)
{
$html = '';
foreach ($array as $item) {
$html .= "<ul>";
$html
.=
'<li><a href="/category/' . $item['id'] . '">' . $item['title']
. "</a></li>";
if (count($item['children']) > 0) {
$html .= $this->htmlFromArray($item['children']);
}
$html .= "</ul>";
}
return $html;
}
Now you can store in your arrays id, title and list of element children so you can access iun htmlFromArray all those elements attributes.
I have a function that generates a category menu.
What i'm trying to do is to group two category in one ul.
The problem is almos fixed but i'm getting an empty ul ( see the pic).
How can I fix this ?
function categorie($tab){
global $mysql;
$return = "";
$categorie = $mysql->select(sprintf("SELECT * FROM categorii WHERE tab = '%d'", $tab));
if($mysql->countRows() > 0){
$i = 0;
$return .= "<ul class=\"group\">\n";
foreach ($categorie as $cat) {
if($i % 2 == 0){
$return .= "</ul><ul class=\"group\">";
}
$return .= "\t<li>\n";
$return .= "\t\t<ul>\n";
$return .= "\t\t\t<li class=\"head-list\">".$cat['categorie']."</li>\n";
$return .= $this->subcategorie($cat['categorie']);
$return .= "\t\t</ul>\n";
$return .= "\t</li>\n";
$i++;
}
$return .= "</ul>";
}
return $return;
}
Try this:
function categorie($tab){
global $mysql;
$return = "";
$categorie = $mysql->select(sprintf("SELECT * FROM categorii WHERE tab = '%d'", $tab));
if($mysql->countRows() > 0){
$i = 0;
$return .= "<ul class=\"group\">\n";
foreach ($categorie as $cat) {
if($i % 2 == 0 && !empty($i)){
$return .= "</ul><ul class=\"group\">";
}
$return .= "\t<li>\n";
$return .= "\t\t<ul>\n";
$return .= "\t\t\t<li class=\"head-list\">".$cat['categorie']."</li>\n";
$return .= $this->subcategorie($cat['categorie']);
$return .= "\t\t</ul>\n";
$return .= "\t</li>\n";
$i++;
}
$return .= "</ul>";
}
return $return;
}
just start counting from -1
$i = -1;