質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

解決済

1回答

706閲覧

shuffleを使用して生成したビンゴシートを別ページで表示

KOSUKE.

総合スコア6

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

0グッド

0クリップ

投稿2020/05/31 04:47

PHP学習中です。お時間がある方はご教授ください。

下記のプログラムを作成しようとしています。
①ビンゴシートを2つ生成。(index.php)
②どちらか一方を選択し、送信ボタンを押す。(index.php)
③選択されたビンゴシートのみを表示。(bingosheet.php)

この時送信ボタンを押すとページが遷移し、bingosheet.phpにて新しいビンゴシートを生成してしまいます。
index.phpで表示されているビンゴシートと同じものをbingosheet.phpで表示するには
どのような処理を実行すればよいのでしょうか?

index.php

<?php require_once(__DIR__ . '/config.php'); require_once(__DIR__ . '/Bingo.php'); $bingo = new \MyApp\Bingo(); $nums1 = $bingo->create1(); $nums2 = $bingo->create2(); ?> <!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <title>BINGO!</title> <link rel="stylesheet" href="styles.css"> </head> <body> <h1>Select BINGO sheet!</h1> <table> <tr> <td> <div id="container"> <table> <tr> <th id="hed">B</th> <th id="hed">I</th> <th id="hed">N</th> <th id="hed">G</th> <th id="hed">O</th> </tr> <?php for ($i = 0; $i < 5; $i++) : ?> <tr> <?php for ($j = 0; $j < 5; $j++) : ?> <td id="content"><?= h($nums1[$j][$i]); ?></td> <?php endfor; ?> </tr> <?php endfor; ?> </table> </div> </td> <td> <div id="container"> <table> <tr> <th id="hed">B</th> <th id="hed">I</th> <th id="hed">N</th> <th id="hed">G</th> <th id="hed">O</th> </tr> <?php for ($i = 0; $i < 5; $i++) : ?> <tr> <?php for ($j = 0; $j < 5; $j++) : ?> <td id="content"><?= h($nums2[$j][$i]); ?></td> <?php endfor; ?> </tr> <?php endfor; ?> </table> </div> </td> </tr> <tr> <td> <div id="selectBox"> <form method="post" action="bingosheet.php"> <input type="submit" name="option1" value="送信ボタン"> </form> </div> </td> <td> <div id="selectBox"> <form method="post" action="bingosheet.php"> <input type="submit" name="option2" value="送信ボタン"> </form> </div> </td> </tr> </table> </form> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script src="bingosheet.js"></script> </body> </html>

bingosheet.php

php

1<?php 2require_once(__DIR__ . '/config.php'); 3require_once(__DIR__ . '/Bingo.php'); 4 5$bingo = new \MyApp\Bingo(); 6$nums1 = $bingo->create1(); 7$nums2 = $bingo->create2(); 8?> 9 10<!DOCTYPE html> 11<html lang="ja"> 12<head> 13 <meta charset="utf-8"> 14 <title>BINGO!</title> 15 <link rel="stylesheet" href="styles.css"> 16</head> 17 18<body> 19 <?php if (isset ($_POST['option1'])): ?> 20 <div id="container"> 21 <table> 22 <tr> 23 <th id="hed">B</th> 24 <th id="hed">I</th> 25 <th id="hed">N</th> 26 <th id="hed">G</th> 27 <th id="hed">O</th> 28 </tr> 29 <?php for ($i = 0; $i < 5; $i++) : ?> 30 <tr> 31 <?php for ($j = 0; $j < 5; $j++) : ?> 32 <td id="content"><?= h($nums1[$j][$i]); ?></td> 33 <?php endfor; ?> 34 </tr> 35 <?php endfor; ?> 36 </table> 37 </div> 38 <?php elseif(isset ($_POST['option2'])): ?> 39 <div id="container"> 40 <table> 41 <tr> 42 <th id="hed">B</th> 43 <th id="hed">I</th> 44 <th id="hed">N</th> 45 <th id="hed">G</th> 46 <th id="hed">O</th> 47 </tr> 48 <?php for ($i = 0; $i < 5; $i++) : ?> 49 <tr> 50 <?php for ($j = 0; $j < 5; $j++) : ?> 51 <td id="content"><?= h($nums2[$j][$i]); ?></td> 52 <?php endfor; ?> 53 </tr> 54 <?php endfor; ?> 55 </table> 56 </div> 57 <?php endif; ?> 58 59 <form method="post" action="index.php"> 60 <input type="submit" value="戻る"> 61 </form> 62</body> 63

Bingo.php

php

1<?php 2 3namespace MyApp; 4 5class Bingo { 6 public function create1(){ 7 $nums = []; 8 9 for ($i = 0; $i < 5; $i++) { 10 $col = range($i * 15 + 1, $i * 15 + 15); 11 shuffle($col); 12 $nums[$i] = array_slice($col, 0, 5); 13 } 14 15 $nums[2][2] = "FREE"; 16 return $nums; 17 } 18 19 public function create2(){ 20 $nums = []; 21 22 for ($i = 0; $i < 5; $i++) { 23 $col = range($i * 15 + 1, $i * 15 + 15); 24 shuffle($col); 25 $nums[$i] = array_slice($col, 0, 5); 26 } 27 28 $nums[2][2] = "FREE"; 29 return $nums; 30 } 31} 32 33 ?> 34

styles.css

css

1body { 2 font-family: Arial, sans-selif; 3 text-align: center; 4 font-size: 16px; 5} 6 7table { 8 margin-left: auto; 9 margin-right: auto; 10} 11 12#container { 13 margin: 0 auto; 14 width: 400px; 15 height: 330px; 16 border: solid 1px black; 17} 18 19td, th { 20 width: 60px; 21 height: 45px; 22} 23 24#content { 25 background: #F06292; 26 color: #fff; 27} 28 29#hed { 30 color: #F06292; 31 font-size: 22px; 32} 33 34#selectBox { 35 width: 400px; 36 height: 50px; 37 border: solid 1px black; 38 39} 40

function.php

php

1<?php 2 3function h($s) { 4 return htmlspecialchars($s, ENT_QUOTES, 'UTF-8'); 5} 6 7 ?> 8

config.php

php

1<?php 2 3ini_set('display_errors', 1); 4 5require_once(__DIR__ . '/functions.php'); 6 7 8 ?> 9

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

ページ遷移時にデータを引き継ぐのはセッションを使うのが一般的です。
クライアントのjsを利用してよいならクッキーやlocalStorageをつかう手もあります

投稿2020/05/31 05:13

yambejp

総合スコア114843

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

KOSUKE.

2020/05/31 07:48

yambejp様 セッションを使用して解決しました。 ご回答ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問