473,748 Members | 3,107 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:

[PHP]
<ul>
//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}
{/foreach}

//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}
[/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.

(example)
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...

A1B2C3D4E5

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
Thanks
Nov 13 '07 #1
2 3228
Atli
5,058 Recognized Expert Expert
Hi.

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.

Moderator
Nov 13 '07 #2
recordlovelife
31 New Member
Hi.

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.

Moderator
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

5
12416
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: ------------
32
4151
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...
63
3153
by: Aaron Ackerman | last post by:
What is the sytax for exiting a for loop in C#?
0
8987
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9366
jinu1996
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...
0
8239
agi2029
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...
1
6793
isladogs
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...
0
6073
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();...
0
4597
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...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2211
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.