Home>
Preface

I am making a program to manage book titles and numbers.
Since DB is not used, book titles and numbers are managed programmatically.

I want to achieve

I want to add a search function to this program.
Example: Enter The Tale of Genji in the search form and press the search button.
➡️ Only books from The Tale of Genji are displayed.

Code details

book.php (class about books)

<? php
class Book {
  public $number;
  public $name;
  public function __construct ($number, $name) {
    $this->number = $number;
    $this->name = $name;
  }
}
?>

data.php (book name and number)

<? php
require_once ('book.php');
$book01 = new Book (01,'I am a cat');
$book02 = new Book (02,'Harry Potter');
$book03 = new Book (03,'The Tale of Genji');
$book04 = new Book (04,'heart');
$books = array ($book01, $book02, $book03, $book04);
?>


index.php (search screen)

<? php require_once ('data.php')?>
<! DOCTYPE html>
<html>
<body>
<form method = "POST" action = "find.php">
  <label>Search by title</label><br />
  <input type = "text" name = "data" /><br />
  <input type = "submit" value = "submit" />
</form>
<table>
  <tr>
    <th>number</th>
    <th>Title</th>
  </tr>
  <? php foreach ($books as $book):?>
    <tr>
      <td><? php echo $book->number?></td>
      <td><? php echo $book->name?></td>
    </tr>
  <? php endforeach?>
</table>
</body>
</html>


find.php (screen after search)

<? php require_once ('data.php')?>
<! DOCTYPE html>
<html>
<body>
<? php foreach ($books as $book):?>
  if ($book->name == $_POST ["data"]) {
  <? php echo $book->name?>
  }
<? php endforeach?>
</body>
</html>
problem

find.phpWhen the character string sent from the search form and the title of the book match, I wrote the code to display the name of the book, but it does not work.
If anyone can understand it, I would like to know it.

php
  • Answer # 1

    <? php require_once ('data.php')?>
    <! DOCTYPE html>
    <html>
    <body>
    <? php foreach ($books as $book):?>
      if ($book->name == $_POST ["data"]) {
      <? php echo $book->name?>
      }
    <? php endforeach?>
    </body>
    </html>


    To
    Do you do this

    <? php require_once ('data.php');?>
    <! DOCTYPE html>
    <html>
    <body>
    <? php
    foreach ($books as $book) {
       if ($book->name == $_POST ["data"]) {
        echo $book->name;
      }
    }
    ?>
    </body>
    </html>

    It's like this

    <? php require_once ('data.php')?>
    <! DOCTYPE html>
    <html>
    <body>
    <? php foreach ($books as $book):?>
    <? php if ($book->name == $_POST ["data"]):?>
      <? php echo $book->name?>
    <? php endif;?>
    <? php endforeach?>
    </body>
    </html>

    It ’s not related to the question,
    $book01 = new Book (01,'I am a cat');of01 01Is recognized as an octadecimal number
    Reference PHP manual
    $book01 = new Book ('01','I am a cat');Will it be a character string as
    $book01 = new Book (1,'I am a cat');When displaying it as an integer
    sprintf ('% 02d', $book->number);Let's fill it with zeros.