Friday 15 March 2013

php - While loop making even/odd blocks with uneven number. Hard code second block -


Maybe it's difficult to understand my title, if you can clarify it for everyone then please edit my post Please.

So what did I do when I made a loop and wanted to block the blocks like x

I have to separate the two blocks from each other. Code> X form. Then it will be going to (block 1) [gray], (block 2) [darkened] and then (block1) [dark gray], (block 2) [gray] . To achieve this, I created a function inside the loop in my time and before that I created a counter ++ counter counter 0 and starts adding 1 until it becomes 3, Because 3 makes it uneven, it will make this x form. But the problem is that the first two blocks will be in the beginning of the uneven number 3 because of the brown color.

Do anyone know how I can hardcod the first two blocks?

Also in my code I am taking information from the database in a descending order.

It's built in WordPress but I made more information about it here.

  It should be first = gray second = dark gray third = dark gray fourth = gray   

and it should be repeated itself in full time.

  Enter the code here   

Already created the code (because I deleted the code in a different language and read the comments easily Changed with):

  global $ wp_query; $ Temp = $ wp_query; $ Args = (array ('post_type' = & gt; 'news', 'posts_per_page' => 9999, 'post__not_in' = & gt; array (835), 'command' => 'DESC')) ; $ Wp_query = new WP_Query (); $ Counter = 0; $ Wp_query- & gt; Query ($ args); While ($ wp_query-> is_poz ()): $ wp_query-> The_post (); ? & Gt; & Lt ;? Php $ counter ++; If ($ counter == 3) {? & Gt; / ** Dark brown block ** ** & lt ;? Php $ counter = 0; ? & Gt; & Lt ;? Php} and {? & Gt; / ** gray block ** / & lt ;? here Php}? & Gt;   

So after the help of the community, I finally found my solution. I feel so stupid that I did not find it myself. Using this method, you will get (block 1 = gray), (block 2 = dark gray), (block 3 = gray), (block 4 = dark gray). Solution

  & lt ;? Php $ counter ++; If ($ counter == 1) {? & Gt; / ** gray block ** / & lt ;? here Php} and if ($ counter == 2 {? & Gt; / ** here is the dark brown block ** / & lt ;? php} and if ($ counter == 3 {? & Gt; / ** here Gray block ** / & lt ;? php} and if ($ counter === 4 {? & Gt; / ** here dark brown block ** /   

So what I finally did here I'm making the first 4 color manual and then I'll reset it back to zero and give it because it gives me one of the two blocks

This simple use modulo < P> % is the modulo operator.

  for Php ($ counter = 0; $ counter <10; ++ $ counter) echo ($ Counter $ 3) "gray": "dark gray"). & Lt; br & gt; ';? & Gt;   

This outputs

  gray gray gray gray gray gray gray gray gray gray gray gray gray gray   

as you wanted.

I made Bella It shows how it works.



No comments:

Post a Comment