PHP+MySqlでの検索

このQ&Aのポイント
  • PHP+MySqlを使用した検索ページの実装方法を教えてください。
  • 検索ページからスペース区切りで検索するとヒットしない問題が発生しています。
  • ソースコードのどの部分にSQL文を書けば実装できるでしょうか?
回答を見る
  • ベストアンサー

PHP+MySqlでの検索

検索ページから下記のソースに検索結果を表示させようと思っておりますが、スペース区切りにして検索をかけるとヒットしません。 こちらのソースのどの部分にどの様なSQL文を書けば実装出来ますでしょうか? 宜しくお願いします。 <?php require_once('Connections/tm.php'); ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $colname_search = "-1"; if (isset($_GET['search'])) { $colname_search = $_GET['search']; } mysql_select_db($database_tm, $tm); $query_search = sprintf("SELECT * FROM posts WHERE title LIKE %s ORDER BY modified DESC", GetSQLValueString("%" . $colname_search . "%", "text")); $search = mysql_query($query_search, $tm) or die(mysql_error()); $row_search = mysql_fetch_assoc($search); $totalRows_search = mysql_num_rows($search); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無題ドキュメント</title> </head> <body> <table width="583" border="1"> <tr> <th width="82" scope="col">タイトル</th> <th width="113" scope="col">メッセージ</th> <th width="111" scope="col">開始時間</th> <th width="105" scope="col">名前</th> <th width="138" scope="col">詳細</th> </tr> <tr> <td height="20"><?php echo $row_search['title']; ?></td> <td><?php echo $row_search['message']; ?></td> <td><?php echo $row_search['modified']; ?></td> <td><?php echo $row_search['n_name']; ?></td> <td><a href="request_list.php?recordID=<?php echo $row_search['id']; ?>">詳細</a></td> </tr> </table> </body> </html> <?php mysql_free_result($search); ?>

  • MySQL
  • 回答数1
  • ありがとう数0

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

  • ベストアンサー
  • yambejp
  • ベストアンサー率51% (3827/7415)
回答No.1

>スペース区切りにして検索をかけるとヒットしません 仕様が提示されていないのでなんともいえませんが (1)スペースで区切ってand検索をする →「A B」であればAとBを順不同に両方含むという意味 (2)同or検索する →「A B」であればAまたはBもしくはその両方を含むという意味 (3)スペースを含め完全一致する →「A B」であれば「A B」という文字を含むという意味 それぞれによって処理はことなります。 ざっと見た感じ今回のソースは(3)を処理しているのでしょうか?

akiaki64
質問者

補足

yambejp様 おっしゃる通りに、3の処理になっています。 and検索を行いたいのですが、いくつかのサイトや参考書を読んで試してみたのですがうまく行きませんでした。 出来ればこちらのソースをそのまま使いたいので、書き換える部分をご伝授頂ければと思っております。 よろしくお願いいたします。

