473,385 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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_content" 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_content" 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|truncate: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...
A12345B12345C12345D12345E12345

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 3211
Atli
5,058 Expert 4TB
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
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
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...
32
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...
63
by: Aaron Ackerman | last post by:
What is the sytax for exiting a for loop in C#?
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.