Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split results into different sized variables...

Status
Not open for further replies.

youradds

Programmer
Jun 27, 2001
817
GB
Hi. I've got a bit of a dilema here. Basically, I need to split up a 100 or so results into smaller sections, defined by the user. For example;

1,5,7,6,4,6,3,5,4,2,6,8 .. etc

I'm trying to work out how I would accomplish something like this. I've been racking my brain for 2 days now...and none of the ideas I've had seems to work.

Does anyone have any ideas on how I could do this? Code examples would be great, but even just suggestions on how to do it are appreciated :)

Cheers

Andy
 
how do u want to arrange them? use the split command to get an each induvidual section...

Known is handfull, Unknown is worldfull
 
Well, I just grab them with;

SELECT * FROM GuestList WHERE Username = '$USER_COOKIE'

Then I use a while() loop to go through all the results.

For example, I have the following tata grabbed;

ID Title
1 Test
2 Test2
3 Test3
4 Test4
5 Test5
6 Test6
7 Test7
8 Test8
9 Test9
10 Test10
11 Test11
12 Test12
13 Test13

An I, for example, need to split them into 4,5 and 4 (in that order), so it would end up something like;

1 Test
2 Test2
3 Test3
4 Test4

5 Test5
6 Test6
7 Test7
8 Test8
9 Test9

10 Test10
11 Test11
12 Test12
13 Test13

Does that make more sense?

Cheers

Andy
 
oops sorry didnt notcie ur post.
try this:
SELECT * FROM GuestList WHERE Username = '$USER_COOKIE limit 0,4'

this will give the first 4 results.


then try this:
SELECT * FROM GuestList WHERE Username = '$USER_COOKIE limit 5,5'

next 5 results and so on....

Note: I assume u r using MySql...


Known is handfull, Unknown is worldfull
 
Looped solution:
Do what vbkris says but define an array for the segments:
Code:
$segment = array(4,5,4);
$offset = 0;
foreach($segment as $value){
   $SQL = "SELECT * FROM myTable WHERE myCondition LIMIT $offset,$value);
   # mysql execution etc. here
   ....
   # calculate next offset
   $offset = $offset == 0 ? $value+1 : $offset+$value;
}
 

Hello DRJ478,

Could you please explain your statement below for me? I tried to understand it but I am a little dense. ;-)

$offset = $offset == 0 ? $value+1 : $offset+$value;

Thanks!
 
I am using the ternary operator:
condition ? value_true : value_false;

If the offset equals 0 ? offset should be the number of rows +1 : if not zero then just add the number of rows selected;

The ternary operator is just like an if / else statement in compressed form.

I could avoid this by starting the offset at 1. However, I chose 0 because that's the first element in PHP arrays etc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top