SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    907
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with checkboxes and array for inport and update

    Hi all,

    I am creating a contract database, and in the bit I am at now, the users are being created and they also need to be connected to the corporations that have already been created.

    The form I have is i am using the same form to add and then if needed edit the users, and have managed to successfully create the checkboxes to display to be able to add the users with the code below:

    (I know I need to update to mysqli, which I will do once i get this working, as need ot anyway as the servers are being changed too)

    Code:
    <label class="myCheckbox">Corporations</label>
    <?php
     $q="select * from Corporations";
     $result = mysql_query($q) or die(mysql_error());
     while($row = mysql_fetch_assoc($result)) {
        echo <<<EOL
     <div class="myCheckbox">
     <input type="checkbox" name="name[]" value="{$row['ID']}" /> <span>{$row['Name']}</span><br />
     </div>
    EOL;
    } ?>
    The checkboxes display fine on the page, but what I need to do now is allow the admin person to select the corporations they want to attach to the user they are adding in.

    How do I get these checkbox values into the field name 'Corporations' in the Useradmin table as an array. Suppose start with that first.

    Code:
     
    if(isset($_POST['btnsubmit']))
    {
    $sr=$_POST['srno1'];
    if ($flag==0)
    {
    $userFirstName=mysql_real_escape_string($_POST['firstName']);
    $userSurName=mysql_real_escape_string($_POST['surName']);
    $userUserName=mysql_real_escape_string($_POST['userName']);
    $userPassWord=mysql_real_escape_string($_POST['pWord']);
    $userActive=$_POST['Active'];  
    $userSuperAdmin=mysql_real_escape_string($_POST['superAdmin']);
    $userCorporationName=????????????;
    $q24=mysql_query("update UserAdmin set First Name='$userFirstName', Last Name='$userSurName', Username='$userUserNam', Password='$userPassWord', Active='$userActive', Super Admin='$userSuperAdmin', Corporations='??????????', Date='$dt2' where ID=$sr") or die (mysql_error());
    $flag=1;
    $update="1";
    }
    else
    {
    $userFirstName=mysql_real_escape_string($_POST['firstName']);
    $userSurName=mysql_real_escape_string($_POST['surName']);
    $userUserName=mysql_real_escape_string($_POST['userName']);
    $userPassWord=mysql_real_escape_string($_POST['pWord']);
    $userActive=$_POST['Active'];  
    $userSuperAdmin=mysql_real_escape_string($_POST['superAdmin']);
    $userCorporationName=????????????;
            
    $q=mysql_query("insert into UserAdmin (First Name, Last Name, Username, Password, Active, Super Admin, Corporations, Date) values('$userFirstName','$userSurName','$userUserName','userPassWord','userActive','$userCorporationName')") or die (mysql_error());
    $update=1;
    }
    }

  2. #2
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    907
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok I managed to get them in as used implode...

    Code:
    <?php
     $q="select * from Corporations";
     $result = mysql_query($q) or die(mysql_error());
     while($row = mysql_fetch_assoc($result)) {
        echo <<<EOL
     <div class="myCheckbox">
     <input type="checkbox" name="corpName[]" value="$row[ID]" /> <span>{$row['Name']}</span><br />
     </div>
    EOL;
    } ?>
    
    $userFirstName=mysql_real_escape_string($_POST['firstName']);
    $userSurName=mysql_real_escape_string($_POST['surName']);
    $userUserName=mysql_real_escape_string($_POST['userName']);
    $userPassWord=mysql_real_escape_string($_POST['pWord']);
    $userActive=$_POST['Active'];  
    $userSuperAdmin=$_POST['superAdmin'];
    $userCorporationName=implode( ';' , $_POST['corpName']);
    $dt2=date("Y-m-d");
            
    mysql_query("INSERT INTO UserAdmin (`First Name`, `Last Name`, `Username`, `Password`, `Active`, `Super Admin`, `Corporations`, `Date`) values('$userFirstName','$userSurName','$userUserName','$userPassWord','$userActive','$userSuperAdmin','$userCorporationName','$dt2')") or die (mysql_error());
    As its the same form to add users and to edit the users, can someone help me adapt the code below, so that if the checkbox value is in the array it checks it already.

    Code:
    <?php
     $q="select * from Corporations";
     $result = mysql_query($q) or die(mysql_error());
     while($row = mysql_fetch_assoc($result)) {
        echo <<<EOL
     <div class="myCheckbox">
     <input type="checkbox" name="corpName[]" value="$row[ID]" /> <span>{$row['Name']}</span><br />
     </div>
    EOL;
    } ?>

  3. #3
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    907
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I recognise that I got to get the array values out so have done that when they click to edit the user.

    Code:
    if(isset($_GET['stock_Id']))
    {
     $stock_Id=$_GET['stock_Id'];
     $q=mysql_query("select * from UserAdmin where ID=$stock_Id") or die (mysql_error()); 
     $rows=mysql_fetch_assoc($q);
     $ccorporationName=$rows["Corporations"];
     $flag=0;
    }
    And obviously somehow I need to match up the value in the checkbox to the value of the array fromt he database.

    Code:
    <?php
     echo $ccorporationName;
     
     $q="select * from Corporations";
     $result = mysql_query($q) or die(mysql_error());
     while($row = mysql_fetch_assoc($result)) {
        echo <<<EOL
     <div class="myCheckbox">
     <input type="checkbox" name="corpName[]" value="$row[ID]" /> <span>{$row['Name']}</span><br />
     </div>
    EOL;
    } ?>
    I was going to put an if statement around the input checkbox code with if value matches check checkbox, and if not dont check the box, but cant seem to get the code right to do it, if that is the best way

  4. #4
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    907
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its wrong but somehting like this, i sort of had in my mind...

    Code:
    <?php
     echo $ccorporationName;
     
     $q="select * from Corporations";
     $result = mysql_query($q) or die(mysql_error());
     while($row = mysql_fetch_assoc($result)) {
        echo <<<EOL
     <div class="myCheckbox">
     if ($row[ID] like $ccorporationName) {
     <input type="checkbox" name="corpName[]" value="$row[ID]" checked /> <span>{$row['Name']}</span><br />
     } else {
     <input type="checkbox" name="corpName[]" value="$row[ID]" /> <span>{$row['Name']}</span><br /> 
     }
     </div>
    EOL;
    } ?>
    This above actually outputs this below onto the page, so its obviously not right, but the idea i was looking to get too -

    [QUOTE][
    if (6 like 6;10) { Keenton International Limited
    } else { Keenton International Limited
    } if (7 like 6;10) { Unotron Limited (HK)
    } else { Unotron Limited (HK)
    }
    if (8 like 6;10) { Unotron Ltd (UK)
    } else { Unotron Ltd (UK)
    }
    if (9 like 6;10) { Unotron Inc (USA)
    } else { Unotron Inc (USA)
    }
    if (10 like 6;10) { Padsafe Limited
    } else { Padsafe Limited
    }
    if (11 like 6;10) { Kamwick International Limited
    } else { Kamwick International Limited
    }

    /QUOTE]

    The top ones are checked, and the bottoms ones arent, but obviously i want it so it checks the right checkboxes and leaves the ones that dont need checking.

    So changed it to this, and better as no outputting everything, but the matching of the values is done the wrong way

    Code:
    <?php
     //echo $ccorporationName;
     
     $q="select * from Corporations";
     $result = mysql_query($q) or die(mysql_error());
     while($row = mysql_fetch_assoc($result)) { ?>
     <div class="myCheckbox">
     <?php if (($row['ID']) == $ccorporationName) { ?>
     <input type="checkbox" name="corpName[]" value="<?php echo $row['ID']?>" checked /> <span><?php echo $row['Name']?></span><br />
     <?php } else { ?>
     <input type="checkbox" name="corpName[]" value="<?php echo $row['ID']?>" /> <span><?php echo $row['Name'] ?></span><br /> 
     <?php } ?>
     </div>
    <?php } ?>

  5. #5
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    907
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ive sort of dealt with the like issue by using strpos, but my problem is how do I break the array up inside $ccorporationName, to allow the compare to occur and so then to dictate which checkbox is checked or not.

    Code:
     <?php if (strpos($row['ID'], $ccorporationName) !== FALSE) { ?>
     <input type="checkbox" name="corpName[]" value="<?php echo $row['ID']?>" checked /> <span><?php echo $row['Name']?></span><br />
     <?php } else { ?>
     <input type="checkbox" name="corpName[]" value="<?php echo $row['ID']?>" /> <span><?php echo $row['Name'] ?></span><br /> 
     <?php } ?>

  6. #6
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    907
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm really struggling with this now, as it seems I can get the array values and at first create the array, but tying them both together is hard work.

    This is where I am at the moment

    Code:
    $q="select * from Corporations";
     $result = mysql_query($q) or die(mysql_error());
     while($row = mysql_fetch_assoc($result))
     $query  = "select Corporations from UserAdmin where ID = $stock_Id";
     $result2 = mysql_query($query);
     $rows = array();  
     while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) 
     {
        $rows[] = $row;
     }
     print "<pre>".print_r($rows,true)."</pre>"; 
     { ?>
     <div class="myCheckbox">    
     <?php if (strstr($row['ID'], $words) !== FALSE) { ?>
     <input type="checkbox" name="corpName[]" value="<?php echo $row['ID']?>" checked /> <span><?php echo $row['Name']?></span><br />
     <?php } else { ?>
     <input type="checkbox" name="corpName[]" value="<?php echo $row['ID']?>" /> <span><?php echo $row['Name'] ?></span><br /> 
     <?php } ?>   
     </div>
    <?php } ?>
    I can print the array out ok, but its exploding those values and then printing all the checkboxes and marking the ones checked if they need to be.

    This is what I can see at the moment, which is the array but no checkboxes.

    Code:
    Array
    (
        [0] => Array
            (
                [Corporations] => 7;9;11
            )
    
    )
    
    Notice: Undefined variable: words in /home/linweb30/u/utgroup.co.uk/user/htdocs/admin/users.php on line 288

  7. #7
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    725
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Kind of a mess you've got going here. Not sure how deep the $rows array will be... one record or more. Without rethinking this whole process you might try this.
    PHP Code:
     { ?>
     <div class="myCheckbox">
     <?php 
     $corpIDs 
    explode(";",$rows[0]['Corporations']);
     
    $checked = (in_array($row['ID'],$corpIDs) ? " checked=\"checked\"" '');
     
    ?>
     <input type="checkbox" name="corpName[]" value="<?php echo $row['ID']?><?php echo $checked?> /> <span><?php echo $row['Name']?></span><br />    
     </div>
    <?php ?>

  8. #8
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    907
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ye sorry, it became a right mess in the end, it had me going crazy then I think I got all screwed up with it.

    Thanks for the reply though/

    I have taken your code and I think added it to mine, and wondered if you could take a look, as I probably gone wrong somewhere

    Code:
    $q="select * from Corporations";
     $result = mysql_query($q) or die(mysql_error());
     while($row = mysql_fetch_assoc($result))
     $query  = "select Corporations from UserAdmin where ID = $stock_Id";
     $result2 = mysql_query($query);
     $rows = array();  
     while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) 
     {
        $rows[] = $row;
     }
     print "<pre>".print_r($rows,true)."</pre>"; 
     { ?> 
     <div class="myCheckbox"> 
     <?php  
     $corpIDs = explode(";",$rows[0]['Corporations']); 
     $checked = (in_array($row['ID'],$corpIDs) ? " checked=\"checked\"" : ''); 
     ?> 
     <input type="checkbox" name="corpName[]" value="<?php echo $row['ID']?>" <?php echo $checked; ?> /> <span><?php echo $row['Name']?></span><br />     
     </div> 
    <?php } ?>
    Sorry if I have missed the point somwhere, as Im not getting the checkboxes showing.
    I will have a look myself too,

  9. #9
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    725
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    You also have a conflict with $row going on here. You seem to be grabbing a single record from UserAdmin based on the variable $stock_Id, so I would grab just the Corporations field and explode it before going into your Corporations loop.
    PHP Code:
    $query  "select Corporations from UserAdmin where ID = $stock_Id";
    $result2 mysql_query($query);
    $rowu mysql_fetch_array($result2MYSQL_ASSOC); 
    $corpIDs explode(";",$rowu['Corporations']); 
    Then as before with the loop.
    PHP Code:
    $q="select * from Corporations";
    $result = mysql_query($q) or die(mysql_error());
    while($row = mysql_fetch_assoc($result))    
    { ?>
        <div class="myCheckbox">
            <?php
            $checked 
    = (in_array($row['ID'],$corpIDs) ? " checked=\"checked\"" '');
            
    ?>
            <input type="checkbox" name="corpName[]" value="<?php echo $row['ID']?><?php echo $checked?> /> <span><?php echo $row['Name']?></span><br />    
        </div>
    <?php ?>

  10. #10
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Chepstow, South Wales
    Posts
    907
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you Drummin,
    thats seems to have done the trick, so thank you very much.

    It's midnight here and been at this since 8am this morning, so can get some sleep without it playing on my mind.

    Thanks again


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •