print certain records in list

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scubadiveflorida
Forum Newbie
Posts: 10
Joined: Wed Oct 29, 2003 9:53 am

print certain records in list

Post by scubadiveflorida »

I have a page where I loop through all the records and display a list of all the records. I would like to ad a check box next to each listing and make it so if the user can check the ones that he wants to print out then click on a print button and it prints the listings that he chose. Can anyone point me in a direction to do this? Thanks.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Take an example. One page (choose_records.php) to show the records to the user and let him choose what will be printed. Another page (print_records.php) to 'draw' a table with the previously selected records to be printed out.

choose_records.php:

Code: Select all

<form action="print_records.php" method="post">
<input type="checkbox" name="recordsї]" value="rec_key1" /> Record #1<br />
<input type="checkbox" name="recordsї]" value="rec_key2" /> Record #2<br />
<input type="checkbox" name="recordsї]" value="rec_key3" /> Record #3<br />
<input type="submit"/>
</form>
Let's imagine that "Record #1" and "Record #3" where selected and the information was submited to print_records.php:

Code: Select all

<?php
// Declaration of a function to be applyed to every member of an array
// This function surrounds every member of the array by $slash
function surSlash(&$value, $key, $slash) {
   $value = $slash . "$value" . $slash;
}

$records = $_POST['records']; // Copy the 'records' sub-array to a new var
array_walk($records, 'surSlash', "'"); // Surround each member of $records by a single slash (')
$values = implode(",", $records); // Look at the reference links below
$sql = "select * from table_name where table_key in ($values) order by table_key";
echo $sql;
?>
which will output:

Code: Select all

select * from table_name where table_key in ('rec_key1','rec_key2') order by table_key
With this $sql you can query the DB and get the desired rows to be listed in a <table> and then let the user print it via the browser's internal print engine.

Check these links for reference:Hope it helps,
Scorphus.
Post Reply