Forum Moderators: coopster

Message Too Old, No Replies

HELP! passing checkbox values to array

HELP! checkbox values to an array to display results from database

         

TheCoupe

1:45 pm on Apr 25, 2005 (gmt 0)

10+ Year Member



Hi, this is my first post as I normally like to resolve issues myself. Now I'm stuck :(

I have a form with a repeating table display data from MySQL, ie, invoice numbers and invoice pricing.

I need to be able to select which invoices someone wants to pay via checkboxes. From here, I need to be able to display the corresponding records on another page where I can SUM() the invoice totals to give a price to be paid.

I've never used array's before but believe this is how it should be done. My brain is currently fried and I'm struggling...

Any help would be greatly appreciated.

Thanks

Justin

buriedUnderGround

2:12 pm on Apr 25, 2005 (gmt 0)

10+ Year Member



this should be quite simple.

name your checkboxes in checkbox group as follows : checkBoxArrayName[]
This means that they will all have the same name.

once the form has been submitted all values of the checked boxes will be available in the array called $checkBoxArrayName

Hope that helps.

TheCoupe

2:19 pm on Apr 25, 2005 (gmt 0)

10+ Year Member



Thanks for that :D

how would I then call the results from the database ie,
WHERE user = 'username' AND id = 'checkboxarrayname'?

Cheers

TheCoupe

2:22 pm on Apr 25, 2005 (gmt 0)

10+ Year Member



also, would I set a session_register('CheckBoxArrayName') or session_register('CheckBoxArrayName[]') etc

or would I be better with $chosen = $checkboxarrayname[] etc?

Like I say, I've never used array's before and am confused LOL

Thanks

buriedUnderGround

2:36 pm on Apr 25, 2005 (gmt 0)

10+ Year Member



:) by checking and submitting the following checkboxes:

<input type="checkbox" name="mySelection[]" value="dog">
<input type="checkbox" name="mySelection[]" value="cat">
<input type="checkbox" name="mySelection[]" value="fish">

you will get an array that looks like this:

$mySelection[0] = "dog";
$mySelection[1] = "cat";
$mySelection[2] = "fish";

So you could use your array data in your query like this:
WHERE user = 'username' AND id = '$mySelection[0]'
if you wanted to search for id='dog'

In this case where you most likely want to search by the entire selection you could run through the array n times and generate a dynamic piece of sql to accommodate for different selections:

<?
for($i=0;$i<count($mySelection);$i++){
$someSQL .= "id='$mySelection[$i]' OR ";
}
?>

and then alter your query to something like this:
WHERE user = 'username' AND ($moreSQL)

To register the array in the session all you need to do is session_register('CheckBoxArrayName').
As soon as you use the []'s you're making a reference to a value in the array.

Hope that makes some sense.

coho75

2:39 pm on Apr 25, 2005 (gmt 0)

10+ Year Member



Maybe something like the following will work for you.

$sql = "SELECT * FROM database WHERE id IN (";

foreach ($_POST['number'] as $username) $sql .= "'" . $username . "',";

$sql = substr($sql,0,-1) . ")";

$result = mysql_query($sql);

while ($row = mysql_fetch_array($result))

coho75

TheCoupe

8:48 am on Apr 26, 2005 (gmt 0)

10+ Year Member



Hi guys, thanks for your help so far... I am getting the array through to the following page and separated to display on separate rows, just to to check that the array was working.

Though I am still having problems to extract the info from the database. I am using Dreamweaver in this and have included the code for the database select. Please can someone have a look and tell me where I'm going wrong.

I'm not totally sure of where to place the sql code you have mentioned.

Thanks

Justin

<?php
$checked_Recordset1 = "0";
if (isset($_SESSION['checkboxed'])) {
$checked_Recordset1 = (get_magic_quotes_gpc())? $_SESSION['checkboxed'] : addslashes($_SESSION['checkboxed']);
}
$colname_Recordset1 = "1";
if (isset($_SESSION['MM_Username'])) {
$colname_Recordset1 = (get_magic_quotes_gpc())? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']);
}
mysql_select_db($database_xerox, $xerox);
$query_Recordset1 = sprintf("SELECT * FROM job WHERE company = '%s' AND id = '%s'", $colname_Recordset1,$checked_Recordset1);
$Recordset1 = mysql_query($query_Recordset1, $xerox) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>