PHPでtableをループさせたい!

このQ&Aのポイント
  • PHPでMysqlから取り出した値をHTMLのtableで出力する方法を教えてください。
  • データベースから取り出した値をループさせてtableに出力する際、while文とmysql_fetch_array関数を使用します。
  • サンプルのソースコードも提供しますので、ご参考にしてください。
回答を見る
  • ベストアンサー

PHPでtableをループさせたい!

【急募!】PHPでMysqlから取り出した値をHTMLのtableで出力したい。 うまくいかなくて困ってます。 今回で2度目の質問となります。今回も急いでおります。 現在、プログラムの勉強をして半年近くになります。本日3/3までに作成を完了しなければならず困っております。 何日か掛けて調べたのですが、まだまだ勉強不足の為に理解が出来ませんでした。 PHPで作成をしております。データベースから取り出した値をHTMLで作成したtableに出力後、tableをデータベースに入っている数だけループさせたいです。※イメージ画像あり おそらくwhile分でループさせれば良いとういうのは理解できるのですが、テーブル自体を増やすやりかたかが解りません。 while(データーベースの値をループさせてひとつずつ出力する。,テーブルも同じ数出力する) 全部取り出したら break で抜ける......となると思うのですが、書き方か解りません。 mysql_fetch_arrayというものもあるようですがまだ理解ができません。 答え合わせになってしまうのが恐縮でございますが、答え合わせのコードを頂けましたら嬉しいです。 まだ理解が浅いため、質問の内容に理解が出来なければ、ご連絡を頂きましたら改善改良をします。 お恥ずかしいですが、ソースを乗せさせて頂きますので宜しくお願い致します。 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <link href="css/style2.css" rel="stylesheet" type="text/css"> <title>管理画面</title> </head> <body> <div id="main"> <?php $dsn='mysql:dbname=motorlinks;host=localhost'; $user='root'; $password=''; $dbh=new PDO($dsn,$user,$password); $dbh->query('SET NAMES utf8'); $sql='SELECT code,gazou,name,shiyo,price,shiharai,first,sample,comment FROM pone WHERE 1'; $stmt=$dbh->prepare($sql); $stmt->execute(); print'製品一覧<br><br>'; print'<form method="post"action="pone_branch.php">'; ?> <?php while(true) { $rec=$stmt->fetch(PDO::FETCH_ASSOC); if($rec==false) { break; } print $rec['gazou']; print $rec['name']; print $rec['shiyo']; print $rec['price'].'円'; print $rec['shiharai']; print $rec['first']; print $rec['sample']; print $rec['comment']; print'<br>'; }?> <table width="800" border="1" cellpadding="0"> <?php print'<input type="radio"name="ponecode"value="'.$rec['code'].'">'; ?> <tr> <th colspan="2" rowspan="4" scope="col"><?php '<img src="gazou/burank.jpg" width="200" height="150" alt=""/>'; ?></th> <th width="162" height="4" scope="col">Product Name</th> <th width="162" height="4" scope="col">Specification</th> <th width="162" height="4" scope="col">Price</th> <th width="162" height="4" scope="col">Payment</th> </tr> <tr> <td height="63">&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> </tr> <tr> <th width="162" height="4" scope="col">First Order</th> <th width="162" height="4" scope="col">Sample</th> <th width="162" height="4" scope="col">Comment</th> <th width="162" height="4" scope="col">Contact</th> </tr> <tr> <td height=63">&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> <td>&nbsp;</td> </tr> </table> <?php print'<br>'; print'<input type="submit" name="disp" value="参照">'; print'<input type="submit" name="add" value="追加">'; print'<input type="submit" name="edit" value="修正">'; print'<input type="submit" name="delete" value="削除">'; print'</form>'; ?> <br> <a href="index_mo.html">トップメニューへ</a><br> </form> </div> </body> </html>

  • PHP
  • 回答数1
  • ありがとう数1

質問者が選んだベストアンサー

  • ベストアンサー
  • agunuz
  • ベストアンサー率65% (288/438)
回答No.1

