php set a menu from an array for loop display - php

$array=array('menu1','menu2','menu3','menu4','menu5','menu6','menu7','menu8','menu9','menu10');
I storied some menu names into an array, Then output some like
in diffirent page, the menu always show 5 items and current page is always 2nd.
And if the current page is in this case, it will loop display menu1, menu2, menu3 after menu10.
foreach($array as $k=>$r){
if($r==$current){
$n=$k;//count current memu position
}
}
echo '<ul>';
foreach($d as $k=>$r){
if($n>1&&$n<7){//normal situation like first image
if($k==($n-1)){
echo '<li><a>'.$r.'</a></li>';
}else if($k==$n){
echo '<li class="current">'.$r.'</li>';
}else{
echo '<li><a>'.$r.'</a></li>';
}
}else{
//How to do in the case of the second image?
}
}
echo '</ul>';

I think you would also like the menu look like this when the current selection is menu1:
menu10
menu1 [selected]
menu2
menu3
menu4
Here is the algorithm. It looks confusing but I am sure you can figure it out.
$array=array('menu1','menu2','menu3','menu4','menu5','menu6',
'menu7','menu8','menu9','menu10');
$current = 'menu1';
$startIndex = array_search($current, $array)-1;
$total = count($array);
$result = array();
for($index = $startIndex ; $index <$startIndex+5; $index++){
$result[] = $array[($index+$total)%$total]; //using % operator
}
print_r($result);

Try this:
if($k==($n-1)){
echo '<li><a>'.$r.'</a></li>';
}else if($k==$n){
echo '<li class="current">'.$r.'</li>';
}else{
if($k >= count($d)-1)
echo ''<li><a>'.$d[$k-count($d)+1].'</a></li>';
else
echo ''<li><a>'.$r.'</a></li>';
}
This actually should work for both cases, so you can remove the big if statement and put this as the body of your foreach.

Related

Insert static code block into a php "while loop" after "N" loops.

I have a php while loop, but I want to insert a "static" code block into it after the third item in the loop and then continue the loop after the static content. Can someone help me update my code to accomplish the "desired outcome" below?
My Code
$x = 0;
while($x <= 5) {
$contentItems = $relatedArray[$rand_keys[$x]];
$contentItems = explode('|', $contentItems);
echo '
<p><img class="faux" src="'.$contentItems[4].'"></p>
';
$x++;
}
Desired Outcome
Loop Content
Loop Content
Loop Content
**STATIC CONTENT**
Loop Content
Loop Content
Loop Content
Just add an if conditional inside of your while loop that checks against the index:
$x = 0;
while($x <= 5) {
// Will only trigger halfway through the loop
if ($x == 3) {
echo '**STATIC CONTENT**';
}
$contentItems = $relatedArray[$rand_keys[$x]];
$contentItems = explode('|', $contentItems);
echo '<p><img class="faux" src="'.$contentItems[4].'"></p>';
$x++;
}
Note that this will still output the original item at this position. If you want to replace it instead, you'll want to wrap everything related to $contentItems inside of an else conditional :)

count within foreach doesn't work in my case

foreach ($dom->find('.post h1 a') as $checkIfResultTrue2) {
$totalSearchResult = $checkIfResultTrue2->href;
if(count($totalSearchResult) > 1){
echo "more than 1";
}
}
why this didn't print more than 1? I tried .length but realised that is JS. I tried sizeof and count, no luck. When I echo $totalSearchResult, there are more than 1 links there.. hmm, what is the problem?
Because you overwrite value of $totalSearchResult as string. If $totalSearchResult is an array, you should write to it as follow:
$totalSearchResult = array();
foreach ($dom->find('.post h1 a') as $checkIfResultTrue2) {
$totalSearchResult[] = $checkIfResultTrue2->href;
...
}
You are overwriting one variable. Take that variable as array, and add every elements in that array. It should be,
$totalSearchResult=array();
foreach ($dom->find('.post h1 a') as $checkIfResultTrue2) {
array_push($totalSearchResult, $checkIfResultTrue2->href); <---
if(count($totalSearchResult) > 1){
echo "more than 1";
}
}

Count results inside foreach then store that value in a variable to be used elsewhere

