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 10 of 10
  1. #1
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts

    Question Function Question Help!

    Hello,
    I'm trying to full info from what looks like a function is an existing script that i'm trying to modify.

    so lets say that the value i pull from the db is, $ethnicity with shows up as 4,

    how do I get the correct entry from this function,

    $prof[ethnicity][0] = "I prefer not to say";
    $prof[ethnicity][1] = "African";
    $prof[ethnicity][2] = "African American";
    $prof[ethnicity][3] = "Asian";
    $prof[ethnicity][4] = "Caucasian";
    $prof[ethnicity][5] = "East Indian";
    $prof[ethnicity][6] = "Hispanic";
    $prof[ethnicity][7] = "Indian";
    $prof[ethnicity][8] = "Latino";
    $prof[ethnicity][9] = "Mediterranean";
    $prof[ethnicity][10] = "Middle Eastern";
    $prof[ethnicity][11] = "Mixed";


    I thought i would be something like:
    $ShowEthnicity= $prof[ethnicity]['.$ethnicity.'];

    but it's not working, and i'm not getting any errors,,

    can someone please show me what i'm doing wrong here?

    thanks in advance for your time!
    -Ken

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    ok, i setup a test script
    and half of it seems to be work,


    my code:

    <?php

    $prof[ethnicity][0] = "I prefer not to say";
    $prof[ethnicity][1] = "African";
    $prof[ethnicity][2] = "African American";
    $prof[ethnicity][3] = "Asian";
    $prof[ethnicity][4] = "Caucasian";
    $prof[ethnicity][5] = "East Indian";
    $prof[ethnicity][6] = "Hispanic";
    $prof[ethnicity][7] = "Indian";
    $prof[ethnicity][8] = "Latino";
    $prof[ethnicity][9] = "Mediterranean";
    $prof[ethnicity][10] = "Middle Eastern";
    $prof[ethnicity][11] = "Mixed";


    echo $prof[ethnicity][4];
    ?>

    I made the value static, as 4.
    this does give me the correct result, but also gives me some errors:


    Notice: Use of undefined constant ethnicity - assumed 'ethnicity' in C:\Program Files\Apache Group\Apache2\htdocs\test.php on line 3

    Notice: Use of undefined constant ethnicity - assumed 'ethnicity' in C:\Program Files\Apache Group\Apache2\htdocs\test.php on line 4

    Notice: Use of undefined constant ethnicity - assumed 'ethnicity' in C:\Program Files\Apache Group\Apache2\htdocs\test.php on line 5

    Notice: Use of undefined constant ethnicity - assumed 'ethnicity' in C:\Program Files\Apache Group\Apache2\htdocs\test.php on line 6

    Notice: Use of undefined constant ethnicity - assumed 'ethnicity' in C:\Program Files\Apache Group\Apache2\htdocs\test.php on line 7

    Notice: Use of undefined constant ethnicity - assumed 'ethnicity' in C:\Program Files\Apache Group\Apache2\htdocs\test.php on line 8

    Notice: Use of undefined constant ethnicity - assumed 'ethnicity' in C:\Program Files\Apache Group\Apache2\htdocs\test.php on line 9

    Notice: Use of undefined constant ethnicity - assumed 'ethnicity' in C:\Program Files\Apache Group\Apache2\htdocs\test.php on line 10

    Notice: Use of undefined constant ethnicity - assumed 'ethnicity' in C:\Program Files\Apache Group\Apache2\htdocs\test.php on line 11

    Notice: Use of undefined constant ethnicity - assumed 'ethnicity' in C:\Program Files\Apache Group\Apache2\htdocs\test.php on line 12

    Notice: Use of undefined constant ethnicity - assumed 'ethnicity' in C:\Program Files\Apache Group\Apache2\htdocs\test.php on line 13

    Notice: Use of undefined constant ethnicity - assumed 'ethnicity' in C:\Program Files\Apache Group\Apache2\htdocs\test.php on line 14

    Notice: Use of undefined constant ethnicity - assumed 'ethnicity' in C:\Program Files\Apache Group\Apache2\htdocs\test.php on line 17
    Caucasian


    the correct value appears at the bottom of the errors on the page.

    what am i doing wrong here, that I get the correct value but still errors?

    thanks again for your time!
    -Ken

  • #3
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This doc page will explain.

  • #4
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    that did the trick!

    thanks!!

    -Ken

  • #5
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    ok, one more question.

    will this same script work with letters insted of numbers?
    like

    $prof['relationship'][act] = "Activity Partner";
    $prof['relationship'][cas] = "Casual";
    $prof['relationship'][fri] = "Friendship";
    $prof['relationship'][mar] = "Marriage";
    $prof['relationship'][rel] = "Relationship";
    $prof['relationship'][rom] = "Romance";
    $prof['relationship'][tra] = "Travel Partner";


    also how do i parse a coma seperated string to get the right values.

    example:

    I would pull:
    act,cas,fri,mar,rel,rom,tra
    from my data base.

    how do i then read into this string, pull the data from the array above. and then have it show up as:
    Activity Partner, Casual, Friendship, and so on?


    thanks again for all you help!
    -Ken

  • #6
    Regular Coder
    Join Date
    Oct 2004
    Posts
    330
    Thanks
    0
    Thanked 13 Times in 13 Posts
    PHP Code:
    <?php
    // Your array
    $prof['relationship'][act] = "Activity Partner";
    $prof['relationship'][cas] = "Casual";
    $prof['relationship'][fri] = "Friendship";
    $prof['relationship'][mar] = "Marriage";
    $prof['relationship'][rel] = "Relationship";
    $prof['relationship'][rom] = "Romance";
    $prof['relationship'][tra] = "Travel Partner";

    // The string from the database
    $str 'act,cas,fri,mar,rel,rom,tra';

    // Split it into diifferent sections
    $str_arr explode(',',$str);

    $str_new '';

    // Make the string.
    foreach($str_arr as $value){
        
    $str_new .= $prof['relationship'][$value] . ', ';
    }

    echo 
    $str_new;
    // OUTPUT: Activity Partner, Casual, Friendship, Marriage, Relationship, Romance, Travel Partner,
    ?>
    The one problem with that is you get an extra ', ' (comma space) on the end of it, there are ways of stopping it...

    Edit: so you don't get the extr ', '
    PHP Code:
    // Split it into diifferent sections
    $str_arr explode(',',$str);

    // Get the first one
    $str_new $prof['relationship'][$str_arr['0']];

    // Make the string
    foreach($str_arr as $key => $value){
        if(
    $key != '0')
            { 
    $str_new .= ', ' $prof['relationship'][$value]; }
    }

    // Add a full stop
    $str_new .= '.';

    echo 
    $str_new;
    // OUTPUT: Activity Partner, Casual, Friendship, Marriage, Relationship, Romance, Travel Partner.
    ?> 
    Last edited by schleppel; 06-28-2005 at 06:50 PM.

  • #7
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If it's a string index, it needs to be quoted, no matter how deep into the array it is. So you should have


    $prof['relationship']['act'] = "Activity Partner";

    The short answer to your db question is that you shouldn't be storing comma-delimited strings in your database. Your db should be structured so that each element in that list has its own field. Do a Google on 'database normalisation' for info. A properly normalized database will allow you to avoid problems like that.

  • #8
    Regular Coder
    Join Date
    Aug 2004
    Location
    The US of A
    Posts
    767
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Why....

    PHP Code:
    foreach($str_arr as $value){
        
    $str_new .= $prof['relationship'][$value] . ', ';

    ...instead of...

    PHP Code:
    $str_new implode(',' $prof['relationship']); 
    ?

  • #9
    Regular Coder
    Join Date
    Oct 2004
    Posts
    330
    Thanks
    0
    Thanked 13 Times in 13 Posts
    because i suck

  • #10
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    thank you!

    that little script was all I needed :-)

    and like i said, I'm just editing an existing set of scripts, if i were to build from ground up it wouldn't be like this.

    thanks again!

    -Ken


  •  

    Posting Permissions

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