在表中搜索结果导致表中的不合适数据。

时间:2017-07-15 08:00:53

标签: php mysqli

大家好,我在制作这个剧本时遇到了麻烦。每当我使用搜索栏以表格形式在我的网站上搜索数据时,第一行总是完全回复数据,但是在第一行之后的所有数据都在其他行中不合适。我能够在非桌面结构情况下完美地做到这一点,所以我不知道为什么这样做我需要帮助我如何保持所有数据的完整。

Screen Shot

    <?php 
//SEARCHX
include("hidden_path/mysqli/procedural/session/session_crud/v1/0/instructions/php/session_and_connection.php");
$output = ' ';

if(isset($_GET['search']) && $_GET['search'] !== ' ') {
$user_input = $_GET['search'];


if ($user_input === "") {
header('Location: '.$_SERVER['PHP_SELF']);
die;
}

//PAGINATION
$user_input = $_GET['search'];
$and = "&";


$pagevx = "page=";

//SEARCHX
$user_input = trim(" $user_input ");

$user_input = preg_replace('/\s+/', ' ', $user_input);

$user_input = mysqli_real_escape_string($connect, $user_input);

$user_input = htmlspecialchars($user_input);

//PAGINATION
$page =  mysqli_query($connect, "SELECT COUNT(*) FROM posts WHERE post_title LIKE '%$user_input%' OR post_content LIKE '%$user_input%'");
// total row count
$row = mysqli_fetch_row($page);
$rows = $row[0];
// results displayed per page
$page_rows = 2;
// page number of last page
$last = ceil($rows/$page_rows);
// makes sure $last cannot be less than 1
if($last < 1) {
$last = 1;
}
// page num
$pagenum = 1;
// get pagenum from URL if it is present otherwise it is 1
if(isset($_GET['page'])) {
$pagenum = preg_replace('#[^0-9]#', '', $_GET['page']);
}
// makes sure the page number isn't below 1, or more then our $last page
if($pagenum < 1) {
$pagenum = 1;
}
else if($pagenum > $last) {
$pagenum = $last;
}
// set the rage of rows to query for the chosen $pagenum
$limit = 'LIMIT ' . ($pagenum - 1) * $page_rows . ',' . $page_rows;
$page = mysqli_query($connect, "SELECT * FROM posts WHERE post_title LIKE '%$user_input%' OR post_content LIKE '%$user_input%' ORDER BY post_id DESC $limit");



// establish $paginationCtrls variable
$paginationCtrls = '';
// if more the 1 page
if($last != 1) {
if($pagenum > 1) {
$previous = $pagenum - 1;
$paginationCtrls .= '<a href="'. $row["post_title"].'?search='.$user_input.$and.$pagevx.$previous.'"><span class="pag_back_arrow"; style="text-decoration: none;"><</a></span> &nbsp; &nbsp; ';
// Render clickable number links
for($i = $pagenum - 4; $i < $pagenum; $i++) {
if($i > 0) {
$paginationCtrls .= '<a href="'. $row["post_title"].'?search='.$user_input.$and.$pagevx.$i.'">'.$i.'</a> &nbsp; ';
}
}
}
// render the target page number without a link
$paginationCtrls .= ''. $pagenum . ' &nbsp; ';
// render clickable number links that appear on the right
for($i = $pagenum + 1; $i < $last; $i++) {
$paginationCtrls .= '<a href="'. $row["post_title"].'?search='.$user_input.$and.$pagevx.$i.'">'.$i.'</a> &nbsp; ';
// allows up to 4 pages
if($i >= $pagenum + 4) {
break;
}
}
if($pagenum != $last) {
$next = $pagenum + 1;
$paginationCtrls .= ' &nbsp; <a href="'. $row["post_title"].'?search='.$user_input.$and.$pagevx.$next.'"><span class="pag_next_arrow"; style="text-decoration: none;">></a></span> ';
}
}

//SEARCHX

?>

<!DOCTYPE html>
<html>
<head>

<title>
Results
</title>

</head>

<meta name="viewport" content="width=device-width">
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="0/instructions/css/query/desktop.css">
<link rel="stylesheet" type="text/css" href="0/instructions/css/query/mobile.css">

<body>



<?php
//SEARCHX
$c = mysqli_num_rows($page);
if($c == 0) {
$output = '<h2 class="no_results_error_message";><span style="color: red;">No results for: </span><b><span style="color: white;">' . $user_input . '</span></h2></b>';


}  else {

?>


<div class="result_section";>
<h2><span class="for_headline">Results For: </span><span id="result_output"><?php $outputx = "$user_input"; print("$outputx"); ?></span></h2>
</div>


<!-- Search Box -->
<?php include("search_box.php"); ?> <br>

<table width='80%' border=0>
<tr bgcolor='#CCCCCC'>
<td>user_id</td>
<td>topic_id</td>
<td>post_title</td>
<td>post_content</td>
<td>post_date</td>
<td>Update</td>
</tr>

<?php

// shows the user what page they are on, and the total number of pages
$textline1 = "Search_i";
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";


while($row = mysqli_fetch_array($page, MYSQLI_ASSOC)) {     
echo "<tr>";
echo "<td>".$row['user_id']."</td>";
echo "<td>".$row['topic_id']."</td>";
echo "<td>".$row['post_title']."</td>";
echo "<td>".$row['post_content']."</td>";
echo "<td>".$row['post_date']."</td>";

echo "<td><a href=\"edit.php?post_id=$row[post_id]\">Edit</a> | <a href=\"delete.php?post_id=$row[post_id]\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td></table>";       

?>

<?php

$output .= '<a href="' . $link . '">

</a>';


}
}
} else {
header("location: ./");
}

print("$output");

mysqli_close($connect);

?>

<!-- PAGINATION -->
<!--shows the user what page they are on, and the total number of pages -->
<p><?php //echo $textline1 = "Search_i"; ?></p>
<p id="page_of";><?php //echo $textline2; ?></p>

<div class="pagination_controls"><?php echo $paginationCtrls; ?></div>


</body>
</html>

1 个答案:

答案 0 :(得分:0)

你正在循环关闭表。使用

更改您的代码
(value != newValue)