Hello all.. Very new to php and am working on an online strategy game as a precursor to my business websites. I am having a problem coming up with something I thought would be simple.

My intention was to have a list of units display on the screen using checkboxes to select which units to attack. In order for me to have a select all button I had to use a bit of javascript in there. My problem is this... I thought I could just use 2 arrays and match them up so I would be able to run through the mysql database and update each unit that was checked. The arrays however come back only with the checkboxes that were checked so my idea for simply matching box 1 to unit 1 won't work. I just want to update a field called 'checked' in a mysql database to yes or no so I can call a routine that will go through the records to use in the combat.

If anybody has a better way of doing this since the way I thought would work won't I would appreciate a point in the right direction. I was thrilled to have the select all button finally work.

if(!$_POST['yes'] && !$_POST['no']){
?>
<TABLE ALIGN="center">
<tr>
<td>
<form action="selectattackers.php" method="post">

     <input type="checkbox" value="on" name="allbox"   
       onclick="checkAll();"/> Select All<br />

<script language="javascript">
function checkAll(){
    for (var i=0;i<document.forms[0].elements.length;i++)
    {
        var e=document.forms[0].elements[i];
        if ((e.name != 'allbox') && (e.type=='checkbox'))
        {
            e.checked=document.forms[0].allbox.checked;
        }
    }
}
</script>
<br>
     <input name="yes" type="submit" value="Yes">
     <input name="no" type="submit" value="No">
<br>

<?
     $result3  = $db->query("SELECT * FROM UnitsZones WHERE OwnerID=\"" . $Owner . "\"");     
     while($unitstoattack= $db->fetch($result3)){
              if($unitstoattack[Checked]=="Y"){
                       echo "Unit Number: $unitstoattack[UnitNumber]  Zone:$unitstoattack[ZoneID]    Description:$unitstoattack[UnitDescription]";

     echo"<INPUT TYPE=checkbox NAME=checkbox[] VALUE=1>   
     <br>"; 
     $unitnumberarray = array($unitstoattack[UnitNumber]);              
                    }         
     }          
?>
     </form> 
     </td>
     </tr>
     </table>




<?
}

elseif($_POST['yes']){

for($i = 0; $i < 12; ++$i)
{
/*This is where I had planned on simply updating the field to Y in 
* the mysqul database the 12 above was to be the sizeof the array
*
*/
    echo "HI";
    echo "$unitnumberarray[$i] <BR>  ";   
    if($checkbox[$i]== true){echo "YAY";}
}



}

include("../includes/inc-footer.php");
?>

Hey.

When you create an array of <input> elements, you can specify the ID that is supposed to be used for that specific element.

For example:

<?php
echo "<form action='process.php' method='post'>";
for($i = 0; $i < 10; $i++) {
    echo "<input type='checkbox' name='box[$i]' value='$i'> Box #$i<br>";
}
echo "<input type='submit'>";
echo "</form>";
?>

Checking only 3, 5 and 9 in the generated HTML form and submitting it to PHP would send a $_POST['box'] value of:

Array(
  [3] = 3,
  [5] = 5,
  [9] = 9
);

Which allows you to mark specific boxes for specific IDs in your database.

See what I mean?

P.S.
A few comments on the PHP code you posted that I though might be helpful:

<?php
// MySQL uses single-quotes for strings, not double quotes, and numbers should not be quoted.
// And you don't have to close a double-quoted PHP string to insert a variable, PHP
// parses variables inside double quotes.

# $result3 = $db->query("SELECT * FROM UnitsZones WHERE OwnerID=\"" . $Owner . "\"");
$result3 = $db->query("SELECT * FROM UnitsZones WHERE OwnerID=$Owner");

while($unitstoattack = $db->fetch($result3)) 
{
    // You can only leave element names unquoted inside strings.
    // Outside of strings, element names are strings themselves and as such
    // should be quoted.
    
    # if($unitstoattack[Checked]=="Y") {
    if($unitstoattack['Checked']=="Y") 
    {
        // All variables need to be prefixed with $ signs, even inside strings, and
        // if you put array elements directly into strings, they should preferably be enclosed
        // in brackets { }. (It's not strictly needed, but avoids a lot of possible problems)
        
        #echo "Unit Number: $unitstoattack[UnitNumber] Zoneunitstoattack[ZoneID] Descriptionunitstoattack[UnitDescription]";
        echo "Unit Number: {$unitstoattack['UnitNumber']} {$Zoneunitstoattack['ZoneID']} {$Descriptionunitstoattack['UnitDescription']}";

        // If your HTML attributes contain anything more than a single word, they need
        // to be quoted. To avoid problems, it is best to just always quote them.
        
        # echo"<INPUT TYPE=checkbox NAME=checkbox[] VALUE=1><br>";
        echo"<INPUT TYPE=\"checkbox\" NAME=\"checkbox[]\" VALUE=\"1\"><br>";
        
        // Again, array element names need to be quoted.
        
        # $unitnumberarray = array($unitstoattack[UnitNumber]);
        $unitnumberarray = array($unitstoattack['UnitNumber']);
    }
}
?>

That is fantastic. Knew there had to be a way. Sorry for my ineptitude but I will learn.

I am very greatful for the help. That works great for me thank you :)

Appreciatively,

Honos1

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.