How do you count the results inside a foreach then store that value in a variable to be used on another foreach.
Example. This foreach returns 5 items
foreach ($xml->items as $item) {
echo "$item->name";
echo "$item->address";
}
Now I want that the foreach above be counted and stored in say $totalitems and be used on another foreach. This second foreach also counts its results and store in $totalitems2. Something like this:
foreach ($xml->items as $item) { //Same source but they will be displayed separately based on a condition.
echo "$item->name";
echo "$item->address";
if_blah_blah_meet_condition_then_break;
}
So basically what I want here is to restrict the total number of items being displayed on both foreach combined. $totalitems and $totalitems2 should have the sum of 8. 8 is the number I want limit the items returned. Doesn't matter if the other foreach has more items than the other. 3 and 5. 4 and 4. 6 and 2. Etc.
How can I achieve this? Please advice.
Just use the simple iterator ++ methods. When you are on the second foreach, watch for when $i passes the number that you want to stop it.
Code:
$i = 0;
foreach ($xml->items as $item) {
echo "$item->name";
echo "$item->address";
$i++;
}
foreach ($xml->items as $item) {
echo "$item->name";
echo "$item->address";
$i++;
if ($i > 5) { // or whatever the number is
break;
}
}
$totalItems = count($xml->items);
foreach ($xml->items as $item) {
echo "$item->name";
echo "$item->address";
}
Just use count($xml->items) and that value in the condition, inside the loop.
It seems your array is stored in xml->items therefor, you only would have to save it in another variable if you want to store it.
foreach ($xml->items as $cont=>$item) {
echo "$item->name";
echo "$item->address";
if($cont<8){
$totalItems_one[] = $item;
}
}
//whatever changes you do to $xml->items
foreach ($xml->items as $cont=>$item) {
echo "$item->name";
echo "$item->address";
if($cont<8){
$totalItems_two[] = $item;
}
}
Like this you have the two new arrays totalItems_one and totalItems_two and you can get the number of items just doing count($totalItems_one) or count($totalItems_two) whenever you want.

foreach loop - if greater than count or else

I have a foreach loop in php.
When the loop is greater than 2 items I would like to display some text instead of the loop. Is this possible?
For example: A loop of 2 or less items shows= item 1, item2
The loop of more items shows the text = Mulitple items.
The example code for indication:
$count++;
foreach($attValConfig as $attValConfigSingle) {
if ($attValConfigSingle["frontend_label"] == "LABELTEXT") {
echo ('<div class="attributes_row">Text</div>');
foreach($attValConfigSingle['values'] as $attValConfigSingleVal) {
if ($count++ > 2) { echo 'SomeNewText'; }
else echo "<option>"list of items"</option>";
I think you want to break in your if statement:
if ($count++ > 2) {
echo 'SomeNewText';
break;
}
You can count the array before looping:
if(count($attValConfigSingle['values']) > 2) {
// More than 2 items
echo "Lots of things";
} else {
// 2 items or less
foreach($attValConfigSingle['values'] as $value) {
// ...
}
}
Edit:
Maybe I didn't understand you correctly. If you want the text:
item 1, item 2, other items...
Then you need to use break to break out of your loop:
foreach($attValConfigSingle['values'] as $attValConfigSingleVal) {
if ($count++ > 2) {
echo 'SomeNewText';
break;
} else {
echo "<option>"list of items"</option>";
}
}
Just declare a variable in which you store your text built during the foreach loops. When you're out of it, print this string if count is less than 3, your other text otherwise.

If Statement inside or outside

I'm trying to figure out if I need to do this inside the for loop or outside the for loop but I want to check to see its empty or not first.
echo "<ul>";
for($x = 0; $x <= (count($quotesArray)-1); $x++)
{
echo "<li>".stripslashes($quotesArray[$x]->quote)."</li>";
}
echo "</ul>";
There is something simpler that checking during loop - it filters all the values and is called array_filter() function:
$quotesArray = array_filter($quotesArray);
echo "<ul>";
foreach($quotesArray as $quote){
echo "<li>".stripslashes($quote)."</li>";
};
echo "</ul>";
The above assumes that $quotesArray contains strings (or elements that work correctly in string context) and you do not want only the elements that are evaluated as false when converted to boolean (see more about converting to boolean).
Additionally you can simplify your code further:
$quotesArray = array_filter($quotesArray);
$quotesArray = array_map('stripslashes', $quotesArray);
echo '<ul><li>'.implode('</li><li>', $quotesArray).'</li></ul>';
if you know $quotesArray contains at least one element.
EDIT:
Short version, that also checks whether the list should be generated (in other words: whether array contains at least one element after processing):
$quotesArray = array_map('stripslashes', array_filter($quotesArray));
if (!empty($quotesArray)) {
echo '<ul><li>'.implode('</li><li>', $quotesArray).'</li></ul>';
};
It needs to be outside the loop because if it is empty, and you don't generate any list items, then you have no list, so you should not generate the ul start and end tags either (since a list with no list items is invalid).
well if you dont wan the list at all then you should do it before the echoing of the first <ul>
if(count($quotesArray) > 0){
//Do your echos and loops in here
}
You can just run both checks.
if($quotesArray){
echo '<ul>';
foreach ($quotesArray as $quote) {
if ($quote) {
echo '<li>' . stripslashes($quote->quote) . '</li>';
}
}
echo '</ul>';
}
When you say you want to check if it's empty, do you mean the entire $quotesArray or one of the values within it?
If you mean you want to check if a value within the array is empty, you could consider this approach:
echo '<ul>';
foreach ($quotesArray as $quote) {
if ($quote) {
echo '<li>' . stripslashes($quote) . '</li>';
}
}
echo '</ul>';

Categories