Using check box in table to select records

This is a discussion on Using check box in table to select records within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hello all I have the following PHP script to place records from my MySql db into a table. However I ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-04-2005
Ian Davies
 
Posts: n/a
Default Using check box in table to select records

Hello all
I have the following PHP script to place records from my MySql db into a
table. However I now wish users to beable to select the multiple records
from the table and on clicking a button the selected records are stored to
another table in my db. I have started by including a check box in the first
column but dont know the code necessary to check all the row in the table
and copy the records of those boxes thats ticked
Can anyone help?
Thanks
Ian
*****************************************
<table width="1022" border="2" align="left" cellpadding="0" cellspacing="0">
<tr>
<td width="48"><div align="left"><span
class="style2">Select</span></div></td>
<td width="91"><span class="style2">Question No </span></td>
<td width="527"><span class="style2">Question</span></td>
<td width="42">path</td>
<td width="42"><span class="style2">Type</span></td>
<td width="256"><span class="style2">Image</span></td>
</tr>
<tr>
<?php
while($row =& mysql_fetch_assoc($result)) {
extract($row);

if ($i%2) {
$class = 'row1';
} else {
$class = 'row2';
}

$i += 1;

if ($ImagePath) {
//$Image = WEB_ROOT . 'images/PupilTester/' . $ImagePath;
$Image = 'images/PupilTester/' . $ImagePath;
} else {
$Image ='images/PupilTester/nopicture.bmp';
}

?>


?>

<tr class="<?php echo $class; ?>">
<td>
<div align="center">
<input type="checkbox" name="checkbox" value="checkbox">
</div></td>
<td><?php echo $QuestionNo; ?>&nbsp;</td>
<td><?php echo $Question; ?></td>
<td><?php echo $ImagePath; ?></td>
<td><?php echo $TypeID; ?> <div align="center"></div></td>
<td><img src=<?php echo $Image; ?>></td>
</tr>
<?php
*************************************


Reply With Quote
  #2 (permalink)  
Old 10-04-2005
JDS
 
Posts: n/a
Default Re: Using check box in table to select records

On Tue, 04 Oct 2005 16:49:37 +0000, Ian Davies wrote:

> <input type="checkbox" name="checkbox" value="checkbox">


Use

name="checkbox[]"

and

value="<? echo $TypeID?>"

Read the following:
http://us2.php.net/manual/en/faq.htm...aq.html.arrays

ask more questions when you get stuck

later...

--
JDS | jeffrey@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Reply With Quote
  #3 (permalink)  
Old 10-06-2005
Ian Davies
 
Posts: n/a
Default Re: Using check box in table to select records

Thanks for your reply

Looking at your code and reading the article it seems that some sort of
array is created. How would I then APPEND this list to a table, Im OK with
the SQL but which part of the php script would be used in the APPEND
statement.
or alternatively how could I use the array as a WHERE clause in a SELECT
statement.

Ian

"JDS" <jeffrey@example.invalid> wrote in message
news:pan.2005.10.04.17.17.09.812634@example.invali d...
> On Tue, 04 Oct 2005 16:49:37 +0000, Ian Davies wrote:
>
> > <input type="checkbox" name="checkbox" value="checkbox">

>
> Use
>
> name="checkbox[]"
>
> and
>
> value="<? echo $TypeID?>"
>
> Read the following:
> http://us2.php.net/manual/en/faq.htm...aq.html.arrays
>
> ask more questions when you get stuck
>
> later...
>
> --
> JDS | jeffrey@example.invalid
> | http://www.newtnotes.com
> DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/
>



Reply With Quote
  #4 (permalink)  
Old 10-07-2005
Dikkie Dik
 
Posts: n/a
Default Re: Using check box in table to select records

In the example Jeffrey gave, the name ends in [].
This would automatically cause an array to be created. As checkboxes are
only passed back to the server when they are set, you should end up with
an array of checked items on the server. You can use the implode
function to convert it to a comma separated list if you want to.

Best regards

Ian Davies wrote:
> Thanks for your reply
>
> Looking at your code and reading the article it seems that some sort of
> array is created. How would I then APPEND this list to a table, Im OK with
> the SQL but which part of the php script would be used in the APPEND
> statement.
> or alternatively how could I use the array as a WHERE clause in a SELECT
> statement.
>
> Ian
>
> "JDS" <jeffrey@example.invalid> wrote in message
> news:pan.2005.10.04.17.17.09.812634@example.invali d...
>
>>On Tue, 04 Oct 2005 16:49:37 +0000, Ian Davies wrote:
>>
>>
>>> <input type="checkbox" name="checkbox" value="checkbox">

>>
>>Use
>>
>>name="checkbox[]"
>>
>>and
>>
>>value="<? echo $TypeID?>"
>>
>>Read the following:
>>http://us2.php.net/manual/en/faq.htm...aq.html.arrays
>>
>>ask more questions when you get stuck
>>
>>later...
>>
>>--
>> JDS | jeffrey@example.invalid
>> | http://www.newtnotes.com
>> DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/
>>

>
>
>

Reply With Quote
  #5 (permalink)  
Old 10-07-2005
JDS
 
Posts: n/a
Default Re: Using check box in table to select records

On Fri, 07 Oct 2005 08:33:51 +0200, Dikkie Dik wrote:

> an array of checked items on the server. You can use the implode
> function to convert it to a comma separated list if you want to.


Example[1]:

$sql = "SELECT * FROM boogers WHERE booger_id IN ( "
. join(",", $_REQUEST['booger_ids']) . " )";

(Note that that is all one line of PHP).

You can use implode() instead of join() -- they are functionally identical.


[1] Change "booger" to "bogie" for those from the UK. :)
--
JDS | jeffrey@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Reply With Quote
  #6 (permalink)  
