?
Solved

URGENT, php/mysql generated table, use 0/more selected checkboxes

Posted on 2005-03-28
13
Medium Priority
?
236 Views
Last Modified: 2008-02-07
If I generate an html table using values out of mysql, how do I perform mysql actions/php queries using the values (an id field per row) in the table?

Do I have to dynamically assign a name to each checkbox (e.g. the value of each id) and use that?  I want to use the value of the id field if the checkbox is checked and there are going to be 1-4 rows in my table.

The table is generated on page1.php and actions are to be performed on page2.php.
0
Comment
Question by:j_byron
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
13 Comments
 
LVL 9

Expert Comment

by:_GeG_
ID: 13646446
you can use arrays
in page1:

while ($row=mysql_fetch($result)){
   echo '<input type="checkbox" name="cbox['.$row['id'].']">';
}

the html look like this then:
<input type="checkbox" name="cbox[2]">

and in page2:
echo $_REQUEST['cbox'][2]:
0
 

Author Comment

by:j_byron
ID: 13646703
Because I never know how many rows will be generated, how do I refer to the nth row/nth checkbox and the associated field values of row n, on page 2?
0
 

Author Comment

by:j_byron
ID: 13646737
my single and double quotes are the opposite way around to yours, is this right?

".........<td><input type='checkbox' name='cbox[" . $row["fieldname"] . "]'></td>........."
0
WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

 
LVL 9

Expert Comment

by:_GeG_
ID: 13646944
yes to the later,
i use the id value for each row:
so in page2, if you receive $_REQUEST['cbox'][2], you know that this is the value of cbox for the row with id=2
0
 

Author Comment

by:j_byron
ID: 13646956
But I cannot write infinity if $_REQUEST... clauses.

The ID is auto increment, over time with deletions and adding new entries, who is to know what the id number will be?
0
 
LVL 9

Accepted Solution

by:
_GeG_ earned 2000 total points
ID: 13647111
let's say you have this sql query
SELECT id, filed1, field2 FROM table LIMIT 100,3
this gives you the row 101-103
this will look like
id   filed1    field2
101   4     max
102   22    eric
103   -4   sarah

while ($row=mysql_fetch($result)){
   echo '<input type="text" name="field1['.$row['id'].']" value="'.$row['field1'].'"><input type="text" name="field1['.$row['id'].']" value="'.$row['field2'].'">';
}

this would result in:
<input type="text" name="field1[101]" value="4"><input type="text" name="field2[101]" value="max">
<input type="text" name="field1[102]" value="22"><input type="text" name="field2[101]" value="eric">
<input type="text" name="field1[103]" value="-4"><input type="text" name="field2[101]" value="sarah">

and in page2
foreach($_REQUEST['field1'] as $id=>$field1){
    $query="UPDATE table SET field1='$field1', field2='{$_REQUEST['filed2'][$id]}' WHERE id=$id";
}
0
 

Author Comment

by:j_byron
ID: 13647213
what does as $id=>$field1 do?
0
 

Author Comment

by:j_byron
ID: 13647226
and I don't get

field2='{$_REQUEST['filed2'][$id]}' WHERE id=$id
0
 
LVL 9

Expert Comment

by:_GeG_
ID: 13650029
http://www.php.net/manual/en/control-structures.foreach.php

it loops over an associative array:
foreach($_REQUEST['field1'] as $id=>$field1){
$_REQUEST['field1'][101]=4;
$_REQUEST['field1'][102]=22;
$_REQUEST['field1'][103]=-4; according to our example

so in the first loop:
$id=101 and $field1=4

second loop:
$id=102 and $field1=22
...

and
$_REQUEST['filed2'][$id] has a typo, it should be
$_REQUEST['field2'][$id] which would be
in the first loop:
$_REQUEST['field2'][101]
second loop:
$_REQUEST['field2'][102]
...

and the result:
in the first loop:
UPDATE table SET field1='4', field2='max' WHERE id=101
second loop:
UPDATE table SET field1='22', field2='eric' WHERE id=102
...
0
 

Author Comment

by:j_byron
ID: 13651099
foreach($_REQUEST['cbox'] as $id=>$cbox)
{
echo $sessionid . "<br />";
}

This reports back the id values which have cbox set.  Is this correctly done?
0
 
LVL 9

Expert Comment

by:_GeG_
ID: 13651152
no
 foreach($_REQUEST['cbox'] as $id=>$cbox)
{
echo "$id = $cbox<br />";
}

echos the values
0
 

Author Comment

by:j_byron
ID: 13651875
But echo $id . "<br />"; works if I want the actual id value (if cbox has a check)?

0
 

Author Comment

by:j_byron
ID: 13757979
Thanks for the help.  Sorry for the delay.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

762 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