I am using WordPress and attempting to nest loops. In the parent loop I want to display regular post and every 3rd post inject a post from the inner loop. The problem is as long as the parent loop has posts then the child loop will spit out its post again which is causing duplicates. Is there a way to only display one post of the child loop at a time and to only show post while it has_posts?
Easy solution, don't nest loops. Make two different queries and a counter, loop the first query and when i%3==0 add one from second query. See if this helps:
$apples = get_posts('post_type=apple');
$oranges = get_posts('post_type=orange');
for ($i=0; i<count($apples); $i++) {
$apple = $apples[$i];
// do something with $apple
// every 3rd apple
if ($i%3 === 0) {
$orange = array_shift($oranges);
// do something with $orange
}
}
At some point you'd have to check if there are enough oranges or not enough apples to print all oranges.
Check docs on get_posts for more info on how to use it.
Related
I want to write a foreach that loops on a wanted certain step so for example if a user send me 2 rooms it just loops twice if the user sends 5 room it loops 5 times and here is what I tried:
$room_count = [1,2,3] //room ids for example
foreach($room_count as $room_counts){
echo 'echo on eachstep'
}
I want to get the requests from $request->all() so I am not sure about saving that in array is a right thing to use for example here or not so I just need to do it in an array for now.
Try this:
for ($i = 0; $i < count($room_count); $i++) {
//your loop body here
}
if you want to loop for certain amount of rooms.
You can't
get the requests from $request->all()
, $request->all() contains only Request parameters like user's input.
I am not able to loop through the same query twice as follow, what am I missing?
$query = "SELECT ..."
while ($a = tep_db_fetch_array($query)) {
while ($a2 = tep_db_fetch_array($query)) {
// stuff
}
}
Thus my question, how to loop through the same query two times one inside the other?
If you know the data is not going to be excessively huge, then you can use get_records_sql() instead. This will return an array, indexed by the first field in the SELECT. You can then do what you want with this array (loop through it multiple times, split, pop, shift, etc)
<?php
for ($i = 1; $i<=$pages_item; $i++) {
echo "<tr><td>Page ".$i."</td></tr>";
}
foreach ($array_item as $item) {
echo "<tr class=link onclick=window.location.href='item.php?
id=".$item[$item_id]."'>";
}
?>
I am creating pages from a MySQL query in PHP with CSS instead of javascript. The page system works.
What I need is to be able to limit the foreach loop, like for every page in the for loop, I want a foreach loop to go through my array at a specific start and end point.
I would like to have it like this
for every page do
for every item start here and ending here do
do magic
increase start here
increase ending here
done
I need to have it display 50 results per page from the foreach loop within the for loop. I've seen array slicing but that will only set a start point and doesn't really do what I want.
I have 3 objects with same variable but rendered with diferent values.
001 - Title one
002 - Title two
003 - Title three
I need to check on another block of page if this first three are the same. The only thing that comes to mind is store this variables in a $_SESSION. So far so good. The problem is that it only stores last value (obviously).
CODE
/* -- block one --*/
session_start();
$_SESSION['lasttitle'] = $item->getTitle;
echo $item->getTitle(); //rendering 1st object
echo $item->getTitle(); //rendering 2nd object
echo $item->getTitle(); //rendering 3rd object
/* -- block two --*/
session_start();
$last3 = $_SESSION['lasttitle'];
if($item->getTitle() != $last3) {
//don't render last 3
}
$_SESSION['lastTitles'][] = $item->getTitle();
$_SESSION['lastTitles'] = array_slice($_SESSION['lastTitles'], -3);
..
if (in_array($item->getTitle(), $_SESSION['lastTitles'])) ..
This stores an array of the last three titles, and checks whether a title is in this array.
Create multidimensional session:
$_SESSION['lasttitle'][$item->id] = $item->getTitle();
$_SESSION['lasttitle'][$item2->id] = $item2->getTitle();
$_SESSION['lasttitle'][$item3->id] = $item3->getTitle();
And check if that element is in array:
$total = 0;
foreach ($items as $item) {
if (isset($_SESSION['lasttitle'][$item->id])) {
$total++;
}
}
if ($total == 3) {
// Do something, it's all 3 titles in session!
}
I know it may be better solution, but I don't understand full algorithm. Also, to compare some arrays, you can use array_diff_assoc()
I think storing data as array in session might help you. Simply gather all titles you need in an array and put it into $_SESSION (instead of one variable). One the next page you will be able to analyze data you have and choose variables you need.
I have a really strange problem with range();
According to docs :
Create an array containing a range of elements
But when I do :
foreach (range(900,950,1) as $art_id){
//ob_start();
//do stuff
//do a lot more stuff
echo $art_id;
//ob_get_clean(); }
or even
$arts_id = range (900, 920);
foreach ($arts_id as $art_id){
//ob_start();
//do stuff
//do a lot more stuff
echo $art_id;
//ob_get_clean(); }
The output is strangly repeating itself in a series like
"900,900,901,900,901,902,900,901,9002,903,900..."
meaning it is comming back to the first ID after each loop.
(1st iteration -> 900
2nd iteration -> 900,901
3rd iteration -> 900,901,902
...)
When I just put a manual array it works perfectly in order and no duplicates :
$arts_id = array(900,901,902,903,904,905,906,907,908,909,910...);
What Am I doing wrong (again ?? )
EDIT I
here is the whole script :
http://pastebin.com/ZHm3ub6n
It is actually a slightly modified version of the slashdot scraping example included in the simplehtmldom script . Nothing special.
It is executed inside WP but OUTSIDE the loop ..
It must be in the rest of your code, because this works fine.
please share more of the script.
It looks like the foreach is nested within a similair foreach,
$arts_id = range (900, 920);
foreach ($arts_id as $art_id){
foreach (range (900,$art_id) as $art_id2){
echo $art_id2."<br/>";
}
}
This produces an output you've described
EDIT
Personally i'd add the the function scraping_slashdot a reset of the variable $ret just in case.
for example:
$ret = array();
Currently the echo of $output is within the loop, which creates an output like the following:
Article 1
Article 1, Article 2
Article 1, Article 2, Article 3
etc.
place echo $output outside the loop, or $ouptut = ''; inside the loop.