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";
}
}
Related
Given an empty array $items = array();
Why should I use the following code (which I've seen used before):
if (count($items) > 0) {
foreach ($items as $item) // do stuff here
}
instead of just
foreach ($items as $item) // do stuff here
If count($items) === 0 the loop won't execute anyway??
You don't generally need to do the check. I see lots of code with that check, but it's often unnecessary, and I think it's usually due to ignorance on the part of the programmer. A similar pattern that I frequently see is after database queries:
if (mysqli_num_rows($stmt) > 0) {
while ($row = mysqli_fetch_assoc($stmt)) {
...
}
}
This test is also unnecessary; if no rows were found, the first fetch will return false, and the loop will stop.
The only case where it's useful is if you want to tell the user that there were no results, instead of just displaying an empty table. So you might do:
if (count($items) > 0) {
echo "<table>";
foreach ($items as $item) {
// display table row
}
echo "</table>";
} else {
echo "<b>No data found!</b>";
}
It actually depends on what you want to do. As the comments have pointed out, you may want an empty array to be a special case, so you'd like to handle that case differently. Otherwise, no warning will be thrown if you execute foreach on an empty array, you just won't get any results. A typical check you should execute is if $items is an array at all, or cast $items into an array anyway to avoid getting that warning. If you did that and $items would be generally be converted into an array of one value, i.e. the value $items had at that point.
e.g.
$items = 2;
foreach((array)$items as $item) {
print $item; //will print 2 alright
}
or
if(is_array($items)) {
foreach($items as $item) {
print $item;
}
}
else {
// do something here
}
$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.
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.
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.
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>';