Jump to content

Need Help With A Function


tcollie

Recommended Posts

Okay, here's what I have going.  I've created a file called cities.php.  This file stores information about cities that I'm using in a site.  Here is what it looks like:

[code]if($city == "1"){
  $city_name = "New York";
  $city_id = "1";
  $loc_01 = "Grand Central Station";
  $loc_02 = "Central Park";
  $loc_03 = "Maxim Lounge";
  $loc_04 = "Madison Square Garden";
  $loc_05 = "Coney Island";
  //Begin City Specific Switch Here
//This page New York City locations
switch ($location) {
case 1:
  $location_name = 'Grand Central Station';
  $loc_pro = 1.5;
  $loc_thug = 1;
  break;
case 2:
  $location_name = 'Central Park';
  $loc_pro = 1;
  $loc_thug = 1.95;
  break;
case 3:
  $location_name = 'Maxim Lounge';
  $loc_pro = 1.25;
  $loc_thug = 1;
  break;
case 4:
  $location_name = 'Madison Square Garden';
  $loc_pro = 1;
  $loc_thug = 1.25;
  break;
case 5:
  $location_name = 'Coney Island';
  $loc_pro = 0.5;
  $loc_thug = 0.75;
  break;
}
} elseif($city == "2"){
$city_name = "Washington DC";
  $city_id = "2";
  //Begin City Specific Switch Here
}else {
echo "You Aren't In Any City";
}[/code]

Now what I'm attempting to do is create a function that retrieves the values from the switch statements within cities.php.  So far I have the following function:

[code]function city($city,$location)
  {
  include('cities.php');
  return $loc_pro;
  return $loc_thug;
  }[/code]

And my function call is like this:

[code]city($city,$location);[/code]

So if this is all working correctly I should be able to just pass my variables to my function call and run a simple echo statement to give me the 2 variables in the function that I want.  But obviously this isn't working or I wouldn't be posting it here.  I can get the results I want if I simply edit the cities.php file and include the 2 variables $city and $location and run an echo script at the bottom of the file.  I will show the information that I want.  Whenever I try and do it the way I have it written, I just get a blank screen.

Can somebody give me some direction on how to make this work, or is there another option for getting the same results that I want?  Thanks in advance.
Link to comment
https://forums.phpfreaks.com/topic/32784-need-help-with-a-function/
Share on other sites

Try this:
[code]
function city($city,$location){
  include('cities.php');
  global $loc_pro, $loc_thug;
  $city['pro'] = $loc_pro;
  $city['thug'] = $loc_thug;
  return $city;
}
[/code]
AFAIK: You can only return once per function, so what you had won't work.
You might want to move this info into a database.
I'm most likely going to have to move it to the database.  This would be much simpler, but I'm really just trying to cut down on calls to the database.  Any way, after trying your suggestion, I get the following error:

[code]Warning: Cannot use a scalar value as an array in C:\Program Files\xampp\htdocs\thugs\functions.php on line 65

Warning: Cannot use a scalar value as an array in C:\Program Files\xampp\htdocs\thugs\functions.php on line 66[/code]

I have no clue what "scalar values" are.  :o

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.