関連するQ&A

  • php+MySqlでの検索ページ

    検索フォームを作成し、結果を別ページに表示したいと思っておりますが、うまくいかないので質問させて頂きます。 ・状況 検索ワードが部分一致での結果は表示できるのですが、Yahooのように複数検索すると結果が表示されません。 また、検索ワードの前後に半角スペースや全角スペースなどを入れても同様です。 ・ソース 検索フォーム側 <form id="form1" name="form1" method="get" action="search.php"> <label for="textfield"></label> <input type="text" name="title" id="textfield" /> <input type="submit" name="button" id="button" value="検索" /> </form> 検索結果側(search.php) <?php require_once('Connections/tm.php'); ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $colname_search = "-1"; if (isset($_GET['title'])) { $colname_search = $_GET['title']; } mysql_select_db($database_tm, $tm); $query_search = sprintf("SELECT * FROM posts WHERE title LIKE %s ORDER BY title DESC", GetSQLValueString("%" . $colname_search . "%", "text")); $search = mysql_query($query_search, $tm) or die(mysql_error()); $row_search = mysql_fetch_assoc($search); $totalRows_search = mysql_num_rows($search); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "ttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="ttp://www.w3.org/1999/xhtml"> <head> <meta ttp-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無題ドキュメント</title> </head> <body> <table width="725" border="1" cellspacing="0" cellpadding="0"> <tr> <th width="120" scope="col">タイトル</th> <th width="120" scope="col">内容</th> <th width="120" scope="col">名前</th> </tr> <tr> <td><?php echo $row_search['title']; ?></td> <td><?php echo $row_search['message']; ?></td> <td><?php echo $row_search['n_name']; ?></td> </tr> </table> </body> </html> <?php mysql_free_result($search); ?> ※検索結果側はテスト段階のため、まだ結果を一件のみ表示されるだけとなっています。 ネットで調べると ・全角スペースを半角に揃える ・orやandを使用する らしき所までしか分かりませんでした。。。 出来れば詳しいソースや参考サイト等がありましたらご伝授をお願い致します。

    • ベストアンサー
    • MySQL
  • 検索結果を出すためには?(phpとmysql利用で)

    ある画像を押したら、検索結果が出る方法がわかりません。 たとえば、「A」「B」「C」の画像を作成しておき、「A」の画像を押したときにMYSQLで作成したデータベースの中から「A」だけ出す方法がわかりません。(画像からリンクする方法はわかります) 「A」という画像を押すと「akekka.php」を出すように作成したのですが、できません。知恵を貸してください。 <?php $sql= "select * from jyusyo where fuk = '愛媛県'"; $result = mysql_query($sql); $rows = mysql_num_rows($result); if($rows == 0){ echo "<p>該当データがありません。</p>\n"; exit; } else { echo "<table border=\"1\">\n"; echo "<tr>\n"; echo "<th>詳細</th>\n"; echo "<th>登録日<br></th>\n"; echo "<th>府県名<br></th>\n"; echo "<th>住所<br></th>\n"; echo "<th>名前</th>\n"; echo "</tr>\n"; while($row = mysql_fetch_array($result)){ $id = $row["id"]; echo "<tr>"; echo "<a href=\"meisai.php?id=$id\">",$id,"</a>\n"; echo "</td><td>"; echo $row["sho"]; echo "</td><td>"; echo $row["day"]; echo "</td><td>"; echo $row["fuk"]; echo "</td><td>"; echo $row["jyu"]; echo "</td><td>"; echo $row["nam"]; echo "</td></tr>"; } echo "</table>\n"; } ?>

  • テーブルの情報が一部しか表示されません。(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
  • 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
  • PHP MYSQLでのページ間のやりとり

    ブログのようなサイトを作ろうと思い、PHP+MYSQLで四苦八苦しております。 ひとつのページにデータベースの一覧を表示することはできました。 ところがそのページの特定のリンク(IDなど)をクリックした後に詳細ページ(又は管理ページ)を表示させよと思ってますが、なんともうまくいきません。 ちなみに下記のリンクはDreamweaverで自動生成しました。一応動いたのですが if (!function_exists("GetSQLValueString")) { 以下から $colname_rst = "-1"; までさっぱりわかりません。 $colname_rst = "-1"; if (isset($_GET['title'])) { $colname_rst = $_GET['title']; } $query_rst = sprintf("SELECT * FROM sample WHERE title = %s", GetSQLValueString($colname_rst, "text")); のみで動作しないものでしょうか。 --------------------------------------------------------------------------------- <?php require_once('Connections/testserver.php'); ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $colname_rst = "-1"; if (isset($_GET['title'])) { $colname_rst = $_GET['title']; } mysql_select_db($database_testserver, $testserver); $query_rst = sprintf("SELECT * FROM sample WHERE title = %s", GetSQLValueString($colname_rst, "text")); $rst = mysql_query($query_rst, $testserver) or die(mysql_error()); $row_rst = mysql_fetch_assoc($rst); $totalRows_rst = mysql_num_rows($rst); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無題ドキュメント</title> </head> <body> <?php echo $row_rst['title']; ?> </body> </html>

    • 締切済み
    • PHP
  • PHPとMySQL 検索フォームを作りたい。

    PHPとMySQLを使って商品一覧を作りました。 ◆商品一覧表 http://pips.chu.jp/0/07/index.php <?php function connect() { return new PDO("mysql:dbname=★.★","★","★", array( PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET `utf8`")); } $pdo = connect(); $st = $pdo->query("SELECT * FROM shohin"); $shohin = $st->fetchAll(); ?>    <TABLE border="1"> <tr> <th>商品番号</th> <th>商品名</th> <th>分類</th> <th>販売単価</th> <th>仕入単価</th> <th>登録日</th> </tr> <?php foreach ($shohin as $g) { ?> <tr> <td><?php echo $g['id'] ?></td> <td><?php echo $g['name'] ?></td> <td><?php echo $g['bunrui'] ?></td> <td class="td-price"><?php echo number_format ($g['htanka']) ?></td> <td class="td-price"><?php echo number_format ($g['stanka'])?></td> <td><?php echo $g['day'] ?></td> </tr> <?php } ?> </TABLE> ◆特定の商品一覧表 http://pips.chu.jp/0/07/irui.php ↓ $st = $pdo->query("SELECT * FROM shohin");の部分を $st = $pdo->query("SELECT * FROM shohin WHERE bunrui='衣類' ");に変更すると 分類が衣類だけの表になります。 ◆入力フォーム http://pips.chu.jp/0/07/form.php ↓ <form action = "kensaku.php" method="post">    分類:<input type="text" name="bunrui">    <input type="submit" name="submit" value="検索"> </form> こんな感じの入力フォームを作り、分類名を入力すると、その分類名の一覧表に反映されるようにしたいです。 ◆検索された一覧表 http://pips.chu.jp/0/07/kensaku.php ↓ <?php function connect() { return new PDO("mysql:dbname=★.★","★","★", array( PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET `utf8`")); } $pdo = connect(); $st = $pdo->query("SELECT * FROM shohin WHERE bunrui='?' "); $shohin = $st->fetchAll(); ?>    <TABLE border="1"> <tr> <th>商品番号</th> <th>商品名</th> <th>分類</th> <th>販売単価</th> <th>仕入単価</th> <th>登録日</th> </tr> <?php foreach ($shohin as $g) { ?> <tr> <td><?php echo $g['id'] ?></td> <td><?php echo $g['name'] ?></td> <td><?php echo $g['bunrui'] ?></td> <td class="td-price"><?php echo number_format ($g['htanka']) ?></td> <td class="td-price"><?php echo number_format ($g['stanka'])?></td> <td><?php echo $g['day'] ?></td> </tr> <?php } ?> </TABLE> 「入力フォーム」と「検索された一覧表」のソースの書き方がわかりません。 どのようにけば実現するでしょうか。 まだ初心者なので、本やサイトを調べながら見よう見まねでやっています。 具体的に、どの部分を書き換えればよいか教えていただければ有難いです。 よろしくお願いします。

  • 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
  • style=displayでの表示/非表示切り替え

    テーブルで表示させている内容をJavaScriptを使って行ごとに表示/非表示の切り替えができるようにしたのですが、表示させた時、IEでは通常のテーブル表示のように表示されるのですが、FirefoxやSafariでは何故かテーブルの一番左の<tr>要素内に全ての<td>要素が入る形で表示されてしまいます。多分CSSの書き方に関係していると思われるのですが、どなたか分かる方アドバイスをお願い致します。 下記は、コードの抜粋です。 [CSS] (略) #content { padding: 10px 0; width: 780px; float: left; } #inquiry { width: 600px; /* ボックスの幅を指定 */ margin-left: auto; margin-right: auto; } #inquiry table { border-top: 1px solid #CCCCCC; border-bottom: none; border-left: none; border-right: 1px solid #CCCCCC; font-size: 100%; width: 100%; } #inquiry td { border-top: none; border-bottom: 1px solid #CCCCCC; border-left: 1px solid #CCCCCC; border-right: none; padding: 10px; text-align: center; } #inquiry th { border-top: none; border-bottom: 1px solid #CCCCCC; border-left: 1px solid #CCCCCC; border-right: none; background-color: #F3F3F3; font-weight: normal; padding: 10px; } (略) [HTML] (略) <script type="text/javascript" src="../../webroot/js/prototype.js"></script> <script type="text/javascript"> <!-- function OnScreenHelp(id){ var elem = document.getElementsByClassName(id); for(var i=0;i<elem.length;i++) { elem[i].style.display = elem[i].style.display == "none" ? "block" : "none"; } } //--> </script> (略) <table border="border" summary="購入履歴" cellspacing="0"> <caption><h3>購入履歴</h3></caption> <tr> <th scope="col"><label for="name">注文番号</label></th> <th scope="col"><label for="name">ご注文日</label></th> <th scope="col"><label for="name">合計金額</label></th> <th scope="col"><label for="name">備考</label></th> </tr> <?php foreach($data1 as $row1) { $id = $row1['Order']['id']; ?> <tr onclick="OnScreenHelp(<?php echo $id; ?>)"> <td><?php echo h($row1['Order']['id']); ?></td> <td><?php echo h($row1['Order']['orderdate']); ?></td> <td><?php echo h($row1['Order']['total']); ?></td> <td><input type="text" name="name" size="30" id="name" class="text1" value="送料込み" /></td> </tr> <?php } ?> </table><br /> <table border="border" summary="明細情報" cellspacing="0"> <caption><h3>明細</h3></caption> <tr> <th scope="col"><label for="customerno">商品番号</label></th> <th scope="col"><label for="name">商品名</label></th> <th scope="col"><label for="price">価格</label></th> <th scope="col"><label for="quantity">数量</label></th> <th scope="col"><label for="subtotal">小計</label></th> </tr> <?php foreach($data2 as $row2) { $id = $row2['Orderitem']['order_id']; ?> <tr class="<?php echo $id; ?>" style="display:none;"> <td><?php echo h($row2['Product']['productno']); ?></td> <td><?php echo h($row2['Product']['name']); ?></td> <td><?php echo h($row2['Product']['price']); ?></td> <td><?php echo h($row2['Orderitem']['quantity']); ?></td> <td><?php echo h($row2['Orderitem']['subtotal']); ?></td> </tr> <?php } ?> </table> (略)

    • ベストアンサー
    • CSS
  • php+MySqlでの検索結果について

    以下のソースで検索結果が表示されるのですが、検索結果が多数ある場合、例えば10件ずつ表示して「次へ」「前へ戻る」「最終頁」等のリンクを貼る方法が分かりません。 また、検索結果から並べ替え(データの更新日時等から)も出来ればと思い質問させていただきました。 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "ttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>検索</title> </head> <body> <?php # 入力値に不正なデータがないかなどをチェック。 function checkInput($var) { if (is_array($var)) { return array_map('checkInput', $var); } else { if (get_magic_quotes_gpc()) { // magic_quotes_gpc対策 $var = stripslashes($var); } if (preg_match('/\0/', $var)) { // NULLバイト攻撃対策 die('不正な入力です。'); } if (!mb_check_encoding($var, 'UTF-8')) { // 文字エンコードの確認 die('不正な入力です。'); } return $var; } } function h($string) { // HTMLでのエスケープ処理をする return htmlspecialchars($string, ENT_QUOTES); } # POSTされたデータをチェック。 $_POST = checkInput($_POST); // データベース設定 $dbServer = 'localhost'; $dbUser = 'user'; $dbPass = 'password'; $dbName = 'sample'; $flag = TRUE; // MySQLデータベースに接続 if (!$link = mysql_connect($dbServer, $dbUser, $dbPass)) { $flag = FALSE; } // データベース選択 else if (!mysql_select_db($dbName, $link)) { $flag = FALSE; } // 文字エンコードの指定(PHP5以降かつMySQL 4.1以降) else if (!mysql_set_charset('utf8', $link)) { $flag = FALSE; } # $flagの値の判定 if ($flag === FALSE) { echo 'データベースエラー'; } else if (isset($_POST['data'])) { $data = $_POST['data']; $dataList = explode(' ', mb_convert_kana($data, 's')); $sql = 'SELECT * FROM search WHERE 1'; foreach ($dataList as $word) { if ($word == '') continue; $sql .= sprintf(" AND data LIKE '%s'", '%' . mysql_real_escape_string($word) . '%'); } echo '<p>SQL: ' . h($sql) . '</p>'; $query = mysql_query($sql, $link); if (!$query) { echo 'データベースエラー'; } else if (mysql_num_rows($query) == 0) { echo '<p>「' . h($data) . '」はデータベースに登録がありません。</p>'; } else { echo '<p>「' . h($data) . '」はデータベースに登録がありました。</p>'; echo '<p>検索結果</p>'; echo '<table border="1">'; echo ' <tr>'; echo ' <th>ID</th>'; echo ' <th>データ</th>'; echo ' </tr>'; while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) { echo ' <tr>'; echo ' <td>' . h($row['id']) . '</td>'; echo ' <td>' . h($row['data']) . '</td>'; echo ' </tr>'; } echo '</table>'; } $sql = 'SELECT * FROM search'; $query = mysql_query($sql, $link); if (!$query) { echo 'データベースエラー'; } else { echo '<p>データベースの内容一覧</p>'; echo '<table border="1">'; echo ' <tr>'; echo ' <th>ID</th>'; echo ' <th>データ</th>'; echo ' </tr>'; while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) { echo ' <tr>'; echo ' <td>' . h($row['id']) . '</td>'; echo ' <td>' . h($row['data']) . '</td>'; echo ' </tr>'; } echo '</table>'; } } ?> <form method="post" action=""> <p>検索ワードを入力して下さい</p> <input type="text" name="data" /> <input type="submit" value="検索する" /> </form> </body> </html> このソースのどの部分に記述してよいのか等も含めてご伝授頂きたく思います。 宜しくお願いします。

    • ベストアンサー
    • MySQL