for loop counter $i=1;$i<=3;$i++ in wordpress is creating duplicates

I am using the below php code to add a class to each post from a specific category. The counter adds the number to class but each of the is adding 3 times. here is my mark up
	 <ul class="ul">	 
	 <?php query_posts('showposts=3&cat=3&orderby=rand'); if (have_posts()) : while (have_posts()) : the_post(); ?>
	 <?php for ( $i=1; $i<=3; $i++ ) { ?>
	 <li class="li<?php echo $i ?>"><a href="<?php $postid = $wp_query->post->ID; echo get_post_meta($postid, '_wpb_nsl_text', true);  ?>" title="<?php the_title_attribute(); ?>"><?php print $post->post_title; ?></a>
	 <a href="<?php $postid = $wp_query->post->ID; echo get_post_meta($postid, '_wpb_nsl_text', true);  ?>"><?php the_post_thumbnail('blog-post-image'); ?></a>
		<?php } ?>
	<?php endwhile; else : endif; ?>
	<?php wp_reset_query(); ?>

Open in new window

The result is like below and as you can see its creating 3 li's for each post instead of only one
<ul class="ul>
 <li class="li1">post 1 </li>
 <li class="li2">post 1 </li>
 <li class="li3">post 1 </li>
 <li class="li1">post 2 </li>
 <li class="li2">post 2 </li>
 <li class="li3">post 2 </li>
 <li class="li1">post 3 </li>
 <li class="li2">post 3 </li>
 <li class="li3">post 3 </li>

Open in new window

What I am looking is
<ul class="ul>
 <li class="li1">post 1 </li>
 <li class="li2">post 2 </li>
 <li class="li3">post 3 </li>

Open in new window

What am I missing?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tom BeckCommented:
You have a for loop inside of a while loop. The while loop runs three times because you requested three posts. You have set the for loop to run three times also. 3 x 3 = 9

You want something more along these lines (not tested):
 <ul class="ul">
// The Query
query_posts( 'showposts=3&cat=3&orderby=rand' );

// The Loop
$i = 1;
while ( have_posts() ) : the_post();
    $postid = $wp_query->post->ID;
    $postmeta = get_post_meta($postid, '_wpb_nsl_text', true);
    echo '<li class="li' . $i . '"><a href="' .  $postmeta  .  '" title="' . the_title_attribute() . '">' .  $post->post_title . '</a>
	 <a href="' . $postmeta . '">' . the_post_thumbnail('blog-post-image') . '</a>

// Reset Query

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
niceoneishereAuthor Commented:
Thanks :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.