473,883 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do i control a "foreach" loop when it is inside a "while" loop? (uses smarty)

31 New Member
So I am trying to display a title, date, and content of a wordpress blog. Word press provides nice drop in functions to get the job done with simple names like "the_title" , and the "the_conten t" But on the homepage of a site, i wanted to truncate the content to like the first 75 characters and then put "..." (a perfect use of the smarty "truncate" modifier) and then give the visitor a link to read the whole article. But since "the_conten t" is a function, i can't really control the output without going in and tweeking the function. So i said, Ill just query the post db on my own, load the first 5 results in an array variable, and loop through them in the presentation, no sweat... here's what i got:

//Wordpress (WP) checking for posts, then starts a "while" loop
{php}if ( have_posts() ) : while ( have_posts() ) : the_post(); {/php}

<!-- If the post is in the category we want to exclude, we simply pass to the next post. -->

//Tell WP to do the first 5 posts
{php} if (in_category('5 ')) continue; {/php}

<div class="post">

//Display the link for the full blog post, using the title of the post as the name
<li><h2><a href="{php} the_permalink() ; {/php}">{php} the_title();{/php}</a></h2></li>

//Display the date posted
<small>{php} the_time('F jS, Y'); {/php}</small><br />

//now HERE is where i dropped in my foreach loop to pull out the first 5
//contents and truncate them and add the "..."
{foreach from=$users item="query"}
{$query->post_content|t runcate:75:"... ":false}

//Made another "Read All" link at the add of content summary
<a href="{php} the_permalink() ; {/php}">Read All</a>
<hr />

</div> <!-- closes the first div box -->
{php} endwhile; else: {/php}
<p>Sorry, no posts matched your criteria.</p>
{php} endif; {/php}

I thought this would be gravy, and at first glance it looked like it worked. Until i took a closer look and saw what happened. WP code works fine, it's provided in their documentation. But my foreach loop cycles through the first five contents every time WP's while loop displays a post. I wanted it to do the first content in the first post, second content in the second post, and so on. To illustrate.

WP function got this (A,B,C,D,E)
My query got these results array(1,2,3,4,5 )

Now i want this to be displayed...


But this is what I get...
A12345B12345C12 345D12345E12345

I understand why this is happening: everytime the WP while loop gets to my query, my smarty code is saying to loop through all my results. So how do i get my foreach loop to output the first index and wait til the next time it's called. My only other way I can think of is making a counter ($i) and actually typing in something like: $array[$i], and at the end of the while loop adding: $i++; but that makes it seem like im missing a bigger picture.

Let me know
Nov 13 '07 #1
2 3239
5,058 Recognized Expert Expert

You posted this in the PHP Articles section. I have moved it over to the PHP Forums for you.

Please try to avoid posting in the Article sections in the future.

Nov 13 '07 #2
31 New Member

You posted this in the PHP Articles section. I have moved it over to the PHP Forums for you.

Please try to avoid posting in the Article sections in the future.

sorry about the misktake, any advice?
Nov 14 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Gustavo Randich | last post by:
Hello, I'm writing an automatic SQL parser and translator from Informix to DB2. Now I'm faced with one of the most difficult things to translate, the "foreach execute procedure" functionality provided by Informix. This is the problem: the execution of the translated SQL leaves the rows in the temp table correctly but raises error SQL0480N. It's very simple to try it: ------------
by: James Curran | last post by:
I'd like to make the following proposal for a new feature for the C# language. I have no connection with the C# team at Microsoft. I'm posting it here to gather input to refine it, in an "open Source" manner, and in an attempt to build a ground-swell of support to convince the folks at Microsoft to add it. Proposal: "first:" "last:" sections in a "foreach" block The problem: The foreach statement allows iterating over all the...
by: Aaron Ackerman | last post by:
What is the sytax for exiting a for loop in C#?
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.