1

I am building a mini test php thing. And I need to be able to search the results. This works great. But I cannot seem to make a paging system around this. I sort of know how to make a paging system, however, I cannot seem to do this because the mysql query is not only searching the table records which idedu_details is equal to $edu_details:

$search_result = mysql_query("SELECT * FROM test WHERE idedu_details='".$edu_details."' AND first_name LIKE '%".$search."%' OR surname LIKE '%".$search."%'");

here is my complete code:

$search_result = mysql_query("SELECT * FROM test WHERE idedu_details='".$edu_details."' AND first_name LIKE '%".$search."%' OR surname LIKE '%".$search."%'");

    while($row_search = mysql_fetch_array($search_result)){


                $idedu_detail_check = mysql_query("SELECT * FROM test WHERE idtest='".$row_search['idtest']."'") or die(mysql_error());
                $row_idedu_check = mysql_fetch_array($idedu_detail_check);

                if($row_idedu_check['idedu_details'] == $edu_details){
                $testid = $row_search['idtest'];
                $firstName = $row_search['first_name'];
                $surname = $row_search['surname'];
                $dob = $row_search['dob'];
                $date = $row_search['date'];
                $status = $row_search['status'];
                $link = "report.php?id=$testid";
                $button = '<button onClick="parent.location='. "'".$link."'".'">Analyse</button>';

                echo "<tr>
                    <td width='100'>$firstName</td>
                    <td width='100'>$surname</td>
                    <td width='100'>$dob</td>
                    <td width='100'>$date</td>
                    <td width='100'>$status</td>
                    <td width='100'>$button</td>

                    </tr>";
                }//end if idedu_detail check

            }//end while

        }//end else search != null

How can I make a paging system around this?

EDIT: I know how to make a general paging system. However, checking the ided_details = $edu_details and building a paging system around this is causing an issue.

1
  • Why are you doing two SELECTs? I bet you can combine those two queries. Commented Aug 8, 2011 at 14:48

3 Answers 3

1

I think you forgot to add parenthesis in WHERE clause:

$search_result = mysql_query("
    SELECT * 
    FROM test 
    WHERE 
        idedu_details='".$edu_details."' AND 
        ( first_name LIKE '%".$search."%' OR surname LIKE '%".$search."%' )
");

Keep in mind that AND always takes precedence over OR.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! This worked straight away! I have now got the paging system working. Thank you.
1

First you would need to set a limit to the results that are being retrieved. Secondly, you have done nothing to paginate the page, try something and let us know what you can't do. :)

2 Comments

I have build a paging system for a different page. But this one includes searching, and making sure idedu_details = $edu_details. I need to know the number of results outside the while.
There is another tutorial, on youtube how to paginate with PHP, here is the link youtube.com/watch?v=wd4fE5fk-fk
1

I think you're saying that your SQL query is pulling where idedu_details!=$edu_details. Try putting in parentheses around the 2 ORs:

$search_result = mysql_query("SELECT * FROM test WHERE idedu_details='".$edu_details."' AND (first_name LIKE '%".$search."%' OR surname LIKE '%".$search."%')");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.