...

View Full Version : Function Question Help!



angst
06-28-2005, 05:31 PM
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
06-28-2005, 05:48 PM
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

Kid Charming
06-28-2005, 05:51 PM
This doc page (http://us3.php.net/manual/en/language.types.array.php#language.types.array.foo-bar) will explain.

angst
06-28-2005, 06:07 PM
that did the trick!

thanks!!

-Ken

angst
06-28-2005, 06:24 PM
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

schleppel
06-28-2005, 06:43 PM
<?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 ', '


// 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.
?>

Kid Charming
06-28-2005, 06:46 PM
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.

Kurashu
06-28-2005, 07:47 PM
Why....



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


...instead of...



$str_new = implode(',' $prof['relationship']);


?

schleppel
06-28-2005, 07:57 PM
because i suck :)

angst
06-28-2005, 08:12 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum