xpath expression

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lemonfreshmedia
Forum Commoner
Posts: 26
Joined: Fri Dec 02, 2005 7:14 pm

xpath expression

Post by lemonfreshmedia »

Wondering how to achieve the following..


i have xx number of articles in my xml file

i'd like to divide them by 3
and get them to display in three (fairly) equal columns

so if i had
37

i'd like them to display like this

1. 13 25
... ... ...
12. 24. 36

with the remainder appearing on the last line

37.

sometimes it will be 30 sometimes it may be 35 or even 20.


Any help on this is much appreciated as I haven't been able to figure this one out.

Thanks
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

A simple combination of count and division operator will get you there...
lemonfreshmedia
Forum Commoner
Posts: 26
Joined: Fri Dec 02, 2005 7:14 pm

Could you be alittle more specific?

Post by lemonfreshmedia »

i've found out how to get the remainder but how to apply it across columns is escaping me

Your tip worked, but i'm still at a loss.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Here is a little sample:

if you have articles 1 to 6, you end up with a col1 having 1 and 4, col2 having 2 and 5, col3 having 3 and 6

(offcourse, for your problem you would need to do some magic with division instead ;))

Code: Select all

<xsl:for-each select='articles/article'>

 <xsl:if test="position() mod 3 = 0">
  // first column
 </xsl:if>

 <xsl:if test="position() mod 3 = 1">
  // second column
 </xsl:if>

 <xsl:if test="position() mod 3 = 2">
  // third column
 </xsl:if>

</xsl:for-each>
lemonfreshmedia
Forum Commoner
Posts: 26
Joined: Fri Dec 02, 2005 7:14 pm

here's what I have so far

Post by lemonfreshmedia »

for column 1

Code: Select all

[position() <= floor(last() div 3)]
for column 2

Code: Select all

[(position() > (floor(last() div 3)) ) and (position() <= ((floor(last() div 3)) * 2)) ]
for column 3

Code: Select all

[(position() > ((floor(last() div 3)) * 2)) and (position() <= ((floor(last() div 3)) * 3)) ]
only thing is when i try to get the remainder of number of articles divided by three the only case that indicates anything other than 1 is if the article number ends in '8'.

example
37 mod 3 = 1 one articled added to the first column
38 mod 3 = 2 one article added to the second column
39 mod 3 = 1 so if its tested to see for this number i get 1 again,

how can get this number to behave in a way that will indicate a perfectly formed row?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: here's what I have so far

Post by timvw »

I snipped your code for columns.. You can make it a little more flexible (and save a bit of writing)

rowspercolumn = total / number of rows
for i := 0 to numbers of rows:
get rows where position between i * rowspercolumn and (i+1) * rowspercolumn

lemonfreshmedia wrote: example
37 mod 3 = 1 one articled added to the first column
38 mod 3 = 2 one article added to the second column
39 mod 3 = 1 so if its tested to see for this number i get 1 again
Your example is wrong.

...
36 mod 3 = 0
37 mod 3 = 1
38 mod 3 = 2
39 mod 3 = 0
40 mod 3 = 1
41 mod 3 = 2
...

As you can see there are only 3 outcomes 0, 1 and 2. Now that you know this, you could use it to decide in which column the row belongs.


column1 | column2 | column3
-----------|------------|-----------
row 0 | row 1 | row 2
-----------|------------|------------
row 3 | row 4 | row 5
-----------|------------|------------
....
lemonfreshmedia
Forum Commoner
Posts: 26
Joined: Fri Dec 02, 2005 7:14 pm

39 MOD 3 = 1

Post by lemonfreshmedia »

i've executred that in my code and it returns 1.
unless i'm coding '39 mod 3' wrong .
the only way to return 0 after division is dividing a number by 0, no?

Thanks for all the help, still pluggin away at this if you had any more ideas.

Cheers.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mod is the remainder after division.
lemonfreshmedia
Forum Commoner
Posts: 26
Joined: Fri Dec 02, 2005 7:14 pm

Thanks,

Post by lemonfreshmedia »

Although i'm still returning 1
when running 39 mod 3 I get a positive result when checking if (last()mod3) = 0

Thanks guys

I'd post my code but instead of using variables im running a php script to write csaes for each possibility.
Post Reply