Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

Displaying result for a particular record through PHP- MySQL Search

After using the search criteria for my database search, I do get the desired records, but I dont want to display all the fields(85 fields) for each of the records; instead I've displayed only 5 of them, and then wish to give a hyperlink saying "Click for further Details", so that the users can then go ahead and view all the fields for a particular record.

For eg, if the user enters "abc" in Search box, and the database contains 5 records with "abc", then it'll show all 5 records, but only 5 specific fields. But to view all the fields, for say the 1st "abc" record, the user will click on "further details", which will direct him to another page with all the 85 fields for that particular 1st abc record.

Kindly guide me through! I dont know what conditions to give, because i tried doing it with header(Location), but displays all the records, and doesn't even take that particular processed sql query!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's an example of what you could do. It's a self-referencing search page that first uses a generic query to get all records. Then, as it cycles through each record (only grabbing specific fields, it makes a form that uses a hidden variable with the Primary Key/Unique ID to send to itself (search.php) as a POST variable. Then, when that POST variable is detected, the query only grabs that 1 specific record and using an IF/THEN statement in the WHILE loop, shows all 80+ fields.

There are better ways to do querying and handling form data, but this should get you started. Please select this as an answer if it works.

search.php

<?php

    $query = "SELECT * FROM table"; 

    if (!empty($_POST['id'])) {
        $query = "SELECT * FROM table WHERE id_field = '" . $_POST['id'] . "'";
    }

    $query_info = mysql_query($query) or die(mysql_error()); 
    $query_info_count = mysql_num_rows($query_info);

    while($query_info_details = mysql_fetch_array($query_info)){

    if ($query_info_count > 1) {

        $field_a = $query_info_details['field_a'];
        $field_d = $query_info_details['field_d']; // Say this is the Primary Key
        $field_f = $query_info_details['field_f'];
        $field_g = $query_info_details['field_g'];
        $field_s = $query_info_details['field_s'];

        echo "<form method='post' action='search.php'>";

        echo "Name: " . $field_a . " - " . $field_f . " - ID: " . $field_d;
        echo " - Location: " . $field_g . "(" . $field_s . ")";

        echo "<input type='hidden' name='id' value='" . $field_d . "'>
        <input type='submit' value='View This Record'><br><br>";

    }
    else {

        // Display all fields for record using $query_info_details['field_X'] from above

    }

    }

?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...