Forum Moderators: coopster

Message Too Old, No Replies

Php: Display checkbox selection result from user selection

         

achilles71

8:51 am on Feb 26, 2007 (gmt 0)

10+ Year Member



Hi,

I'm new to PHP and Mysql and webdevelopment. I'm trying to create a mini project for my department to create a simple loan system. For the loan process, I managed to query out my items to loan and select.
I'm stuck on the checkbox processing.

Does anyone know how to display a result set based on a table of checkbox selection?

Basically, I have a table consisting of 10 columns of data generated from mysql query and I have a column of checkbox for each row.

I am able to echo out the selection based on POST(["item_id"]) passing into a $result variable. This would display only the item_id. For eg: I have a list of 10 rows, I select 1, 3, 5.

I am able to display 1, 3, 5.

How can I display selected columns for 1, 3, 5 row? Such that I have a table of fiels like (ItemID, Description, Qty, Date) rather than all the 10 columns from 1st query.

Feel free to comment and suggest improvement.

Kindly be more detail in explaining to me how it works. I'm really new and need sometime to digest the abundant info from the web. :-)

Below is my ProcessCheckbox script and Item query page.

processcb.php
===============
<?
if ($_POST['cb'] == "0")
$cbcount = isset($_POST['cb'])? count($_POST['cb']) : 0;
{
//$count=0;
$count = count($_POST[cb]);

if ($count >1) {
echo "You have selected $count items for loan";
}
else {echo "You have selected $count item for loan";
}
}
?>
<?
if(is_array($_POST[cb]))
{
$loan="<br> You have selected item ".implode(", ",$_POST[cb]);
}
echo $orders;
?>
<br>
<table border="0" cellpadding="5" cellspacing="1" class=rowtablehead>
<tr>
<th>S/No.</th>
<th>Category</th>
<th>Descriptions</th>
<th>Serial No.</th>
<th>Qty</th>
</tr>
</table>

software.php
<?php
include_once"../../include/interfaces.php";
include_once"../../include/includefile.php";
buildheader();
createmenu(menuMain());

if($action == "add"){
if(isset($id) AND is_numeric($id)){

$dbconn->Execute("INSERT INTO cart (sid) VALUES ('".$id."')");
}
}

//$dbResult is the array result from the database, use a for loop to print out all the array entries
$dbResult = $dbconn->Execute("SELECT item_id, item_cat_id, item_description, item_serial_num, item_qty, purchase_order_date, item_cost, create_date FROM items");
if($dbconn->ErrorNo()<>0){
$error = mysql_error();
}
//recordcount tells you how many succesfull records the database found, if more than 0 proceed
if($dbResult->RecordCount()<>0){
$class = 0;
//EOF - End Of File, MoveNext() goes from current record to next record in the array
for(;!$dbResult->EOF;$dbResult->MoveNext()){
$class = 1 - $class;
$listingResult .="<tr class=row$class>
<td>".$dbResult->fields["item_id"]."</td>
<td>".$dbResult->fields["item_cat_id"]."</td>
<td>".$dbResult->fields["item_description"]."</td>
<td>".$dbResult->fields["item_serial_num"]."</td>
<td>".$dbResult->fields["item_qty"]."</td>
<td>".$dbResult->fields["purchase_order_date"]."</td>
<td>".$dbResult->fields["item_cost"]."</td>
<td>".$dbResult->fields["create_date"]."</td>
<td><a href=\"$sitedir/html/listing/software.php?action=add&id=".$dbResult->fields["item_id"]."\">add</a></td>
<td><form action=\"processcb.php\" method=\"post\"><input type =\"Checkbox\" name=\"cb[]\" value=".$dbResult->fields["item_id"]." title=\"Loan\"></td>
</tr>
";
}
}


// getting the total number of items in your cart
$noofcart = $dbconn->Execute("SELECT count(*) FROM cart");
$counter = $noofcart->fields[0];
?>

</form>
<div id="mainbody">
<h4>Available Software</h4>
<hr noshade >
<p><? echo $counter;?></p>
<table border="0" cellpadding="5" cellspacing="1" class=rowtablehead cols=10>
<tr>
<th>S/No.</th>
<th>Category</th>
<th>Descriptions</th>
<th>Serial No.</th>
<th>Qty</th>
<th>PO Date</th>
<th>Costs.</th>
<th>Create Date</th>
<th>Selection</th>
<th>Loan</th>
</tr>

<?php echo $listingResult;?>
<tr>
<th colspan="9"><div align="right">Total Item Selected </div></th>
<th bgcolor="aliceblue">&nbsp;</th>
</tr>
</table>
<hr noshade >

<tr>
<div align="right">
<input type="submit" name="submit" value="Loan selected item" align="right">
<input type="reset" name="reset" value="Unselect all" align="right">
</div>
</tr>

<?php
backarrow();
?>
</div>
<?php
endheader();

?>

mcibor

3:19 pm on Feb 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's an error:

$class = 0;
//EOF - End Of File, MoveNext() goes from current record to next record in the array
for(;!$dbResult->EOF;$dbResult->MoveNext()){
$class = 1 - $class;
$listingResult .="<tr class=row$class>
<td>".$dbResult->fields["item_id"]."</td>
<td>".$dbResult->fields["item_cat_id"]."</td>
<td>".$dbResult->fields["item_description"]."</td>
<td>".$dbResult->fields["item_serial_num"]."</td>
<td>".$dbResult->fields["item_qty"]."</td>
<td>".$dbResult->fields["purchase_order_date"]."</td>
<td>".$dbResult->fields["item_cost"]."</td>
<td>".$dbResult->fields["create_date"]."</td>
<td><a href=\"$sitedir/html/listing/software.php?action=add&id=".$dbResult->fields["item_id"]."\">add</a></td>
<td><form action=\"processcb.php\" method=\"post\"><input type =\"Checkbox\" name=\"cb[]\" value=".$dbResult->fields["item_id"]." title=\"Loan\"></td>
</tr>
";
}
}

You open many forms, therefore on submit only one is submitted.
It should be:


$class = 0;
$listingResult = '<form action="processcb.php" method="post">';
//EOF - End Of File, MoveNext() goes from current record to next record in the array
for(;!$dbResult->EOF;$dbResult->MoveNext()){
$class = 1 - $class;
$listingResult .="<tr class=row$class>
<td>".$dbResult->fields["item_id"]."</td>
<td>".$dbResult->fields["item_cat_id"]."</td>
<td>".$dbResult->fields["item_description"]."</td>
<td>".$dbResult->fields["item_serial_num"]."</td>
<td>".$dbResult->fields["item_qty"]."</td>
<td>".$dbResult->fields["purchase_order_date"]."</td>
<td>".$dbResult->fields["item_cost"]."</td>
<td>".$dbResult->fields["create_date"]."</td>
<td><a href=\"$sitedir/html/listing/software.php?action=add&id=".$dbResult->fields["item_id"]."\">add</a></td>
<td><input type =\"Checkbox\" name=\"cb[]\" value=".$dbResult->fields["item_id"]." title=\"Loan\"></td>
</tr>
";
}
$listingResult .= '</form>';
}

And to display only selected rows:
$cb = validate($_POST['cb']);//function validate should look eg:

function validate($arr) {
$result = array();
if(!is_array($arr)) $result;
foreach($arr as $key => $value) {
$result[$key] = "'".(int)$value."'";
}
return $result;
}

$cb = implode(", ", $cb);
$sql = "SELECT * FROM table WHERE id IN ($cb)";

Regards
Michal