Hi petrakid,
Ok here goes the example. I think I have understood your requirement and according to that I have made some changes to your requested table structure. I will try to explain you as much as I can and it’s up to you to do more research on it.
class table structure
ID – [PRIMARY KEY]:auto generated number :: its always a best practice to have a primary key and sometimes it’s a must (if it’s a joining table, in a situation like many to many and one to many relationship then you cannot have a primary key instead you have to defined the primary key in those main two tables.). more information research on table relationships.
className - Varchar :: This will hold all the class names. Ex: druid, hunter
status - Int :: This will hold the status value of the each class. Ex: 1 – open / 0 – close
This is the table structure we are going to have in our example. Its has an ID which is a auto generated number. It’s going to be a unique number so when ever we are updating a field we are going to use that ID field.
Here comes the PHP part.
<body>
<?php
mysql_connect('localhost','[user name]','[password]');
mysql_select_db('phpbuilder');
if(isset($_POST['Submit']))
{
while(list($key,$val) = each($HTTP_POST_VARS))
{
if(trim(substr($key,0,10))=='cmbStatus_')
{
$classNameErr = explode('_',$key);
$classNameId = trim($classNameErr[1]);
$classNameStatus = $val;
$sql = 'UPDATE mytable SET status='.$classNameStatus.' WHERE id='.$classNameId;
mysql_query($sql);
}
}
print 'update done';
}
?>
<table width="100%" border="0">
<tr>
<td><form id="form1" name="form1" method="post" action="10851604.php">
<table width="100%" border="0">
<tr>
<td>Class Name</td>
<td>Status</td>
</tr>
<?php
$sql = 'SELECT * FROM mytable';
$row = mysql_query($sql);
while($result = mysql_fetch_array($row))
{
?>
<tr>
<td><?=$result['className'];?></td>
<td><select name="cmbStatus_<?=$result['id'];?>" id="cmbStatus">
<option value="1" <?=$result['status']==1 ? 'selected' : '';?>>Open</option>
<option value="0" <?=$result['status']==0 ? 'selected' : '';?>>Close</option>
</select>
</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<?php
}
?>
<tr>
<td colspan="2"><input type="submit" name="Submit" value="Submit" /></td>
</tr>
</table>
</form></td>
</tr>
</table>
</body>
Now the explanation:
First we look at the first part of the code;
<?php
$sql = 'SELECT * FROM mytable';
STEP 1:$row = mysql_query($sql);
STEP 2:while($result = mysql_fetch_array($row))
{
?>
<tr>
<td><?=$result['className'];?></td>
STEP 3: <td><select name="cmbStatus_<?=$result['id'];?>" id="cmbStatus">
STEP 4: <option value="1" <?=$result['status']==1 ? 'selected' : '';?>>Open</option>
<option value="0" <?=$result['status']==0 ? 'selected' : '';?>>Close</option>
</select>
</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<?php
}
?>
STEP 1 & STEP 2:
In these steps we are just fetching all the records from the mytable and loop through the records.
STEP 3:
Here what we do is we are setting the combo boxes name. the name will have two parts.
1 – A default value as ‘cmbStatus_’ to identify the combo boxes from other objects we used in our form.
2 – Record Id to identify individual records when we doing the update query.
STEP 4:
In this step we are checking the previous records status and adjust the combo value according to it.
Now we are done with setting up the form. Lets look at the second part.
STEP 1: if(isset($_POST['Submit']))
{
STEP 2: while(list($key,$val) = each($HTTP_POST_VARS))
{
STEP 3: if(trim(substr($key,0,10))=='cmbStatus_')
{
STEP 4: $classNameErr = explode('_',$key);
$classNameId = trim($classNameErr[1]);
$classNameStatus = $val;
STEP 5: $sql = 'UPDATE mytable SET status='.$classNameStatus.' WHERE id='.$classNameId;
STEP 6: mysql_query($sql);
}
}
print 'update done';
}
STEP 1:
Checking if the ‘submit’ button is clicked.
STEP 2:
Loop through the HTTP_POST_VARS array to get the all the combo values.
Step 3:
Get the first 10 characters of each html element and match with the combo default value.
Step 4:
If match found then get the ID part from the combo name.
Step 5:
Prepare the update statement.
Step 6:
Update the table.
Hope this explanation is good and if you need further helop please let me know.
Thanks,
Regards,
Niroshan