Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Apr 2010
    Posts
    417
    Thanks
    4
    Thanked 1 Time in 1 Post

    how to access the ref number of an array

    I wish to access the following chart by the number and the headings names.

    field text mysqlfield
    1 username Please enter a username un
    2 name Please enter you name nm
    3 age Please enter your age ag
    4 town What town do you live in tw
    5 postcode What is your postcode pc

    I place the field and the text in to a form and use the mysqlfield to placce the data in the mysql database.

    what i do not know is how to store the array data and how to access it.
    Last edited by jasonpc1; 11-18-2010 at 11:16 PM.

  • #2
    Regular Coder
    Join Date
    Apr 2010
    Posts
    417
    Thanks
    4
    Thanked 1 Time in 1 Post
    ok been playing around with stuff from php.net site.

    this is what i have, but how do i get the number 1,2,3,4,5 ?
    Code:
    $array = array(
          '1'=>   array('fieldtext'=>'option1', 'mysqlfield'=>'mysql1'),
          '2'=>   array('fieldtext'=>'option2', 'mysqlfield'=>'mysql2'),
          '3'=>   array('fieldtext'=>'option3', 'mysqlfield'=>'mysql3'),
          '4'=>   array('fieldtext'=>'option4', 'mysqlfield'=>'mysql4'),
          );
    
    foreach ($array as $options) {
             foreach ($options as $key => $value) {
             echo("number = [". $number . "]   -   key = [". $key . "]   -   value = [". $value . "]<br>");
             }
             ?><br><br><?
    }

  • #3
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Code:
    foreach ($array as $pkey => $options) {
    $pkey will be the parent key for each nested array.

  • #4
    Regular Coder
    Join Date
    Apr 2010
    Posts
    417
    Thanks
    4
    Thanked 1 Time in 1 Post
    Quote Originally Posted by MattF View Post
    Code:
    foreach ($array as $pkey => $options) {
    $pkey will be the parent key for each nested array.
    thank you MattF, but I still do not know what I do with this.

    replace? add?, I have not been using array for long so need all the help I can get.

  • #5
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    Quote Originally Posted by jasonpc1 View Post
    thank you MattF, but I still do not know what I do with this.

    replace? add?, I have not been using array for long so need all the help I can get.
    to see how it works make the simple output:
    PHP Code:
    $array = array(
          
    '1'=>   array('fieldtext'=>'option1''mysqlfield'=>'mysql1'),
          
    '2'=>   array('fieldtext'=>'option2''mysqlfield'=>'mysql2'),
          
    '3'=>   array('fieldtext'=>'option3''mysqlfield'=>'mysql3'),
          
    '4'=>   array('fieldtext'=>'option4''mysqlfield'=>'mysql4'),
          
    'A'=>   array('fieldtext'=>'optionA''mysqlfield'=>'mysqlA'),
          
    'B'=>   array('fieldtext'=>'optionB''mysqlfield'=>'mysqlB'),
          
    'word'=>   array('fieldtext'=>'word''mysqlfield'=>'mysql word'),
          
    'another word'=>   array('fieldtext'=>'option another''mysqlfield'=>'mysql another')
          );
    foreach (
    $array as $pkey => $options) {
       echo 
    $pkey ' - ' $options['fieldtext'] . ' = ' $options['mysqlfield'] . '<br />';

    Last edited by poyzn; 11-19-2010 at 10:21 AM.

  • #6
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post
    I don't know, but on the other hand I don't even want to, as databases shouldn't work that way: linked tables are the way to go, in my opinion.
    What I mean is: if you have a record with many numbers (the array) associated to it, then any good database developer will tell you that you should have two tables, one to store the record, and the other to store the associated array - one number per row, with as many rows as array entries.

  • #7
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Code:
    $array = array(
          '1'=>   array('fieldtext'=>'option1', 'mysqlfield'=>'mysql1'),
          '2'=>   array('fieldtext'=>'option2', 'mysqlfield'=>'mysql2'),
          '3'=>   array('fieldtext'=>'option3', 'mysqlfield'=>'mysql3'),
          '4'=>   array('fieldtext'=>'option4', 'mysqlfield'=>'mysql4'),
          );
    
    foreach ($array as $pkey => $options) {
             foreach ($options as $key => $value) {
             echo('number = ['.$pkey.']   -   key = ['.$key.']   -   value = ['.$value.']<br>');
             }
             ?><br><br><?
    }

  • #8
    Regular Coder
    Join Date
    Apr 2010
    Posts
    417
    Thanks
    4
    Thanked 1 Time in 1 Post
    I think I understand what is going on with the array now.
    Thank you for your answer to this.


  •  

    Posting Permissions

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