添加分页代码后的结果在PHP中搞砸了

时间:2013-03-04 01:40:49

标签: php html mysql css

以下是我的代码的一部分。我创建了一个文本框,当用户在文本框中输入单词/短语时,它会搜索数据库中的关键字。此代码还包含分页代码。 我在这段代码中遇到的问题是,它第一次显示页码,但在第一次显示时,它不会显示所需的结果。当我从一个页面移动到另一个页面时,它会从变量“$ each”中丢失值,并显示数据库中的所有结果。而该代码应该只显示符合查询标准的数据部分

    <input type="text" name="k" 
        value="<?php if(isset($_GET['k'])){echo htmlentities($_GET['k']);} ?>" style="border: 1px, thin; width:92%; "/> 
    <input type="image" style="margin-bottom: 0; margin-top: 2px;" src="search.png"  value="submit" />
    </div>
    </fieldset>
</form>
</div>
<table cellpadding="0" cellspacing="0" border="1">
<tbody>    
<?php
$connection = mysql_connect('', '', '');
if(!$connection)
    echo "No database connected";
$dbase = mysql_select_db("", $connection);
if(!$dbase)
    echo "No datatable connected";

if(isset($_GET['k'])){ 
    $k1 = $_GET['k']; 
} else { 
    $k1 = ''; 
}

echo $k1;

$term = explode(" ", $k1);

$query = "SELECT * FROM kcpdatabase ";

foreach ($term as $each) {
   echo $each;
    $i++;
   if($i==1) {
        $query .= "WHERE keywords LIKE '%$each%' ";
   } else {
        $query .= "OR WHERE keywords LIKE '%$each%' ";
    }
}

echo $query;

$per_pages=3;
$page_query = mysql_query("SELECT COUNT('title') FROM kcpdatabase");

$pages = ceil(mysql_result($page_query, 0)/$per_pages)  or die($page_query."<br/><br/>".mysql_error()); //Calculating the number of pages and round it to higher side

$page = (isset($_GET['page'])) ? (int) ($_GET['page']) : 1; //Checking wheather value of page is set or not. 
                                                         //If not then assign it as a default value of 1.
$start = ($page - 1) * $per_pages; // Point to the record zero always to start displaying the data

$query .= "LIMIT $start, $per_pages";

$ourquery1 = mysql_query ($query);
if(!$ourquery1)
    echo "No query found";

    $row1 = mysql_num_rows ($ourquery1);

if($pages >= 1 && $page <= $pages){ 
   for($x = 1; $x <= $pages; $x++) {
        echo '<a href="?page='.$x.'">'.$x.'</a> ';
   }

    if ($row1 > 0) {

        while($result = mysql_fetch_assoc($ourquery1)) {
         echo "<tr>";
            echo "<td>";
            $title = $result['title'];
            $link = $result['link'];
            $region = $result['region'];
            $sector = $result['sector'];
            $theme = $result['theme'];      
            echo "<td> <a href=$link><h3>$title<h3></a>";
            echo "<h4>Sector: $sector Theme: $theme &nbsp; 
            <br> Region: $region </td> </tr>";
        }
    }   
}
echo "</tbody>";

2 个答案:

答案 0 :(得分:0)

新代码中的

Please, don't use mysql_* functions。它们已不再维护and are officially deprecated.请参阅red box?请改为了解 prepared statements ,并使用 PDO MySQLi - this article帮你决定哪个。如果您选择 PDO here is a good tutorial.

答案 1 :(得分:0)

您需要将搜索条件添加到您的分页链接(k=$k1),因为它目前仅在您提交表单时设置。尝试像 -

这样的东西
if($pages >= 1 && $page <= $pages){ 
   for($x = 1; $x <= $pages; $x++) {
        echo '<a href="?k='.$k1.'&page='.$x.'">'.$x.'</a> ';
   }

注意 - 您目前在$k1 =&gt;中使用未经过抽样的用户输入$each