Hi,

I am trying to run a search on mysql

which is coming through but i am getting a duplicate on the number of rows to show per page

the code is

    <?php



// get data

$button = $_GET['submit'];

$search = $_GET['search'];
if (!$button)
echo "You didn't submit a keyword";
else
{
if (strlen($search)<=2)
echo "search term too short.";
else
{
echo "you search for $search";
}
mysql_connect("localhost","user","password");
mysql_select_db("varrscom_search");



{
$search_exploded = explode(" ",$search);
foreach($search_exploded as $search_each)
{
$x++;
if ($x==1)
$construct .= "keywords LIKE '%$search_each%'";
else
$construct .= "OR keywords LIKE '%$search_each%'";


}

}
$construct = "SELECT * FROM searchengine WHERE $construct";
$run = mysql_query($construct);

$foundnum = mysql_num_rows($run);

if ($foundnum==0)
echo "no results found.";
else
{
echo "$foundnum results found!<p>";
while ($runrows = mysql_fetch_assoc($run))
{
$title = $runrows['title'];
$desc = $runrows['description'];
$url = $runrows['url'];

echo "
<b>$title</b><br>
$desc<br>
<a href='$url'>$url</a><p>
";




}
}
$per_page = 2;

$start = $_GET['start'];

$record_count = mysql_num_rows(mysql_query("SELECT * FROM searchengine"));

$max_pages = $record_count / $per_page;

if (!$start)
    $start = 0;



$get = mysql_query("SELECT * FROM searchengine LIMIT $start, $per_page");
while ($row = mysql_fetch_assoc($get))





$pre = $start - $per_page;
$next = $start + $per_page;


if (!($start<=0))
echo "<a href='index.php?start=$prev'>Prev</a>";

$i=1;

for ($x=0;$x<$record_count;$x=$x+$per_page)

{
    if(!$start!=$x)
    echo "<a href='index.php?start=$x'>$i</a>";
else
    echo "<a href='index.php?start=$x'><b>$i</b></a>";
echo 
$i++;
}


if (!($start>=$record_count-$per_page))
echo "<a href='index.php?start=$next'>Next</a>";
}


?>

im testing @ http://varriastudios.com/phpsearcheingie/ in there type search engine to understand my problem as far as the duplicate goes

All feedback will be appreciated

thank you Aroldo

2 answers

o.k.w 2355
1
point

First of all, the result returned was not paginated as your first query just perform a non-limiting select based on the keywords. The result displayed is the correct behaviour.

Secondly, your pagination query to display page numbers and links are correct (though not optimised) is correct too. The double display of the page number is because of the line:

echo 
$i++;

It prints out the increment, hence the additional number after each page number link.

I'm not too sure if I fully understood and addressed your question though. Did i?

:)

Answered about 7 years ago by o.k.w
  • the 1st query is running but my confusion is $get = mysql_query("SELECT * FROM searchengine LIMIT $start, $per_page"); I think my problem is that im making to queries & need to fix the code up some more My main question is why is there not a # 2 page after a click on the link ? thanks in advanced Aroldo Chavarria about 7 years ago
  • @Aroldo - This hurts my head to look at. You need to seriously rethink the logic of this code. First you search for key words and then you do another search for pagination that doesn't involve the result set you already have. What the heck? Why would you want pagination that goes through every single database entry, instead of going through the result set? The fact that there is not a #2 page is the least of your worries. Start over. Get the logic right first. Then code it out. Abinadi Ayerdis about 7 years ago
  • @Aroldo - And while I'm thinking about it, part of the reason your code is hard to follow (and it is hard to follow) is because you do not have any consistent formatting. Make it more readable and don't be afraid to add more than one comment (// get data -- please). Abinadi Ayerdis about 7 years ago
  • @o.k.w. - You deserve a gold star for your answer. I'm not sure if it really tells Aroldo what he wants to know or not, but kudos to you for wading through that code. +1 Abinadi Ayerdis about 7 years ago
  • @Abinadi: Thanks for the comment, I haven't been following this post for past few days, didn't notice the new activities. Ok, basically the logic issue is with the second query. If it's supposed to be for pagination, the consition should be the same but without limiting the return count. The current second query returns everything from the table and has got nothing to do with the keyword search. I probably can rewrite the code but the entire structure of the code is rather 'patchy' or inconsistent. Aroldo, if you understand what I meant and would like me to code the entire thing for you, let me know :) o.k.w almost 8 years ago
0
points

The reason why I am using LIKE and OR is to make sure the query runs & gives a result for every keyword found , not just 1 row.

I never did get around this so I am going to have to search for a script that can do what i am trying to on here.

There is an upcoming system I am doing that is flash based & it will required a search function.

After looking at this code again, I think I just got a new headache.

Thank you

Answered almost 7 years ago by Aroldo Chavarria