Display line by line of the .txt file when clicking

0

I'm a beginner in PHP, I'm trying to create a button, and according to whether I was clicking the next line of the file would be displayed.

1st click - line 1 is displayed / 2nd click - line 2 is displayed / and so on

<?php
$linhas = file("texto.txt");
for($i = 0;$i < count($linhas);$i++) {
echo "<button value='". $linhas[$i]. "'>". $linhas[$i] ."</button>";
}
?>

But in the code above, all the lines end up being buttons:

WhatIwouldliketoseehappen:Ifthecodeisreadingline5,itwilldisplayonlyline5.Ifthecodeisreadingline6,itwillonlydisplayline6.

MoreclearexampleofwhatI'mtryingtodo:

    
asked by anonymous 11.11.2018 / 19:16

1 answer

0

This is not a PHP problem, it is HTML and JavaScript

You need to change the display style of your line to hide or show the content.

function toggleDisplay(e) {
  let linhas = document.getElementsByClassName('linha');
  for (let i = 0; i < linhas.length; i++) {
    linhas[i].style.display = i === e ? '' : 'none';
  }
}
<button onclick="toggleDisplay(0)">Clique Aqui1</button>
<button onclick="toggleDisplay(1)">Clique Aqui2</button>
<button onclick="toggleDisplay(2)">Clique Aqui3</button>
<p class="linha" style="display: none">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p class="linha" style="display: none">Nam vulputate, risus non finibus commodo, dolor est euismod mauris, eu condimentum enim lectus id risus.</p>
<p class="linha" style="display: none">Fusce sed sapien tristique, pellentesque libero vitae, elementum erat.</p>

There are several ways to do this, frameworks usually make this more practical, but considering that you did not even know where to start, I suggest you read about HTML before proceeding with your idea.

    
11.11.2018 / 19:51