Brain problems, there is a program loop logic problem, urgent online and so on

Source: Internet
Author: User
Tags php write
Correct answer: thanks to the original code universe {code...}: note that this is a very scary question. Please read it carefully! First, there is a condition that cannot be changed, that is, the code of the loop output content, such as {code ...} the HTML Format I need to output is as follows {code ...} condition: text... correct answer:
Thanks to the Code universe

$i=0;
      if ($i % 5 == 0) {        echo '

'; } echo '

...

'; if ($i % 5 == 4) { echo '

'; } $i++; if ($i % 5 != 0) { echo '

';}

Original article content:
Note: This is a matter of extreme fear. Read the questions carefully!

First, there is a condition that cannot be changed, that is, the code of the loop output content, such


  // Multiple articles will be output cyclically here
  

The HTML Format I need to output is as follows:

...

...

...

...

...

// Five articles in total, that is, five posts

...

...

...

...

...

...

...

...

...

...

Condition:

  1. The number of articles is uncertain. It must be greater than 5.

  2. Use a Box p package every five articles (the difficulty is that the first output

    , Fifth output

    )

  3. You cannot store every five posts in other places and output them together!

A chestnut? :
Suppose I have 11 articles, then the final html format should be

...

...

...

...

...

...

...

...

...

...

...

What should I do with my PHP code at this time?

My attempt:

';$qp_a++;} ?> ...post ';} ?>

The disadvantage of this method is that it can only be the value specified in the IF statement, but also the number of articles that can be divisible.

You may have thought of making an output five times after making judgments such as $ I ++. However, please note that the first loop requires echo

To close the tag, add it after the fifth output, that is, echo

.

Abstract:
Zhou Shen's answer

= !!! Isn't that easy?... it's difficult to get a hair $ postCount = 0; // The article count echo'

'; // Multiple articles will be output cyclically here $ postCount ++; if ($ postCount> 1 & $ postCount % 5 = 1) {echo'

';} Else {echo'

...

';} Echo'

';

Result:

The first five, the second three four, left blank

