Jump to content

[SOLVED] Displaying the nth result


Sydcomebak

Recommended Posts

Well, what exactly does $desired_num represent and how does the user/code pick it?

Best bet would be to use that as criteria in your SQL statement. Such as

SELECT * FROM NamesTbl WHERE id = SOMEID

or somesuch where clause. Then you'll only need to retrieve 1 record instead of ALL records. Then you can simply display the results of your query and be done.

Link to comment
Share on other sites

OK, let's say that I have an array of 28 names and I want to display them in columns.

 

I want to display NameLast[1] then NameLast[4] then Namelast[7] in the first row of a table.

 

The easiest way to do that would be to do a for loop

 

for x = 1 to 3 do begin

 

echo NameLast[3x-2]

 

end

 

I hope that's clearer.

 

Link to comment
Share on other sites

re-re-edited, sorry :P

 

<?php
$desired_num=4;
$result = mysql_query("SELECT * 
FROM NamesTbl") or die(mysql_error());
$x=0;
while($row = mysql_fetch_array($result))  {
$i=0;
if(($i % $desired_num) == 0) echo $row['NameLast'];
$i++;
}
//edited
?>

Try that..edited

Link to comment
Share on other sites

it will. what you need to do is put an if() into the while() loop to tell it to jump to the row for the next iteration of the loop. keep in mind you haven't been totally clear - do you want to display them in three columns, or do you want to display them in columns of three? your 1, 4, 7 example only works if it keeps going (let's assume 28 names, the way you did):

 

1 4 7 10 13 16 19 22 25 28
2 5 8 11 14 17 20 23 26
3 6 9 12 15 18 21 24 27

Link to comment
Share on other sites

alright, but to be honest, you're wasting a fair bit of resources by not taking advantage of PHP's MySQL functions.  it's roughly the same amount of coding work to get it working within the loop.  i was going to write an example to work with, but i didn't know whether you'd be displaying in a set of 3 columns or columns of 3.

Link to comment
Share on other sites

Oh sorry I didn't totally understand, well I gave you the concept, you can figure out the easy bits.

 

or what you want to do i guess then you need 2 nested for loops to do this

start

1

2

3

4

middle

1 4 7 10

2 5 8

3

4

end

1 4 7 10

2 5 8 11

3 6 9 12

4 9 12 13

 

Using increments of one in your first loop, and increments of 3 in your nested loop.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.