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
Share on other sites

Oops, forgot something.  The information that I want is in the switch statements basically.

For example, If $city = 1 and $location = 1 then result I would be looking for is

$echo $loc_pro;  //Should dispay 1.5
$echo $loc_thug;  //Should display 1
Link to comment
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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

Probably has to do with my reusing the city variable.

Try this:
[code]
function city($city,$location){
  include('cities.php');
  global $loc_pro, $loc_thug;
  $info['pro'] = $loc_pro;
  $info['thug'] = $loc_thug;
  return $info;
}
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.