Results 1 to 11 of 11

Thread: [RESOLVED] fill a page with multiple entries from a db

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Resolved [RESOLVED] fill a page with multiple entries from a db

    ok if i have a db called 'skills' how can i add checkboxes for each skill to my registration page dynamically. so for instance if i have 5 skills in the table i want to add 5 checkboxes for each skill... i know this is with php but im not sure of the correct code.. what do i need look at to work this out?

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: fill a page with multiple entries from a db

    the simplest way is to store the fields in a static array and loop through them on the form. this will not be completely dynamic, so if you change your database's table you will need to update the registration form, but, even so, it works well.
    PHP Code:
    <form action="register.php">
    <?php
      $mycheckboxes 
    = array("Caption of field"   => "nameoffield",
                            
    "Caption of field2"  => "nameoffield2",
                            
    "Caption of field3"  => "nameoffield3");
      foreach(
    $mycheckboxes as $caption => $name){ ?>
        <input type="checkbox" name="<?php echo $name?>" /><?php echo $caption?><br />
    <?php ?>
    <input type="submit" value="Register" />
    </form>
    If this isn't what you want, you can build the form's checkboxes by looking through field names of whichever table you like. You will have to query your database to return the table's information (if you can't just grab the fields themselves, that is), and parse said information if necessary.

    Lastly, I hope you don't really have a database called skills.. you should use one database for a website, with many tables inside of it that hold all of the relevant information.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: fill a page with multiple entries from a db

    lol sorry no it is not a database called skills - but a table!

    i need to do this dynamically so if i add new records/skills the registration form can update itself ok. i could this with vb or some other programming languages but no idea where to start with php or sql...

    as long as when the registration form loads up it gets the same number of records from the skills table that is ok. it is fine to update it by refreshing....

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: fill a page with multiple entries from a db

    you need to do a query to grab all skills from the database and then loop them:

    PHP Code:
    $sSQL = "SELECT * FROM skills";

    $rSkills = mysql_query( $sSQL ) or die( mysql_error() );

    while( $aSkills = mysql_fetch_array( $rSkills ) )
    {
    ?>
        <input type="checkbox" name="skills[]" value="<?php echo $aSkills"skill_id" ]; ?>" /><?php echo $aSkills"skill_name" ]; ?> <br />
    <?php
    }
    Of course you'll have to modify that to fit your database schema.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: fill a page with multiple entries from a db

    well, if you're doing it by RECORDS and not the actual field names, then that's VERY simple. here's an example, you should notice that it's very similar to the first example I posted above:
    PHP Code:
    <form action="register.php" method="post">
    <?php
      
    //this assumes that your skills table has both a CAPTION (the text that will show up) and a NAME (name of the form field) field
      
    $sql "SELECT caption, name FROM skills";
      
    $skills mysql_query($sql);
      if(
    mysql_num_rows($skills) > 0){
        while(
    $skill mysql_fetch_array($skills)){ ?>
      <input type="checkbox" name="<?php echo $skill['name']; ?>" /><?php echo $skill['caption']; ?><br />
    <?php
        
    }
      }else{
        
    //no skills found!
        
    echo 'No skills found.';
      }
    ?>
    </form>

  6. #6
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: fill a page with multiple entries from a db

    Then on the form submit, you can loop the results with something similiar to:

    PHP Code:
    if( isset( $_POST"skills" ] ) ) // if none are checked, will be false
    {
        foreach( 
    $_POST"skills" ] as $iSkill_ID )
        {
            
    // do something with $iSkill_ID
        
    }

    My evil laugh has a squeak in it.

    kristopherwilson.com

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: fill a page with multiple entries from a db

    Ok this is what I've tried:

    Code:
    $skills=$_POST['skills'];
    
    if( isset($skills)) // if none are checked, will be false 
    {
    line20>>> foreach($skills as $iSkill_id) 
    { 
    $query = "INSERT INTO Userskills VALUES ('$user','$iSkill_id')";
    mysql_query($query);
    }
    But I'm getting this error:

    Code:
    Warning: Invalid argument supplied for foreach() in ~ on line 20

  8. #8
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: fill a page with multiple entries from a db

    The error means that $skills is not an array. Are you sure you setup the checkboxes like...

    Code:
    <input type="checkbox" name="skills[]" ...
    It is important to have the [] after the name so that it gets sent to PHP as an array.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: fill a page with multiple entries from a db

    Thanks for that - I'm not getting any error now! However the checkbox entries are being added to the correct db and I think it may be because of the php code:

    Code:
    $user=$_POST['username'];
    $pass=$_POST['password'];
    $level=$_POST['levelid'];
    $email=$_POST['email'];
    $biog=$_POST['biog'];
    $signup=date('Y-m-d');
    $skills=$_POST['skills'];
    
    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    
    $query = "INSERT INTO Users VALUES ('','$user','$pass','$level', '$biog', '$email', '$signup')";
    mysql_query($query);
    
    if( isset($skills)) // if none are checked, will be false 
    {
    foreach($skills as $iSkill_id) 
    { 
    $query = "INSERT INTO Userskills VALUES ('', '$user','$iSkill_id')";
    mysql_query($query);
    } 
    
    } 
    
    mysql_close();
    As you can see I'm doing 2 queries, is this Ok?

  10. #10
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: fill a page with multiple entries from a db

    Yeah, that is fine. Is it not working? If not you might want to add a die() clause to your mysql_query()s:

    PHP Code:
    mysql_query($query) or die( mysql_error() ); 
    Sometimes it doesn't always show the errors and it doesn't always give you helpful errors unless you tell it to.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: fill a page with multiple entries from a db

    Thanks I've got it to work!

Posting Permissions

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



Click Here to Expand Forum to Full Width