所以我有一个大型数据库(120k行),我正在构建一个小型Web应用程序,以使其对社区有用(基于天文学的东西)。我是php的新手,我正在寻求一些帮助。 我有一个search.php文件和一个class-search.php文件,目前结果如下:
2 results found
Array
(
[count] => 2
[results] => Array
(
[0] => stdClass Object
(
[disc] => LOC 7
[fstdate] => 2016
[lstdate] => 2016
[lstpa] => 170
[lstsep] => 0.4
[stype] =>
[comp] => AB
[fstmag] => 13.80
[secmag] => 14.10
[dnum] =>
[rahour] => 05
[ramin] => 38
[rasec] => 48.04
[decdeg] => -02
[decmin] => 27
[decsec] => 14.2
)
[1] => stdClass Object
(
[disc] => LOC 7
[fstdate] => 2016
[lstdate] => 2016
[lstpa] => 284
[lstsep] => 7.5
[stype] =>
[comp] => AB,C
[fstmag] => 13.20
[secmag] => 16.60
[dnum] =>
[rahour] => 05
[ramin] => 38
[rasec] => 48.04
[decdeg] => -02
[decmin] => 27
[decsec] => 14.2
)
)
)
我希望将结果列表,我对我正在做的事情有一个粗略的了解,但我并没有把所有事情都搞定 - 有人可以提出任何建议。 search.php的关键部分如下所示:(代码可能不完全准确,因为我已经取出了一些开关和ifs以减少帖子长度)
<?php
//Check if search data was submitted
if ( isset( $_GET['s'] ) ) {
// Include the search class
require_once( dirname( __FILE__ ) . '/class-search.php' );
// Instantiate a new instance of the search class
$search = new search();
// Store search term into a variable
$search_term = htmlspecialchars($_GET['s'], ENT_QUOTES);
// Send the search term to our search class and store the result
$search_results = $search->search($search_term);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>WDSC search</title>
</head>
<body>
<h1>Search the WDSC</h1>
<div class="search-form">
<form action="" method="get">
<div class="form-field">
<label for="search-field">Search term</label>
<input type="search" name="s" placeholder="Enter your search term..." results="5" value="<?php echo $search_term; ?>">
<input type="submit" value="Search">
........
</form>
</div>
<?php if ( $search_results ) : ?>
<div class="results-count">
<p><?php echo $search_results['count']; ?> results found</p>
</div>
<div class="results-table">
<?php foreach ( $search_results['results'] as $search_result ) : ?>
<div class="result">
<p><?php echo $search_result->title; ?></p>
</div>
<?php endforeach; ?>
</div>
<div class="search-raw">
<pre><?php print_r($search_results); ?></pre>
</div>
<?php endif; ?>
</body>
</html>
并且class-search.php的关键部分如下所示:
<?php
/**
* Performs a search
*
* This class is used to perform search functions in a MySQL database
*
*/
class search {
/**
* MySQLi connection
* @access private
* @var object
*/
private $mysqli;
/**
* Constructor
*
* This sets up the class
*/
public function __construct() {
// Connect to our database and store in $mysqli property
$this->connect();
}
/**
* Database connection
*
* This connects to our database
*/
private function connect() {
$this->mysqli = new mysqli( 'server', 'user', 'pass', 'db' );
}
/**
* Search routine
*
* Performs a search
*
* @param string $search_term The search term
*
* @return array/boolen $search_results Array of search results or false
*/
public function search($search_term) {
// Sanitize the search term to prevent injection attacks
$sanitized = $this->mysqli->real_escape_string($search_term);
// Define variable for search category from list selection
$cat = $_GET['category'];
// Run the query.
// Query for discoverer
$query = $this->mysqli->query("
SELECT disc, fstdate, lstdate, lstpa, lstsep, stype, comp, fstmag, secmag, dnum, rahour, ramin, rasec, decdeg, decmin, decsec
FROM WDS_CAT
WHERE $cat LIKE '{$sanitized}%'
");
}
// Check results
if ( ! $query->num_rows ) {
return false;
}
// Loop and fetch objects
while( $row = $query->fetch_object() ) {
$rows[] = $row;
}
// Build our return result
$search_results = array(
'count' => $query->num_rows,
'results' => $rows,
);
return $search_results;
}
}
endif;
?>
我试图通过
将表格链接到变量$ search_resultsecho '<table border=1px>'; // opening table tag
echo'<th>Discoverer</th><th>First year observed</th><th>Last year observed</th><th>Last position angle</th><th>Last separation</th><th>Spectral type</th><th>Components</th><th>Primary Magnitude</th><th>Secondary magnitude</th><th>Durchmusterung number</th><th>Right ascension</th><th></th><th></th><th>Declination</th><th></th><th></th>'; //table headers
while($data = mysql_fetch_array($search_results))
{
// we are running a while loop to print all the rows in a table
echo'<tr>'; // printing table row
echo '<td>'.$data['disc'].'</td><td>'.$data['fstdate'].'</td><td>'.$data['lstdate'].'</td><td>'.$data['lstpa'].'</td><td>'.$data['lstsep'].'</td><td>'.$data['stype'].'</td><td>'.$data['comp'].'</td><td>'.$data['fstmag'].'</td><td>'.$data['secmag'].'</td><td>'.$data['dnum'].'</td><td>'.$data['ragiyr'].'</td><td>'.$data['ramin'].'</td><td>'.$data['rasec'].'</td><td>'.$data['decdeg'].'</td><td>'.$data['decmin'].'</td><td>'.$data['decsec'].'</td>'; // we are looping all data to be printed till last row in the table
echo'</tr>'; // closing table row
}
echo '</table>'; //closing table tag
?>
<?php endif; ?>
但没有快乐 - 任何人都可以提出一些建议。
提前致谢
答案 0 :(得分:0)
假设您只是想在HTML表格中输出结果......?
更新:
foreach($search_results['results'] as $resultRow) {
var_dump($resultRow);
}
这应该可以解决问题。您将已处理的结果集返回到数组中,因此您只需要循环该数组而不再调用mysql_fetch_array()
我先添加
var_dump($data);
exit();
之后
while($data = mysql_fetch_array($search_results))
{
只循环一次,看看每个循环返回的内容。它可能只返回[count]和[results]数组,因此您可能需要在[results]数组上再次循环。查看mysql_fetch_array http://php.net/manual/en/function.mysql-fetch-array.php
的PHP参考e.g。
while($data = mysql_fetch_array($search_results))
{
if(is_array($data['results'])){
foreach($data['results'] as $resultRow) {
var_dump($resultRow);
}
}
在foreach循环中,您可以像上面一样添加HTML输出
注意:所有这些都是基于mysql_fetch_array()根据您的第一个代码片段返回数据结构的假设。
2 results found
Array
(
[count] => 2
[results] => Array
(
[0] => stdClass Object
(
答案 1 :(得分:0)
固定!!
我最终开启了我的大脑,并确定从转储返回的是一个对象而不是数组,因此我使用$resultsArray = (array)$resultRow;
将其转换为数组并将其运行到表中。
这很脏,但现在正在做我想做的事。谢谢@JI-Web,没有你的帮助,我无法做到。我想我现在已经掌握了PHP。 根据结果将第3方源的相关数据分组并绘制到帧中。我可能会回来寻求更多帮助!