Limit number of sub_menu items to 4 - php

I am trying to limit the number of sub_menu items (li) to maximum of 4. I'm no php developer but gave it a go with some code as provided below.
This is the existing code, which will just keep displaying it all, no limit set right now.
if (count($sub_menu_array)) {
echo '<nav id="sub-nav"><ul>';
foreach ($sub_menu_array as $sub_menu_row) {
// print_r($sub_menu_row);
echo '<li>'.strtoupper($sub_menu_row['categoryName']).'</li>';
}
echo '</ul></nav>';
} else {
echo '<nav id="sub-nav"><ul><li></li></ul></nav>';
}
Here is what I tried but it ended up displaying nothing instead.
if (count($sub_menu_array)) {
echo '<nav id="sub-nav"><ul>';
$i = 0;
foreach ($sub_menu_array as $sub_menu_row => $v) {
// print_r($sub_menu_row);
echo '<li>'.strtoupper($sub_menu_row['categoryName']).'</li>';
if (++$i == 3) break;
}
echo '</ul></nav>';
} else {
echo '<nav id="sub-nav"><ul><li></li></ul></nav>';
}

Syntax of PHP foreach statement has two different variants
foreach (array_expression as $value){
statement
}
foreach (array_expression as $key => $value)
statement
When you chanched code from
foreach ($sub_menu_array as $sub_menu_row) {
to
foreach ($sub_menu_array as $sub_menu_row => $v) {
You also changed values what appropriated to $sub_menu_row. For example:
$sub_menu_array = array ('a','b');
In the first variant at first iteration
$sub_menu_row=='a'
and in second variant
$sub_menu_row==0 #array's element key
$v=='a' #value
There are two solutions
Simply remove '=> $v'
Change $sub_menu_row to $v inside foreach statement

You have changed foreach, you should now use $v as a value.
if (count($sub_menu_array)) {
echo '<nav id="sub-nav"><ul>';
$i = 0;
foreach ($sub_menu_array as $sub_menu_row => $v) {
// print_r($sub_menu_row);
echo '<li>'.strtoupper($v['categoryName']).'</li>';
if (++$i == 3) break;
}
echo '</ul></nav>';
} else {
echo '<nav id="sub-nav"><ul><li></li></ul></nav>';
}

This should be enough for the menu part:
echo '<nav id="sub-nav"><ul>';
$i = 0;
foreach ($sub_menu_array as $sub_menu_row => $v) {
if($i < 4) {
echo '<li>'.strtoupper($sub_menu_row['categoryName']).'</li>';
}
$i++;
}
echo '</ul></nav>';

Insert this line before displaying the menu
$sliced_sub_menu_array = array_slice($sub_menu_array, 0, 4);
and then
foreach ($sliced_sub_menu_array as $sub_menu_row => $v) {
// displaying here
}

Use this code .
<?php
if (count($sub_menu_array)) {
echo '<nav id="sub-nav"><ul>';
$i = 0;
foreach ($sub_menu_array as $sub_menu_row => $v) {
// print_r($sub_menu_row);
echo '<li>'.strtoupper($sub_menu_row['categoryName']).'</li>';
if ($i == 3)
{
break;
}
$i++;
}
echo '</ul></nav>';
} else {
echo '<nav id="sub-nav"><ul><li></li></ul></nav>';
}
?>

Related

I want to echo something only on the first time foreach runs

I want to echo something only on the first time foreach() runs!
let's say if I have an array $i
$i = array(............);
and I want to use it in foreach like...
foreach($i as $key => $value) {
echo '<h1>'.$key.'</h1>';
echo '<p>'.$value.'</p>';
}
while this foreach runs the first time only i want to add class="show" to the h1 tag.
How to do that?
Update code:
$i=['hello','world'];
foreach($i as $key => $value) {
$class= ($key == 0)?'show':'';
echo '<h1 class="'.$class.'">'.$key.'</h1>';
echo '<p>'.$value.'</p>';
}
Output:
<div>
<h1 class="show">0</h1><p>hello</p>
<h1 class="">1</h1><p>world</p>
</div>
You'll need to add a var to track that
$first = 0;
foreach($i as $key => $value) {
$show=($first == 0) ? 'h1 class=show' : 'h1';
echo '<$show>'.$key.'</h1>';
echo '<p>'.$value.'</p>';
$first++;
}
There are many ways to do it, but this might be easier to read
if you have single dimension array then you can use
$i=['test1','test2','test3'];
foreach($i as $key => $value) {
if($key == 0){
echo $key;
echo '<br>';
echo '<p>'.$value.'</p>';
}
}
if you have an associative array
$i=array('a'=>'test1','b'=>'test2','c'=>'test3');
$j = 0;
foreach($i as $key => $value) {
if($j == 0){
echo $key;
echo '<br>';
echo '<p>'.$value.'</p>';
}
$j++;
}
output will be

XML: Show two child nodes with the same name in php

I have a problem with showing my xml into a php page. I have an xml that looks like this (obviously its only a part of it because it really to long to post it all).
It all goes well until it cames to the "genre", I have two values for it and I don't know how to show them at the same time.
<movie>
<id>4441</id>
<title>Rudderless</title>
<title_long>Rudderless (2014)</title_long>
<year>2014</year>
<rating>7.5</rating>
<runtime>105</runtime>
<genres>
<genre>Comedy</genre>
<genre>Drama</genre>
</genres>
</movie>
(an important thing to notice is that not every movie will have two genre, sometimes there is only one and sometime two or three)
This is my code right now
$genere = array();
foreach ($xml->data->movies->movie->genres as $row) {
foreach ($row->children() as $key => $val) {
if ($key == "genre") {
$genere[] = $val;
}
}
}
//and then I'll print
for ($i = 0; $i < 20 ; $i++) {
echo "<div class=text><b>" . $genere[$i] . "</b></div>";
}
When I'm doing this it will print only for the first item of the array, and the others just give me a "Notice: Undefined offset: 1 in /path/ on line 53"
I've tried to follow some guides but it was a failure
What am I doing wrong?
/--Edit--/
<?php
$xml = simplexml_load_file("https://yts.to/api/v2/list_movies.xml")
$titolo = array();
$locandina = array();
$anno = array();
$durata = array();
$genere = array();
foreach ($xml->data->movies->movie as $element)
{
foreach($element->children() as $key => $val)
{
$chiave = $key;
$valore = $val;
if ($key == "title")
{
$titolo[] = $val ;
}
if ($key == "medium_cover_image")
{
$locandina[] = $val ;
}
if ($key == "year")
{
$anno[] = $val ;
}
if ($key == "runtime")
{
$durata[] = $val;
}
}
}
foreach ($xml->data->movies->movie->genres as $row)
{
foreach($row->children() as $key => $val)
{
if ($key == "genre")
{
$genere[] = $val;
}
}
}
var_dump($genere);
for ($i=0 ; $i<20 ; $i++)
{
echo "<div class=totbox>";
echo "<div class=box><img src=" . $locandina[$i] . "></div>";
echo "<div class=text><b>" . $titolo[$i] . "</b> - " . $anno[$i] . "</div>";
echo "<div class=text><b>" . $genere[$i] . "</b></div>";
echo "<div class=text><b> Durata:" . $durata[$i] . "</b></div>";
echo "</div>";
}
?>
UPDATED - Code fixed. Please, try now.
<?php
$xml = simplexml_load_file("https://yts.to/api/v2/list_movies.xml")
$titolo = array();
$locandina = array();
$anno = array();
$durata = array();
$genere = array();
$i = 0;
foreach ($xml->data->movies->movie as $element) {
foreach ($element->children() as $key => $val) {
$chiave = $key;
$valore = $val;
if ($key == "title") {
$titolo[] = $val;
}
if ($key == "medium_cover_image") {
$locandina[] = $val;
}
if ($key == "year") {
$anno[] = $val;
}
if ($key == "runtime") {
$durata[] = $val;
}
if ($key == "genres") {
for($g = 0; $g < count($xml->data->movies->movie[$i]->genres->genre); $g++) {
$genere[$i][] = $xml->data->movies->movie[$i]->genres->genre[$g];
}
}
}
$i++;
}
for ($i = 0; $i < count($titolo); $i++) {
echo "<div class=totbox>";
if (isset($locandina[$i])) {
echo "<div class=box><img src=" . $locandina[$i] . "></div>";
}
echo "<div class=text><b>" . $titolo[$i] . "</b> - " . $anno[$i] . "</div>";
foreach ($genere[$i] as $genResult) {
echo "<div class=text><b>" . $genResult . "</b></div>";
}
echo "<div class=text><b> Durata:" . $durata[$i] . "</b></div>";
echo "</div>";
}
I hope this helps!
Look on my code, based on yours. PHP file:
<?php
$xml = new SimpleXMLElement(file_get_contents('test.xml'));
$genere = array();
foreach ($xml->genres as $row) {
foreach ($row->children() as $key => $val) {
if ($key == "genre") {
$genere[] = $val;
}
}
}
//and then I'll print
foreach ($genere as $genre) {
echo "<div class=text><b>" . $genre . "</b></div>";
}
XML file:
<movie>
<id>4441</id>
<title>Rudderless</title>
<title_long>Rudderless (2014)</title_long>
<year>2014</year>
<rating>7.5</rating>
<runtime>105</runtime>
<genres>
<genre>Comedy</genre>
<genre>Drama</genre>
</genres>
</movie>
Its work fine, test it.
Based on your full xml file, look on this:
<genres>
<genre>Horror</genre>
</genres>
You have only one genre on first position, so its work fine. You need to loop on movie tag first, not genres. So put your genre tag inside movie foreach.
Like:
foreach($element->children() as $key => $val)
{
if ($key == "genres")
{
// your loop, based on $val variable!
}

Count duplicate values in PHP array

my problem is, i have this script
foreach ($arr1 as $k => $val) {
if (in_array($val, $arr2)) {
echo 'Obsazeno <br>';
} else {
echo $val . "<BR>";
}
}
Where $arr1 is generated array of days and $arr2 is array from mysql results. Actually it works fine. If one of days from $arr1 is in database, it will echo "Obsazeno" instead of day value.
But i need small upgrade. Script check if $val is in $arr2 and i need small counting contidion that will do this:
If $val is in $arr2 once - it will echo $val.
If $val is in $arr2 twice - it will echo $val.
But if $val is in $arr2 thrice - it will echo "Obsazeno".
I hope you understand my question.
I quest, it is possible by array_count_values. I try it by myself but no success.
You can achieve this with array_count_values.
$values = array_count_values($arr2);
foreach ($arr1 as $k => $val) {
if (array_key_exists($val, $values) && $values[$val] >= 3) {
echo 'Obsazeno <br>';
} else {
echo $val . '<br>';
}
}
try
$i=0;
foreach($arr1 as $k=>$val) {
if(in_array($val,$arr2)) {
$i++;
if($i == 3) {
echo 'Obsazeno <br>';
}
else {
echo $val."<BR>";
}
}
}
$data = array();
foreach($arr1 as $k=>$val)
{
if(!array_key_exists($val,$data)){
$data[$val] = 0;
}
if(in_array($val,$arr2))
{
$data[$val]++;
if($data[$val]==3){
echo 'Obsazeno <br>';
}else{
echo $val."<BR>";
}
}
else
{
echo $val."<BR>";
}
}

display php foreach values in div

I have
foreach ($a as $key => $value) {
echo "<DIV id='container'>
<DIV id='middle'>$value</DIV>
</DIV>";
//echo "<br />$key:$value<br />\n";
}
which displays result one below other,
like
1234
5678
2010-05-20
5678
1590
2010-05-19
but i want it in a table like structure like
1234 5678 2010-05-20
5678 1590 2010-05-19
how can i do that?
You could keep track with for example modulus if you should begin a new row or not.
EDITED after reformatting of the question
I think what you need is simply something on the line of:
echo '<table><tr>';
$n = 0;
foreach ($a as $key => $value)
{
if ($n==3)
{
$n = 0;
echo '</tr><tr>';
}
echo '<td>'.$value.'</td>';
$n++;
}
echo '</tr></table>';
You can do like:
$counter = 0;
foreach ($a as $key => $value) {
$counter++;
if (($counter % 3) === 0)
{
echo "<DIV id='container'>
<DIV id='middle'>$value</DIV>
</DIV>";
}
else
{
echo "<DIV id='container' style='float:left;'>
<DIV id='middle'>$value</DIV>
</DIV>";
}
}
Kind of what nico was saying:
foreach ($a as $key => $value)
{
echo '<div style="width:33%;float:left">'.$value.'</div>';
}
Or what don was saying:
$i = 0;
$open = false;
echo '<table>';
foreach( $a as $key => $value )
{
if( 0 == $i % 3 ) {
echo '<tr>';
$open = true;
}
echo "<td>$value</td>";
if( 2 == $i % 3 ) {
echo '</tr>';
$open = false;
}
$i++;
}
if( $open )
echo '</tr>';
echo '</table>';
Simply use like this,
foreach ($a as $key => $value) {
echo "<DIV id='container' style='display:inline'>
<DIV id='middle' style='display:inline'>$value</DIV>
</DIV>";
//echo "<br />$key:$value<br />\n";
}
add the break statement depending on your second line.
By default, div's don't float next to each other. You could give all the div's, except every third one float: left in the style. However it's okay to use table's if your displaying a table.
If you can derive from the value of $key wether to start a new row or not you can do something like:
echo '<table><tr>';
foreach ($a as $key => $value) {
if($key == 'somevalue'){
//start a new row if the $key allow
echo '</tr><tr>';
}
echo '<td>'.$value.'</td>';
}
echo '</tr></table>';
If you can't derive from the value of $key wether to start a new row or not, use a counter, like so:
echo '<table><tr>';
$cnt = 0;
foreach ($a as $key => $value) {
if($cnt % 3 == 0){
//start a new row if this row contians 3 cells
echo '</tr><tr>';
}
echo '<td>'.$value.'</td>';
$cnt++;
}
echo '</tr></table>';

Looping through an array of arrays, changing output on a given line(s)

This is what im using to loop through an array of arrays.
$csvpre = explode("###", $data);
$i = 0;
$bgc = 0;
foreach ( $csvpre AS $key => $value){
$info = explode("%%", $value);
$i++;
if($i == "1"){
echo "<tr bgcolor=#efefef><td></td>";
foreach ( $info as $key => $value ){ echo "<td>$value</td>"; }
echo "</tr>";
} else {
if($bgc=1) { $bgcgo = "bgcolor=\"#b9b9b9\"" ;} else { $bgcgo = "bgcolor=\"#d6d6d6\""; }
echo "<tr $bgcgo><td></td>";
foreach ( $info as $key => $value ){ echo "<td>$value</td>"; }
echo "</tr>";
$bgc++;
}
}
How can i add an if/elseif statement to the last foreach, so that the output changes on a given line of the array.
Say i want <td>$value</td> for all unless specified, but on line 30, i want <textarea>$value</textarea>
You mean like this:
<?php
.......
echo "<tr $bgcgo><td></td>";
$j = 0; //you need a counter
foreach ( $info as $key => $value ) {
$j++;
if ($j != 30) {
echo "<td>$value</td>";
} else {
echo "<textarea>$value</textarea>";
}
}
echo "</tr>";

Categories