Forum Moderators: coopster

Message Too Old, No Replies

Function Question Help!

function help

         

angst

4:36 pm on Jun 28, 2005 (gmt 0)

10+ Year Member



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

angst

4:47 pm on Jun 28, 2005 (gmt 0)

10+ Year Member



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

sned

4:56 pm on Jun 28, 2005 (gmt 0)

10+ Year Member



I could be wrong, but it looks like there should be some quotes here and there:


$prof[ethnicity][0] = "I prefer not to say";
$prof[ethnicity][1] = "African";
$prof[ethnicity][2] = "African American";

Try this:

$prof['ethnicity'][0] = "I prefer not to say";
$prof['ethnicity'][1] = "African";
$prof['ethnicity'][2] = "African American";

Then you should be able to call it like this:

$ShowEthnicity = $prof['ethnicity'][$ethnicity];

hope that helps ...
-sned

angst

5:06 pm on Jun 28, 2005 (gmt 0)

10+ Year Member



that worked,
thanks!

-Ken

angst

5:24 pm on Jun 28, 2005 (gmt 0)

10+ Year Member



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

sned

6:00 pm on Jun 28, 2005 (gmt 0)

10+ Year Member



Sure, it should work just fine, just put the letters in quotes:


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

should be:

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

If your database returns a string like "act,cas,fri,mar,rel,rom,tra"

You can use the explode [us4.php.net] function: $some_array = explode(',', $database_result);

Then, to pull the data from the $prof['relationship'] array, you would do something like:


foreach($some_array as $id=>$value){
echo $prof['relationship'][$id];
}

That's one of the ways it could be done ....
-sned

angst

7:13 pm on Jun 28, 2005 (gmt 0)

10+ Year Member



kool!

thanks, that works:-)

-Ken