Old 10-08-2005
Ian Davies
 
Posts: n/a
Default Re: Using check box in table to select records

Thanks for all the feedback

However I am still confused. Let me

In QUESTION.PHP I have records displayed from a MySQL database table in an
HTML table and a check box for each record as follows



************************************************** ***********

<input type="checkbox" name="checkbox[]" value="<? echo
$QuestionNo?>"
************************************************** ***************



I also have a form and a button as follows, which is suposed to run
FILTER.PHP



************************************************** ********

<form name="form1" method=post action="Filter.php">

<input type="submit" name="Submit" value="Submit">

************************************************** ***********



I am unsure of the script to use in the SQL of FILTER.PHP to filter records
based on the ticked checkboxs

FILTER.PHP has the following SQL (which is obviously wrong). Basically I am
not sure how to use the array from QUESTION.PHP to filter records in
FILTER.PHP



************************************************** ****

<?php

$filtered = @mysql_query("SELECT * FROM Questions WHERE QuestionNo IN ( " .
join(",", $_REQUEST['$QUESTION_NOS_FROM_THE_ARRAY']) . " )";



if (!$filtered) {
exit('<p>Error performing query: ' . mysql_error() . '</p>');
}
$filteredrow = mysql_fetch_assoc($filtered);
extract($filteredrow);



// Display the text of each Question in a paragraph





//while ($filteredrow = mysql_fetch_array($filtered)) {
// echo '<p>' . $filteredrow['QuestionNo'] . '</p>';
//}
?>

***********************************************



"JDS" <jeffrey@example.invalid> wrote in message
news:pan.2005.10.07.19.42.08.511056@example.invali d...
> On Fri, 07 Oct 2005 08:33:51 +0200, Dikkie Dik wrote:
>
> > an array of checked items on the server. You can use the implode
> > function to convert it to a comma separated list if you want to.

>
> Example[1]:
>
> $sql = "SELECT * FROM boogers WHERE booger_id IN ( "
> . join(",", $_REQUEST['booger_ids']) . " )";
>
> (Note that that is all one line of PHP).
>
> You can use implode() instead of join() -- they are functionally

identical.
>
>
> [1] Change "booger" to "bogie" for those from the UK. :)
> --
> JDS | jeffrey@example.invalid
> | http://www.newtnotes.com
> DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/
>



Reply With Quote
  #7 (permalink)  
Old 10-08-2005
Steve
 
Posts: n/a
Default Re: Using check box in table to select records


> In QUESTION.PHP I have records displayed from a MySQL database table in an
> HTML table and a check box for each record as follows


> <input type="checkbox" name="checkbox[]" value="<? echo
> $QuestionNo?>"


> I also have a form and a button as follows, which is suposed to run
> FILTER.PHP


> <form name="form1" method=post action="Filter.php">
>
> <input type="submit" name="Submit" value="Submit">


First point, the <input /> fields should be declared INSIDE the <form>
so that when the submit button is clicked the current values of the
selected items are sent to your processing script. Declared outside, as
you show above, means that they are purely cosmetic rather than
functional.

> FILTER.PHP has the following SQL (which is obviously wrong). Basically I am
> not sure how to use the array from QUESTION.PHP to filter records in
> FILTER.PHP


> $filtered = @mysql_query("SELECT * FROM Questions WHERE QuestionNo IN ( " .
> join(",", $_REQUEST['$QUESTION_NOS_FROM_THE_ARRAY']) . " )";


Nearly there, but what is '$QUESTION_NOS_FROM_THE_ARRAY'? Instead of
that, modify Jeff's example to match your field name, 'checkbox[]':

> $filtered = @mysql_query("SELECT * FROM Questions WHERE QuestionNo IN ( " .
> join(",", $_REQUEST['checkbox']) . " )";


---
Steve

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 06:05 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0