Print contents in new window

Posted on 2006-05-10
Last Modified: 2008-01-09
I'm trying to redesign a web-based DB search that I've got running on my server.  As of now, the user enters some basic search criteria into an html form, these criteria are posted to a PHP script, and the script queries the DB and gets matching results.  Each resulting record from the DB is dumped into an HTML table, and the results page is a series of tables, each populated with a record from the DB.

What I want to do is change the initial results page to be a list of links, each one corresponding to a match from the DB.  If the user chooses the last name "Smith" to search, the results page would look something like:

Smith, Adam 10/12/1977
Smith, John  2/8/1980
Smith, Jonas 5/17/1966

I can get as far as outputting a list of links corresponding to the matches from the DB, but I don't know what to actually put *in* the links that will allow me to open a new window, dump code into it, and give the user an option to print.  Is this something I can do with pure PHP, or will I need to throw some javascript in there too?

What I was thinking was to create a class which is instantiated by each new result record from the DB.  The constructor would return the html of the link, which could just be echoed to the intial search results page as unordered list elements from within the while loop that gets the DB results.  When the link is clicked, the ideal action would be for another function in the class to be called, which would open a new window, dump the html-table-encoded results into it, and provide a "print me" link.  

Does anyone have any suggestions on how I might pull this off?
Question by:Zeek0
    LVL 9

    Accepted Solution

    <a href='page.php?key=123' TARGET='_new'>Smith, John</a>

    using target= will open in a new window

    page.php will then using the key information to query the database for items to be displayed in the pop up.

    LVL 6

    Assisted Solution

    while ($row = mysql_fetch_array($result)) {
        echo "{$row['surname']}, {$row['name']} {$row['birthdate']}\n";
        echo "[ <a href=\"#\" onClick=\"'extended_info.php?id={$row['id']}', 'PersonInfoWindow', 'scrollbars=1, width=700, height=450'); PersonInfoWindow.focus();\">info</a> ]";

    or link can be:
    <a href=\"'extended_info.php?id={$row['id']}', 'PersonInfoWindow', 'scrollbars=1, width=700, height=450'); PersonInfoWindow.focus();\">info</a>

    Author Comment

    So what I'm seeing is that I'll need two scripts, correct?  

    The first will do an initial query for, say, the first name, last name, and birthdate.  The results will be displayed on a table of links, each to a second script with a GET variable.  The second script will query the DB again for the entire record(s) and build the print table out of them.

    Is it better, performance-wise and efficiency-wise to use two queries, each one returning as little data as is necessary?  Or is that simply the way it has to be done?  In my case, performance is not a major issue because the database records are short and the maximum number of matches to a particular query would only be a couple dozen.  

    It makes more sense to query only for the needed data, but to this point I was getting everything at once because I though the interaction between the script and the DB was the performance bottleneck.  Is that incorrect?  That is, does it hurt performance more to a) have the script receiving and parsing unnecessary data from the DB, in the interest of using a single query or b) query the database repeatedly with more restrictive statements that return less data?

    LVL 9

    Assisted Solution

    If the php is running from the same box as the sql database, it's not going to matter that much.  The performance is very negligable.  If you grab more data than you need, you will take up more memory resources.  If you use more queries, you are relying on the speed of your TCP stack and the performance of the sql server.  Either way, it's extremely fast.

    I personally would make 2 smaller queries that were more relevant.

    With my example, you would have a query on each of 2 pages.  The first page builds the links.  And the popup window retrieves the data base on the clicked link.

    Author Comment

    Thanks for the info.  I got everything working and as far as I can tell, the performance is more than adequate.  Thanks for the help.

    Featured Post

    Easy Project Management (No User Manual Required)

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now