Forum Moderators: coopster

Message Too Old, No Replies

need to work out all the different combinations of pages.

little script needed

         

londrum

3:54 pm on Jan 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hoping someone will be able to help me with a little problem. otherwise i have got weeks and weeks and weeks and weeks of work to look forward to...

i want to write a sitemap.xml file for one of my pages. the url will always have two query strings on it. and each string could be one of 300 different possibilities.

so the url might look like this...

example.com/page.html?var1=red&var2=apple

i have worked out that there are approxiametly 100,000 different combinations of the two variables. which would take ages to write out in a sitemap. eg.

var1=red&var2=apple
var1=red&var2=orange
var1=red&var2=pear
var1=green&var2=apple
var1=green&var2=orange
var1=green&var2=pear

does anyone know a way to do a little php script that could print out all the different combinations?

henry0

4:02 pm on Jan 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Intersting question
I tried a G with "php permutations"
and found This [webmasterworld.com]

let us know if it could do the trick

Philosopher

4:09 pm on Jan 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So you're querying a db to get the results?

If so, the logic is simple enough..

Step 1 - Query the db to get the first result set that contains all the possible variations of var1. You'll likely need to use the DISTINCT command to get only 1 version of each variation.

Step 2 - Create a while loop to iterate through the result set returned from step 1

Step 3 - Inside the while loop created in Step 2, you will need a 2nd db query to grab the result set that contains all the possible variations for var 2 (again using the DISTINCT command I'm betting).

Step 4 - Create a 2nd while loop inside the first while loop to iterate through the 2nd result set returned.

Step 5 - Print out the data

Basically, you want to grab a version of var 1, then cycle through all the possible versions of var 2, then move to the next version of var 1, rinse..repeat

at least that's how I'd do it :)

[edited by: Philosopher at 4:11 pm (utc) on Jan. 20, 2009]

londrum

7:12 pm on Jan 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



duh, why didn't i think of that.
that's how i did it in the end and it was easy, thanks