Jump to content

PHP Pagination and Search Issue


Phantaz

Recommended Posts

Greetings All!  New to PHP, and trying to learn as much as I can as quickly as I can for a client.  I have been following some of  Tutorials i understand most of it, but I still don't understand a few of the functions. 

 

Anyways, here is my problem.  I am trying to combined the two tutorials (Pagination and Search Internal)  but I seem to have myself in a loop...

 

Code is as follows:

    <?php

$connect = mysql_connect("localhost","root","~~~~");
$db = mysql_select_db("~~~");

$button = $_GET['submit'];
$search = $_GET['search'];

if ($button)

{
	if (strlen($search)<=0)
		echo "Please enter a keyword";

	else
	{
		//explode our search terms
		$search_exploded = explode(" ",$search);
		foreach($search_exploded as $search_each)
		{
			//construct query
			$x++;
			if ($x==1)
				$construct .= "keywords LIKE '%$search_each%' OR vid_state LIKE '%$search_each%' OR users LIKE '%$search_each%' OR title LIKE '%$search_each%'";
			else
				$construct .= " OR keywords LIKE '%$search_each%' OR vid_state LIKE '%$search_each%' OR users LIKE '%$search_each%' OR title LIKE '%$search_each%'";
		}

	//eco out construct

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

	$foundnum = mysql_num_rows($run);

	//max display per page
        $per_page = 3;
        
        //get start variable
        $start = $_GET['start'];
        
        //count records
        $record_count = $foundnum;
        
        //count max pages
        $max_pages = $record_count / $per_page; //may come out as decimal
	$pagenumbers = intval($max_pages) + 1;

	if (!$start)
            $start = 0;
            
        //setup previous and next variables
        $prev = $start - $per_page;
        $next = $start + $per_page;
        
        //show prev button
        if (!($start<=0))
            echo "<a href='searchvideos.php?start=$prev'><img src='../images/texts/previous.png' border='0'></a> ";
        
	//show next button
        if (!($start>=$record_count-$per_page))	
            echo " <a href='searchvideos.php?start=$next'><img src='../images/texts/next.png' border='0'></a>";
        
        
        //set variable for first page
        $i=1;
        
        for ($y=0;$y<$record_count;$y=$y+$per_page)
        {
            if($start!=$y)
                echo " <a href='searchvideos.php?start=$y'><span class='searchtext'>$i</span></a>";
            else
                echo " <a href='searchvideos.php?start=$y'><span class='searchtext'> <font size='+1'><b>$i</b></font></span></a>";
		$i++; 
        }
	//show page numbers
        echo " / <font color='#FF6600' size='-1'>$pagenumbers</font>
	 	</td>
		<td align='right' width='50%'>
			<form action='../pages/searchvideos.php' method='get'>
			<span class='searchtext'>Search Videos</span> <input name='search' type='text' value='by Keywords, Title, Name, or State' size='35' /><input type='submit' name='submit' value='search' />
			</form>
		</td>
	</tr>
	</table>
	</div>
	<br /><br /><div class='videotitle'><span class='createtext'>  Videos </span></div>
	<br /><br />";

	if ($foundnum==0)
		echo "No results found.";

	else
	{
		echo "$foundnum results found!<br><br>";

		$query = mysql_query("SELECT * FROM video_url ORDER BY RAND() LIMIT $start, $per_page");

		while($row = mysql_fetch_array($query))
		//while ($numrows = mysql_fetch_assoc($run))
		{
			$title = $row['title'];
			$url = $row['url'];
			$state = $row['vid_state'];
			$users = $row['users'];

			echo "
			<table bgcolor='#191919' cellpadding='0' cellspacing='0' background='../images/videobgs.png' align='center'>
				<tr>
					<td width='10px'></td>
					<td width='440px' height='370px' rowspan='6'>
					$url
					</td>
					<td width='340px' align='left'>
					  <span class='createtext'>$title</span><hr />
					  $users<br />
					  $state
					</td>
					<td width='5px'></td>
				</tr>
				<tr><td> </td></tr>
				<tr><td> </td></tr>
				</table><br />";
		}
	}
	}
}
?>
   

 

I know my code is dirty, and I have a lot to learn, and that I should probably be doing this a different way, but please try to correct the way I have it. 

 

Anyways..first page loads great, the search works great, the pagination works as it should (correct pages, correct page numbers, etc.).  However, once I click on the "next" or the next "number" the next page is blank because the very first if statement throws a kink in my code...

 

if ($button) - when continuing to the next page, a "$button" is not pressed and therefore doesn't continue the code, leaving the page blank.

 

Any clues on how to fix this?  I have tried combining the two in many different ways, this is the best i got so far...

 

Thanks in advance,

Phantaz

Link to comment
Share on other sites

Hey. Well, there's no need to do

 

if ($button) ...

 

Why not just do

 

if ($search)...?

 

(remember to escape your $_GET data, at the moment this script is potentially vulnerable)

 

And then you need to plug the search keywords into your url, like:

 

 

<a href='searchvideos.php?start=$y&search=$_GET['search']'><span class='searchtext'>$i</span></a>";

 

Not sure if it'll work like that, with the array inserted in there, but get the idea? You are only submitting the FIRST time, and you do not have to check for the submit every time. You just need to check if there is a search string and then put it back into the URL if there is one.

 

Hope this puts you on the right track.

 

I am also not sure about the lengthy SQL statement. If someone put in more than a few words of search terms, things could get really slow (you haven't set a limit either). No time to go into it, but you could look into fulltext indexing (http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html), that can REALLY speed things up if you can implement it.

 

I know, I know, you just want to get it working, it's a hassle doing all the optimising and securing, but it's gotta be done...

 

Link to comment
Share on other sites

Thank you for your reply markowe.

 

I did plug those valves in as you suggest, but the $button or $search still throws me into a loop because neither of those variables are not being submitted when the paginations are clicked. Or maybe I am just not understanding you logic...

 

I know there is a lot of issues with my code, and I am going to go back and optimize and secure it.  Thanks for the tips, I will look into fixing those...

 

Link to comment
Share on other sites

Thank you for your reply markowe.

 

I did plug those valves in as you suggest, but the $button or $search still throws me into a loop because neither of those variables are not being submitted when the paginations are clicked. Or maybe I am just not understanding you logic...

 

I know there is a lot of issues with my code, and I am going to go back and optimize and secure it.  Thanks for the tips, I will look into fixing those...

 

 

Hey, did you do the other part of my answer, add "&search=blabla" to the 'paginated' url? Then it would be submitted as a GET.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.