Я пытаюсь получить результаты моей страницы поиска в пределах <table>
. Этот код действительно возвращает нужные мне данные, я просто не знаю, как его структурировать, чтобы выводить его так, как я хочу.
<?php include('connection.php'); $error = array(); $results = array(); if (isset($_GET['search'])) { $searchTerms = trim($_GET['search']); $searchTerms = strip_tags($searchTerms); if (strlen($searchTerms) < 3) { $error[] = "Search terms must be 3 or more characters."; }else { $searchTermDB = mysql_real_escape_string($searchTerms); } if (count($error) < 1) { $searchSQL = " SELECT ITEM_NUMBER, ITEM_NAME, VENDOR, CONCAT('$', FORMAT(WHOLESALE_PRICE, 2)) AS WHOLESALE_PRICE, CONCAT('$', FORMAT(RETAIL_PRICE, 2)) AS RETAIL_PRICE, CONCAT('$', FORMAT(RETAIL_PRICE - WHOLESALE_PRICE, 2)) AS PROFIT, DESCRIPTION FROM PRODUCTS WHERE "; $types = array(); if (count($types) < 1) $types[] = "`ITEM_NUMBER` LIKE '%{$searchTermDB}%' OR `ITEM_NAME` LIKE '% {$searchTermDB}%' OR `VENDOR` LIKE '%{$searchTermDB}%'"; $searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `ITEM_NUMBER`"; $searchResult = mysql_query($searchSQL) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}"); if (mysql_num_rows($searchResult) < 1) { $error[] = "The search term provided {$searchTerms} yielded no results."; }else { $results = array(); $i = 1; while ($row = mysql_fetch_assoc($searchResult)) { $results[] = " {$i})<br /> Item Number : {$row['ITEM_NUMBER']}<br /> Item Name : {$row['ITEM_NAME']}<br /> Vendor : {$row['VENDOR']}<br /> Wholesale Price : {$row['WHOLESALE_PRICE']}<br /> Retail Price : {$row['RETAIL_PRICE']}<br /> Profit : {$row['PROFIT']}<br /> Description : {$row['DESCRIPTION']}<br /><br />"; $i++; } } } } function removeEmpty($var) { return (!empty($var)); } ?> <html> <title>Search Products</title> <style type="text/css"> #error { color: red; } </style> <body> <?php echo (count($error) > 0)?"The following had errors:<br /><span id=\"error\">" . implode("<br />", $error) . "</span><br /><br />":""; ?> <form method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>" name="searchForm"> Search For (Item Number, Name or Vendor) : <input type="text" name="search" value="<?php echo isset($searchTerms)?htmlspecialchars($searchTerms):''; ?>" /><br /> <input type="submit" name="submit" value="Search!" /><input type="reset" value="Reset" name="Reset" /> </form> <?php echo (count($results) > 0)?"Your search term: {$searchTerms} <br /> Returned:<br /><br />" . implode("", $results):""; ?> </body> </html>
в<?php include('connection.php'); $error = array(); $results = array(); if (isset($_GET['search'])) { $searchTerms = trim($_GET['search']); $searchTerms = strip_tags($searchTerms); if (strlen($searchTerms) < 3) { $error[] = "Search terms must be 3 or more characters."; }else { $searchTermDB = mysql_real_escape_string($searchTerms); } if (count($error) < 1) { $searchSQL = " SELECT ITEM_NUMBER, ITEM_NAME, VENDOR, CONCAT('$', FORMAT(WHOLESALE_PRICE, 2)) AS WHOLESALE_PRICE, CONCAT('$', FORMAT(RETAIL_PRICE, 2)) AS RETAIL_PRICE, CONCAT('$', FORMAT(RETAIL_PRICE - WHOLESALE_PRICE, 2)) AS PROFIT, DESCRIPTION FROM PRODUCTS WHERE "; $types = array(); if (count($types) < 1) $types[] = "`ITEM_NUMBER` LIKE '%{$searchTermDB}%' OR `ITEM_NAME` LIKE '% {$searchTermDB}%' OR `VENDOR` LIKE '%{$searchTermDB}%'"; $searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `ITEM_NUMBER`"; $searchResult = mysql_query($searchSQL) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}"); if (mysql_num_rows($searchResult) < 1) { $error[] = "The search term provided {$searchTerms} yielded no results."; }else { $results = array(); $i = 1; while ($row = mysql_fetch_assoc($searchResult)) { $results[] = " {$i})<br /> Item Number : {$row['ITEM_NUMBER']}<br /> Item Name : {$row['ITEM_NAME']}<br /> Vendor : {$row['VENDOR']}<br /> Wholesale Price : {$row['WHOLESALE_PRICE']}<br /> Retail Price : {$row['RETAIL_PRICE']}<br /> Profit : {$row['PROFIT']}<br /> Description : {$row['DESCRIPTION']}<br /><br />"; $i++; } } } } function removeEmpty($var) { return (!empty($var)); } ?> <html> <title>Search Products</title> <style type="text/css"> #error { color: red; } </style> <body> <?php echo (count($error) > 0)?"The following had errors:<br /><span id=\"error\">" . implode("<br />", $error) . "</span><br /><br />":""; ?> <form method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>" name="searchForm"> Search For (Item Number, Name or Vendor) : <input type="text" name="search" value="<?php echo isset($searchTerms)?htmlspecialchars($searchTerms):''; ?>" /><br /> <input type="submit" name="submit" value="Search!" /><input type="reset" value="Reset" name="Reset" /> </form> <?php echo (count($results) > 0)?"Your search term: {$searchTerms} <br /> Returned:<br /><br />" . implode("", $results):""; ?> </body> </html>
Желаемый вид – это нечто подобное:
--------------------------------- |Item Number : | 0000-0001 | |-------------------------------- |Item Name : | test | |-------------------------------- |Vendor : | me | |-------------------------------- |Wholesale Price : | $10.00 | |-------------------------------- |Retail Price : | $20.00 | |-------------------------------- |Profit : | $10.00 | |-------------------------------- |Description : | Test | |--------------------------------
Я не делал PHP примерно за 4 или 5 лет, я даже не близок к тому, что вы назвали бы промежуточным. Я действительно не знаю, где это сделать и как в коде. Этот код – это много разных фрагментов, которые я нашел и озадачил вместе, чтобы работать так, как сейчас.