View row by row of.txt file by clicking

Asked

Viewed 206 times

0

I’m a beginner in PHP, I’m trying to create a button, and according to that 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 turning buttons: inserir a descrição da imagem aqui

What I would like to happen: If the code is reading line 5, it will display only line 5. If the code is reading line 6, it will display only line 6.

Clearest example of what I’m trying to do: inserir a descrição da imagem aqui

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 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 many ways to do this, frameworks usually make it more practical, but considering that you didn’t even know where to start yet, I suggest you read about HTML before proceeding with your idea.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.