Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Can someone loan my a gun to shoot my scripts?
Old 08-12-2005, 08:49 AM Can someone loan me a gun to shoot my scripts?
Novice Talker

Posts: 5
Trades: 0
Here's the setup. I have 2 pages, the first is test.php and the second it functions.php. test.php is including functions.php.

I created a function in functions.php called date_arrays and have the code to create 3 arrays. 1 for days, 1 for months, 1 for years.

I am trying to call the date_arrays function from test.php and populate drop_down menus for a form. For some reason I don't believe the array is getting returned to the test.php page. If I move the code to create the array from the functions.php page to the test.php page, it all works fine. I know that it is an issue with getting the array from functions.php to test.php.

functions.php
Code:
<?
require('dbconn.php');

function date_arrays($months) {

	while ($d < 32) {
		$d++;
		$days[$d] = $d;
	}

	$months[1] = 'January';
	$months[2] = 'February';
	$months[3] = 'March';
	$months[4] = 'April';
	$months[5] = 'May';
	$months[6] = 'June';
	$months[7] = 'July';
	$months[8] = 'August';
	$months[9] = 'September';
	$months[10] = 'October';
	$months[11] = 'November';
	$months[12] = 'December';

	$y = 1940;
	while ($y < 2006) {
		$years[$y] = $y;
		$y++;
	}
	return $months;//, $days, $years;
}

?>
test.php
Code:
<form name="form1" method="post" action="">
  <select name="select" size="1">
    <option selected>Test 1</option>
    <option>Test 2</option>
    <option>Test 3</option>
    <option>Test 4</option>
  </select>
  <br><br>
  <select name="month" size="1">
  	<?
		$months = array();
  		date_arrays($months);	
		
		foreach ($months as $current_month) {
			echo "<option>$current_month</option>";
		}	
		
		/*$current_month = current($months);
		if ($current_month) {
			echo "<option>$current_month</option>";
			//echo "<option>Test</option>";
		}
		else {
		}*/
  	?>
  </select>
</form>
Any help is greatly appreciated. Also, if the code isn't done right, tell me. I won't mind, I promise.

Last edited by loki_racer; 08-12-2005 at 09:15 AM..
loki_racer is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-12-2005, 09:39 AM
Vonna.com's Avatar
Experienced Talker

Posts: 43
Trades: 0
Took a quick look,

You did not include the complete code , but i think this is your problem:

$months = array(); ( in test.php )

This will delete the current $months array , so i think you need to do this before you make the "functions.php" include.
__________________
best regards Preben Eriksen. :)

[
Please login or register to view this content. Registration is FREE
] [
Please login or register to view this content. Registration is FREE
] [
Please login or register to view this content. Registration is FREE
]
Vonna.com is offline
Reply With Quote
View Public Profile Visit Vonna.com's homepage!
 
Old 08-12-2005, 09:45 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
That's not the trouble, the function is called after the $months=array() assignment so the ordering is OK.

The problem is that you don't assign the return value to anything, or use references. You can either write the function like this:

PHP Code:
  function date_array() {
  
//create $months array
  
  
return $months;
  } 
and call it with:
$months = date_array();

or create it like this:

PHP Code:
  function date_array(&$months) {
   
//Note the & meaning a reference to $months
   //assign to $months
   //don't bother to return anything
  

and call it with
$months = array();
date_array($months);

The assignments in the function affect the array you pass as an argument since in this case it is passed by reference.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)

Last edited by 0beron; 08-12-2005 at 09:48 AM..
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 08-12-2005, 10:00 AM
Novice Talker

Posts: 5
Trades: 0
Ok. I found a way to make it all work. I created a global variable at the start of the function. I am still trying to do it the first way, I got this error

Code:
<b>Fatal error: Call to undefined function:  date_array() in <b>/home/httpd/vhosts/thecoastalcompanies.com/httpdocs/dev/HR/includes/test.php</b> on line <b>26</b><br />
line 26 =
Code:
date_array($months);
So this is my code so far

test.php

Code:
<? 

include('functions.php');

?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<form name="form1" method="post" action="">
   <br><br>
  <select name="month" size="1">
  	<?
		$months = array();
		date_array($months);	
		
		foreach ($months as $current_month) {
			echo "<option>$current_month</option>";
		}
  	?>
  </select>
</form>
<br>
<br>
</body>
</html>
functions.php =

Code:
<?

function date_arrays() {

	$months[1] = 'January';
	$months[2] = 'February';
	$months[3] = 'March';
	$months[4] = 'April';
	$months[5] = 'May';
	$months[6] = 'June';
	$months[7] = 'July';
	$months[8] = 'August';
	$months[9] = 'September';
	$months[10] = 'October';
	$months[11] = 'November';
	$months[12] = 'December';

	return $months;
}

?>
I got it to work using this code

test.php

Code:
<? 

include('functions.php');

?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<form name="form1" method="post" action="">
   <br><br>
  <select name="month" size="1">
  	<?
		$months = array();
		month_array($months);	
		
		foreach ($months as $current_month) {
			echo "<option>$current_month</option>";
		}
  	?>
  </select>
</form>
<br>
<br>
</body>
</html>
functions.php

Code:
<?

function month_array() {
global $months;

	$months[1] = 'January';
	$months[2] = 'February';
	$months[3] = 'March';
	$months[4] = 'April';
	$months[5] = 'May';
	$months[6] = 'June';
	$months[7] = 'July';
	$months[8] = 'August';
	$months[9] = 'September';
	$months[10] = 'October';
	$months[11] = 'November';
	$months[12] = 'December';

}

?>

Last edited by loki_racer; 08-12-2005 at 10:09 AM..
loki_racer is offline
Reply With Quote
View Public Profile
 
Old 08-12-2005, 10:44 AM
Anacrusis's Avatar
Defies a Status

Posts: 2,099
Name: Adam
Location: Colchester CT
Trades: 0
That's one way to do it, but certainly not the best. It sort of defeats the purpose of having a function.

0beron showed you the correct way of using functions. You either pass a variable by value and return the result, or you pass the variable by reference and return nothing.

I think you need to read up some on how to write functions and use them.
Anacrusis is offline
Reply With Quote
View Public Profile
 
Old 08-16-2005, 01:27 PM
Novice Talker

Posts: 5
Trades: 0
Thanks for the help guys. I finally figured out how to pass information to and from functions correctly, not using the global feature
loki_racer is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Can someone loan my a gun to shoot my scripts?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.17194 seconds with 12 queries