Refer:


  ';?>
  1 & $ a % 5 = 1) {echo'

';} Else {echo'

Content

';}}?> ';?>

Effect: http://www.presscode.cn/temp.php

Reply content:

Correct answer:
Thanks to the Code universe

$i=0;
      if ($i % 5 == 0) {        echo '

'; } echo '

...

'; if ($i % 5 == 4) { echo '

'; } $i++; if ($i % 5 != 0) { echo '

';}

Original article content:
Note: This is a matter of extreme fear. Read the questions carefully!

First, there is a condition that cannot be changed, that is, the code of the loop output content, such


  // Multiple articles will be output cyclically here
  

The HTML Format I need to output is as follows:

...

...

...

...

...

// Five articles in total, that is, five posts

...

...

...

...

...

...

...

...

...

...

Condition:

  1. The number of articles is uncertain. It must be greater than 5.

  2. Use a Box p package every five articles (the difficulty is that the first output

    , Fifth output

    )

  3. You cannot store every five posts in other places and output them together!

A chestnut? :
Suppose I have 11 articles, then the final html format should be

...

...

...

...

...

...

...

...

...

...

...

What should I do with my PHP code at this time?

My attempt:

';$qp_a++;} ?> ...post ';} ?>

The disadvantage of this method is that it can only be the value specified in the IF statement, but also the number of articles that can be divisible.

You may have thought of making an output five times after making judgments such as $ I ++. However, please note that the first loop requires echo

To close the tag, add it after the fifth output, that is, echo

.

Abstract:
Zhou Shen's answer

= !!! Isn't that easy?... it's difficult to get a hair $ postCount = 0; // The article count echo'

'; // Multiple articles will be output cyclically here $ postCount ++; if ($ postCount> 1 & $ postCount % 5 = 1) {echo'

';} Else {echo'

...

';} Echo'

';

Result:

The first five, the second three four, left blank

Refer:


  ';?>
  1 & $ a % 5 = 1) {echo'

';} Else {echo'

Content

';}}?> ';?>

Effect: http://www.presscode.cn/temp.php

$i=0;echo '

'; if ($i > 0 && $i % 5 == 0) { echo '

'; } echo '

...

'; $i++; echo '

';
$i=0;
      if ($i % 5 == 0) {        echo '

'; } echo '

...

'; if ($i % 5 == 4) { echo '

'; } $i++; if ($i % 5 != 0) { echo '

';}

      echo '

'; echo '

...

'; if (have_posts()) { the_post(); echo '

...

'; } else { echo '

'; break; } if (have_posts()) { the_post(); echo '

...

'; } else { echo '

'; break; } if (have_posts()) { the_post(); echo '

...

'; } else { echo '

'; break; } if (have_posts()) { the_post(); echo '

...

'; } echo '

';

      echo '

'; echo '

...

'; if (have_posts()) { the_post(); echo '

...

'; if (have_posts()) { the_post(); echo '

...

'; if (have_posts()) { the_post(); echo '

...

'; if (have_posts()) { the_post(); echo '

...

'; } } } } echo '

';

= !!! Isn't that easy?

$ PostCount = 0; // document count
Echo'

';

// Multiple articles will be output cyclically here
$ PostCount ++;
Echo'

...

';
If ($ postCount> 1 & $ postCount % 5 = 1 ){

echo '

';

}


Echo'

';

$a = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17);$s = array();foreach($a as $k=>$v){    if(count($s) === 5){        echo '

'; foreach($s as $key=>$val){ echo '

'.$val.'

'; } echo '

'; unset($s); $s = array(); $s[] = $v; }else{ $s[] = $v; }}if(count($s) > 0){ echo '

'; foreach($s as $key=>$val){ echo '

'.$val.'

'; } echo '

'; unset($s);}

?>

The Problem description is a bit simple. But at least one output is required for counting. Set an I to output I + 1 at a time and try it by yourself. This is certainly not hard to achieve.

This is the same as the display image I used to display in five columns.
Data structure:
List

Final output structure list

The size of the second listi is 5, and the data is put into listi in sequence. Finally, use listi to put the data into the list, and the page outputs listi.

Rules are not difficult to sort out. It is not difficult to keep an eye on "number of nodes that have been written into the DOM" and insert appropriate events before and after the nodes are written to the DOM at the same time.

The only problem is:During the iteration process, we cannot know that this iteration is the last one.. Therefore, after the iteration, if the total number is not divided by 5, the last group is not closed. Therefore, there must be a check after the iteration ends.


  '. PHP_EOL); define ("DIV_CONTAINER_END ",'

'. PHP_EOL); define ("DIV_BOX_START ",''.'

'. PHP_EOL); define ("DIV_BOX_END ",''.'

'. PHP_EOL); define ("DIV_POST_FORMAT ",''.'

% S

'. PHP_EOL); define ("GROUP_BY", 5); $ items = range (1, 11); // try changing $ max to 9, 10, 11. echo DIV_CONTAINER_START; $ written_to_dom = 0; foreach ($ items as $ item) // the same row is equivalent to while (have_posts (): the_post (); prepare the article content {if ($ written_to_dom % GROUP_BY = 0) {echo DIV_BOX_START;} echo sprintf (DIV_POST_FORMAT, $ item); // This row is equivalent to the_content (); that is, the output article content $ written_to_dom + = 1; if ($ written_to_dom % GROUP_BY = 0) {Echo DIV_BOX_END ;}}// this row is equivalent to the endwhile of the loop syntax during mixed HTML/PHP write; and wp_reset_query (); if ($ written_to_dom % GROUP_BY! = 0) {echo DIV_BOX_END;} echo DIV_CONTAINER_END;

Do not be so clever in actual projects. In the V (View) layer, we recommend that you discard the loop method of getting an output.

You need to fetch a batch (five or take to the end) at a time, and then get one

Group output. The loop method (pseudo code) suitable for actual projects should be like this:

define("POSTS_PER_GROUP", 5);echo "p container start";while (have_posts()){    $posts_grouped = [];    for ($i=0; $i
  
   content;    }    echo "p group end";}echo "p container end"
  

"Save every five posts to other places, and then output them together.", In fact, it is correct in actual engineering. Don't forget.

I provide my methods for reference:

if ( have_posts() ) :    global $wp_query;    while ( $splice_post = array_splice( $wp_query->posts, 0, 5 ) ) {        printf( '

'); foreach( $splice_post as $key => $post ) { setup_postdata( $post ); the_title(); } printf( '

'); }endif;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.