Please help with the code below.

I am a beginner and only recently began to learn programming.

List of variables and their values:

$slideMenu = ''; $slides = array( array( 'background' => 'image', 'h2' => 'Text', 'text' => 'Text' ), array( 'background' => 'image', 'h2' => 'Text', 'text' => 'Text' ), array( 'background' => 'image', 'h2' => 'Text', 'text' => 'Text' ), ); 

And further, here:

 <?php foreach ($slides as $i => $slide) { ?> <div id="slide-<?php echo $i + 1; ?>" data-id="<?php echo $i + 1; ?>" class="slide" style="display: <?php echo($i > 0 ? 'none' : 'block'); ?>;background-image: url(<?php echo $slide['background'] ?>);"> <div class="container"> <div style="width: 40%;color: #fff;" class="col"> <div style="padding: 0 15px;"> <h2><?php echo $slide['h2'] ?></h2> <p><?php echo $slide['text'] ?></p> </div> </div> </div> </div> <?php $slideMenu .= '<a href="#" data-slide-id="' . ($i + 1) . '" data-rel="#slide-' . ($i + 1) . '" class="' . ($i == 0 ? 'current' : '') . '"></a>'; ?> <?php } ?> 

These parts are not completely clear:

 <div id="slide-<?php echo $i + 1; ?>" data-id="<?php echo $i + 1; ?>" class="slide" style="display: <?php echo($i > 0 ? 'none' : 'block'); ?>;background-image: url(<?php echo $slide['background'] ?>);"> <?php $slideMenu .= '<a href="#" data-slide-id="' . ($i + 1) . '" data-rel="#slide-' . ($i + 1) . '" class="' . ($i == 0 ? 'current' : '') . '"></a>'; ?> 

Closed due to the fact that the essence of the issue is incomprehensible by the participants of Kromster , user194374, cheops , pavel , lexxl 9 Aug '16 at 10:31 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 2
    To what level do you need to razkazyvat? before explaining what are for and if? - KoVadim
  • Honestly, the question is similar to the search for explanations of homework. - Andrewus
  • No, not at all d / s :) I just start my way as a programmer, and I don’t understand a bit of things in certain things. You, too, were once newcomers. Therefore, please treat with understanding. - Z.Zum
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

 <?php foreach ($slides as $i => $slide) { ?> 

Creates a loop on all $slides elements. During each loop pass, there will be 2 elements $i - index, $slide - array of values array('h2' => "значение", 'text' => "значение", ...);


 <div id="slide-<?php echo $i + 1; ?>" data-id="<?php echo $i + 1; ?>" class="slide" style="display: <?php echo($i > 0 ? 'none' : 'block'); ?>;background-image: url(<?php echo $slide['background'] ?>);"> 

A block is created where the attributes are generated: id = slide + наш индекс с каждым проходом цикла (it will be like this: id="slide-1", id="slide-2", id="slide-3"... ). The same with the data-id attribute. And the check is at the end, at non-zero $i . If you are in the first element then the block will have display: block , otherwise display: none ;


 <h2><?php echo $slide['h2'] ?></h2> <p><?php echo $slide['text'] ?></p> 

Just display 2 values ​​from the $slide array


 <?php $slideMenu .= '<a href="#" data-slide-id="' . ($i + 1) . '" data-rel="#slide-' . ($i + 1) . '" class="' . ($i == 0 ? 'current' : '') . '"></a>'; ?> 

Writes a link with data- attributes to the $slideMenu variable. Apparently generates the number of points (switches) by the number of slides.

  • Thank you very much for the help in clarifying! - Z.Zum
  • @ Z.Zum to mark then the question as correct, if you understand everything, and he answers your question - Vasily Barbashev