自分で while(データーベースの値をループさせてひとつずつ出力する。,テーブルも同じ数出力する) と書いているとおりにソースを書けばいいです。 while ($rec = $stmt->fetch(PDO::FETCH_ASSOC)) { // $rec にデータを読込んでいるので、それを出力する } あるいはprepareする必要がない(プレースホルダがない)ので foreach ($pdo->query('select * from pone') as $rec) { // $rec にデータを読込んでいるので、それを出力する } でもいいです。 ただ、tableのtr要素に含まれてない部分にinput要素を出力しているのはダメです(htmlソースとして正しくありません)。まずは「正しいhtmlソース」を出力出来るようにしてください(phpより先にhtmlを学んでください)。

kintethu
質問者

お礼

今まで色々と試してみてそして聞いて見ましたが解りませんでした。 しかし、丁寧な説明によりそれを元に考えることが出来ましたので出来ました! 感謝します!!

関連するQ&A

  • 動的なtableの値を取得したい

    いつもお世話になります。 見よう見まねでやっている超初心者です。 下記の様なテーブル(phpで読み込んでいますので実際は動的です。) の回答ボタンをクリックすると テーブル下の<INPUT type="text" name="kotae">に ボタンをクリックされた行のb列の値を入れたいと思っています。 function kaito()をどのように書いたら取得できるか悩んでいます。 初心者なもので具体的な回答でしたら助かります。 ご教授の程どうぞよろしくお願い致します。 <html> <head> <meta content="text/html; charset=utf-8" /> <script> </script> </head> <body> <table width="100" border="1"> <tr> <th width="30" scope="col">a</th> <th width="30" scope="col">b</th> <th width="40" scope="col">c</th> </tr> <tr> <td>1</td> <td>2</td> <td><input type="button" value="回答" onclick="kaito()"/></td> </tr> <tr> <td>5</td><td>3</td> <td><input type="button" value="回答" onclick="kaito()"/></td> </tr> <tr> <td>5</td> <td>4</td> <td><input type="button" value="回答" onclick="kaito()"/></td> </tr> <tr> <td>5</td> <td>5</td> <td><input type="button" value="回答" onclick="kaito()"/></td> </tr> </table> <INPUT type="text" name="kotae" size="12" > </body> </html>

  • テーブルの情報が一部しか表示されません。(php)

    すべてのID分の提出状況を表示させたい("○"か"×")のですが、 id=1の提出状況しか表示されません。 >>CODE $recordSet2 = mysql_query('SELECT * FROM report'); <<CODE をwhileにいれてみたり試行錯誤してみたのですが、上手くいきません。 ご指摘よろしくお願い致します。 プログラム >>CODE <table width="70%"> <tr> <th scope="col"><bl>ID</bl></th> <th scope="col"><bl>教科名</bl></th> <th scope="col"><bl>課題名</bl></th> <th scope="col"><bl>担当教員</bl></th> <th scope="col"><bl>提出期限</bl></th> <th scope="col"><bl>再提出</bl></th> <th scope="col"><bl>再提出期限</bl></th> <th scope="col"><bl>提出状況</bl></th> </tr> <?php $recordSet = mysql_query('SELECT * FROM exercise ORDER BY id'); while ($table = mysql_fetch_assoc($recordSet)){ ?> <tr> <td><?php print(htmlspecialchars($table['id'])); ?></td> <td><?php print(htmlspecialchars($table['lessonname'])); ?></td> <td><?php print(htmlspecialchars($table['name'])); ?></td> <td><?php print(htmlspecialchars($table['teacher'])); ?></td> <td><?php print(htmlspecialchars($table['presentday'])); ?></td> <td><?php print(htmlspecialchars($table['represent'])); ?></td> <td><?php print(htmlspecialchars($table['representday'])); ?></td> <td><?php $recordSet2 = mysql_query('SELECT * FROM report'); $report = mysql_fetch_assoc($recordSet2); if ($table['lessonname'] == $report['lessonname'] && $table['name'] == $report['exercisename']) { if(eregi($member['name'], $report['upfile'])) { echo "○"; }else { echo "×"; } } ?></td> </tr> <?php } ?> </table> <<CODE

    • ベストアンサー
    • PHP
  • テーブルの情報が一部しか表示されません。(php)

    すべてのID分の提出状況を表示させたい("○"か"×")のですが、 id=1の提出状況しか表示されません。 テストデータはid=5までの5件を入力しています。 $recordSet2 = mysql_query('SELECT * FROM report'); をwhileにいれてみたり試行錯誤してみたのですが、上手くいきません。 ご指摘よろしくお願い致します。 プログラム <table width="70%"> <tr> <th scope="col"><bl>ID</bl></th> <th scope="col"><bl>教科名</bl></th> <th scope="col"><bl>課題名</bl></th> <th scope="col"><bl>担当教員</bl></th> <th scope="col"><bl>提出期限</bl></th> <th scope="col"><bl>再提出</bl></th> <th scope="col"><bl>再提出期限</bl></th> <th scope="col"><bl>提出状況</bl></th> </tr> <?php $recordSet = mysql_query('SELECT * FROM exercise ORDER BY id'); while ($table = mysql_fetch_assoc($recordSet)){ ?> <tr> <td><?php print(htmlspecialchars($table['id'])); ?></td> <td><?php print(htmlspecialchars($table['lessonname'])); ?></td> <td><?php print(htmlspecialchars($table['name'])); ?></td> <td><?php print(htmlspecialchars($table['teacher'])); ?></td> <td><?php print(htmlspecialchars($table['presentday'])); ?></td> <td><?php print(htmlspecialchars($table['represent'])); ?></td> <td><?php print(htmlspecialchars($table['representday'])); ?></td> <td><?php $recordSet2 = mysql_query('SELECT * FROM report'); $report = mysql_fetch_assoc($recordSet2); if ($table['lessonname'] == $report['lessonname'] && $table['name'] == $report['exercisename']) { if(eregi($member['name'], $report['upfile'])) { echo "○"; }else { echo "×"; } } ?></td> </tr> <?php } ?> </table>

    • ベストアンサー
    • PHP
  • 空のテーブルを表示させたい

    DBから検索条件に当てはまるデータを全て読み込み、 特定の内容だけ表のように表示させるプログラムを作成しています。 ほとんど作成し終わったのですが、項目の中のデータが空だと表の枠(?)を表示してくれず、空白になってしまいます。 それでは見た目が悪いので、なんとかExcelのような表にしたいのですが、やり方がわかりません。 どなたか教えていただけないでしょうか? ○開発環境 ・OS:WindowsXP Professional ・ツール:Microsoft InterDev6.0 ・DB:SQLServer2000 ○ソース(表示部分のみ) 該当データ数 <%=objRS.RecordCount%><br> <% Response.Write"<input type=""submit"" id = ""Indication1"" value=""表示する"" name=""Indication"">" %> <br> <table border="1" width="100%"> <tr> <th scope="col" bgcolor="#ccffff"></th> <th scope="col" bgcolor="#ccffff"><font size="2">入力番号</font></th> <th scope="col" bgcolor="#ccffff"><font size="2">日付</font></th> <th scope="col" bgcolor="#ccffff"><font size="2">名前</font></th> </tr> <% '***** 検索されたデータの表示 ***** Do Until ObjRS.EOF %> <tr> <td scope="col"> <% Response.Write"<input type=""radio"" value=""" & ObjRS("no")& """ name=""Check"">" %> </td> <td scope="col"><%=ObjRS("no")%></td> <td scope="col"><%=ObjRS("date")%></FONT></td> <td scope="col"><%=ObjRS("name")%></FONT></td> </tr> <% ObjRS.MoveNext LOOP %> </table>

  • テーブルのテーブルの間に間ができるのでしょうか?

    はじめまして、超html初心者のものです。 なぜ同じようテーブルを3つ書いているのに一番上のテーブルと真ん中のテーブルの間が開いてしまっているのかがわかりません。 あと、一番上のテーブルの-から先をもう少し左に寄せることもうまくいきません。 なにかアドバイスをよろしくお願いします。 ↓これがソースです。 <html> <head><title>test</title> </head> <form> <table border=1 cellspacing=0 cellpadding=2 width=760> <tr> <th width=95 height=30 align="right">日付け:</th> <td width=284> <input type="text"><input type="button"> </td> <td>&nbsp;-&nbsp;</td> <td><input type="text"><input type="button"></td> </tr> </table> <table border=1 cellspacing=0 cellpadding=2 width=760> <tr> <th width=95 height=30 align="right">部署:</th> <td width=100> <select name="busyo"></select> </td> <th width=80 align="right">営業所:</th> <td width=100> <select name="eigyousyo"></select> </td> <th width= 80 align="right">担当コード:</th> <td width=450> <select name="tantocode"></select> </td>   </tr> </table> <table border=1cellspacing=0cellpadding=2width=760> <tr> <th width=80 align="right">権限:</th> <td width=700> <select name="kengen"></select> </td> </tr> </table> </form> </html>

  • 動的な表の計算

    いつもお世話になります。 見よう見まねでやっている初心者です。 下記のような動的な表があります(PHPで表の行が可変します。) name="aa"に入力された値とname="bb"に入力された値の足し算の 回答をname="kaito"に出したいと思っています。 計算ボタンを押すとname="kaito"に答えを出したいのですが、 以下のような式ですと当然ながら全く反応しません。 javascriptまたはPHPをどう記述すれば表の値を計算できるようになるのでしょうか? できれば具体的に教えて頂ければ大変助かります。 ご教授の程よろしくお願い致します。 <script> function keisan(){document.tasizan.kaito.value=(document.tasizan.aa.value)+(document.tasizan.bb.value)} </script> <form name="tasizan"> <table width="100" border="1"> <tr> <th width="10" scope="col">a</th> <th width="10" scope="col">b</th> <th width="20" scope="col">回答</th> </tr> <tr> <td><INPUT type="text" name="aa" ></td><td><INPUT type="text" name="bb" ></td><td><INPUT type="text" name="kaito" ></td> <td><input type="button" value="計算" onclick="keisan()"/></td> </tr> <tr> <td><INPUT type="text" name="aa" ></td><td><INPUT type="text" name="bb" ></td><td><INPUT type="text" name="kaito" ></td> <td><input type="button" value="計算" onclick="keisan()"/></td> </tr> <tr> <td><INPUT type="text" name="aa" ></td><td><INPUT type="text" name="bb" ></td><td><INPUT type="text" name="kaito" ></td> <td><input type="button" value="計算" onclick="keisan()"/></td> </tr> <tr> <td><INPUT type="text" name="aa" ></td><td><INPUT type="text" name="bb" ></td><td><INPUT type="text" name="kaito" ></td> <td><input type="button" value="計算" onclick="keisan()"/></td> </tr> </table> </form>

  • tableの外枠をCSSで表示させない方法

    HTMLで作ったテーブルの外枠の縦線をCSSで表示させないようにしたいのですが可能でしょうか? HTMLはさわらずにできる限りCSSでやりたいと思い、いろいろ試してみましたがうまくいきませんでした。 線種はsolidを使いたいです。 よろしくお願いいたします。 <html> <head> <title>css table</title> <style type="text/css"> <!-- table.sample { width:550px; height:auto; border:solid 1px; border-collapse:collapse; border-left:none; border-right:none; } .sample th { width:100px; border:solid 1px; } .sample tr { border:solid 1px; } .sample td { border:solid 1px; } --> </style> </head> <body> <table class="sample" frame="hsides"> <tr> <th scope="row">&nbsp;</th> <td>&nbsp;</td> <td></td> <td></td> </tr> <tr> <th scope="row">&nbsp;</th> <td>&nbsp;</td> <td></td> <td></td> </tr> <tr> <th scope="row">&nbsp;</th> <td>&nbsp;</td> <td></td> <td></td> </tr> <tr> <th scope="row">&nbsp;</th> <td>&nbsp;</td> <td></td> <td></td> </tr> <tr> <th scope="row">&nbsp;</th> <td>&nbsp;</td> <td></td> <td></td> </tr> </table> </body> </html>

    • ベストアンサー
    • CSS
  • phpのエラーについてです

    mysql上のデータをphpでブラウザに表示するコードを書いています。 下記のプログラムを実行した所 Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampplite\htdocs\shop\index.php on line 18 というようなエラーが表示されます。 本の通りに進めているので間違っていないはずなんですが・・ どこが成立していないのでしょうか??? <?php mysql_connect('localhost', 'root', '') or die(mysql_error()); mysql_select_db('mydb'); mysql_query('SET NAMES UTF8'); $recordSet = mysql_query('SELECT m.name, i.* FROM makers m, my_ items i WHERE m.id=i.maker_id ORDER BY id DESC'); ?> <table width="100%"> <tr> <th scope="col">ID</th> <th scope="col">メーカー</th> <th scope="col">商品名</th> <th scope="col">価格</th> </tr> <?php while ($table = mysql_fetch_assoc($recordSet)) { ?> <tr> <td><?php print(htmlspecialchars($table['id'])); ?></tb> <td><?php print(htmlspecialchars($table['name'])); ?></tb> <td><?php print(htmlspecialchars($table['item'])); ?></tb> <td><?php print(htmlspecialchars($table['price'])); ?></tb> </tr> <?php } ?> </table>

    • ベストアンサー
    • PHP
  • 表の作成

    いつもお世話になっています。 下記の構文で表を作成した際に、セルの幅がずれてしまいます。 <TABLE border="1" cellspacing="0" width="940"> <TBODY> <TR> <TH height="17" width="790" colspan="5">A</TH> <TH height="17" width="75" rowspan="2">G</TH> <TH height="17" width="75" rowspan="2">H</TH> </TR> <TR> <TH height="17" width="150">B</TH> <TH height="17" width="80" >C</TH> <TH height="17" width="220">D</TH> <TH height="17" width="280">E</TH> <TH height="17" width="60" >F</TH> </TR> </TBODY> </TABLE> <TABLE border="1" cellspacing="0" width="940"> <COLGROUP> <col width="150"> <col width="80" > <col width="220"> <col width="280"> <col width="60" > <col width="75" > <col width="75" > </COLGROUP> <TBODY> <TR height="30"> <TD><FONT SIZE="-1">○</FONT></TD> <TD><FONT SIZE="-1">×</FONT></TD> <TD><FONT SIZE="-1">△</FONT></TD> <TD><FONT SIZE="-1">□</FONT></TD> <TD><FONT SIZE="-1">◆</FONT></TD> <TD align="center"> <INPUT type="button" value="◎" style="width:55px;height:27px" onClick=""> <!-- ж --> </TD> <TD align="center"> <INPUT type="button" value="Δ" style="width:55px;height:27px" onClick=""> <!-- ☆ --> </TD> </TR> </TBODY> </TABLE> どなたか解決方法を教えていただけないでしょうか? よろしくお願いいたします。

    • ベストアンサー
    • HTML
  • PHPでフォーム作成について(初歩的質問)

    PHPに関しては超初心者のものです。 テキストで学び始めたのですが、HTMLのフォームにPHPスクリプトが組み込まれた以下のようなプログラムの時、写真のようになってうまく表示されません(当然動作しません)。 経験者の方なら、おわかりになられると思うのですが、調べてもわからず困っています。 お助けをよろしくお願いいたします。 【環境】 Windows XP, XAMPP(PHP5.3/Apache2.2・・・) 【ソース】 <?php //------------------------------------------------------- // ■ 初期値設定 //------------------------------------------------------- $kname = ""; ~ (中略) ~ ?> <hr> <b>会員登録フォーム)</b><p> <form action="<?=$_SERVER["PHP_SELF"]?>" method="POST" enctype="multipart/form-data"> <table border="1" width="600" cellspacing="0" cellpadding="0"> <?php //-------------------------------------------------- // □ 会員名 //-------------------------------------------------- ?> <tr> <td align="center" width="150" bgcolor="#b2ce77">会員名</td> <td align="left" width="450"> &nbsp;<input type="text" name="kname" value="<?=$kname?>" size="40"> </td> </tr> <?php //-------------------------------------------------- // □ 住所 //-------------------------------------------------- ?> <tr> <td align="center" width="150" bgcolor="#b2ce77">住所</td> <td align="left" width="450"> &nbsp;<select name="ken"> <option value="1"<?if ($ken=="1"){echo " selected";}?>>北海道</option> <option value="2"<?if ($ken=="2"){echo " selected";}?>>青森県</option> <option value="3"<?if ($ken=="3"){echo " selected";}?>>秋田県</option> <option value="4"<?if ($ken=="4"){echo " selected";}?>>岩手県</option> </select> </td> </tr> <?php //-------------------------------------------------- // □ 性別 //-------------------------------------------------- ?> <tr> <td align="center" width="150" bgcolor="#b2ce77">性別</td> <td align="left" width="450"> &nbsp;<input type="radio" name="seibetsu" value="1"<?if ($seibetsu=="1"){echo " checked";}?>>男性 &nbsp;<input type="radio" name="seibetsu" value="2"<?if ($seibetsu=="2"){echo " checked";}?>>女性 </td> </tr> <?php //-------------------------------------------------- // □ メールアドレス //-------------------------------------------------- ?> <tr> <td align="center" width="150" bgcolor="#b2ce77">メールアドレス</td> <td align="left" width="450"> &nbsp;<input type="text" name="mail" value="<?=$mail?>" size="30"> </td> </tr> <?php //-------------------------------------------------- // □ パスワード //-------------------------------------------------- ?> <tr> <td align="center" width="150" bgcolor="#b2ce77">パスワード</td> <td align="left" width="450"> &nbsp;<input type="password" name="pw" value="<?=$pw?>" size="30"><br> &nbsp;5文字以下の半角数字を入力して下さい。 </td> </tr> <?php //-------------------------------------------------- // □ 画像ファイル //-------------------------------------------------- ?> ~ 中略 ~ <?php //-------------------------------------------------- // □ 備考 //-------------------------------------------------- ?> <tr> <td align="center" width="150" bgcolor="#b2ce77">備考</td> <td align="left" width="450"> &nbsp;<textarea name="biko" cols="60" rows="10"><?=$biko?></textarea> <input type="hidden" name="no" value="abc"> </td> </tr> </table> <br> <?php //-------------------------------------------------- // □ 登録ボタン //-------------------------------------------------- ?> <input type="submit" name="submit" value="   登録する   "> </form> </body> </html>

    • 締切済み
    • PHP

専門家に質問してみよう