Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

explode() working on one server but not another, why?

Status
Not open for further replies.

trantron

Programmer
Joined
Feb 12, 2004
Messages
15
Location
US
I am running this code on version 4.3.4

This code takes a range of years e.g. 1980-85 and inserts into the database 80,81,82,83,84,85

$yr=explode("-", $years);
if($yr)
{
$left=substr($yr[0],-2);
$right=substr($yr[1],-2);
$var=range("$left","$right");
for($i=0; $i < $right; $i++)
$years=implode(&quot;,&quot;, $var);

}//end if


$query=&quot;INSERT INTO midway(div, rate, name, years, email, photo, notes, pass, id)
VALUES (trim('$div'), trim('$rate'), trim('$name'), trim('$years'), trim('$email'), trim('$photo'),trim('$notes'), trim('$pass'), '$id' )&quot;;

I then use the variable $years in a database.

The code works perfect on version 4.3.4 but when I run it on version 4.2.1 it does not work

I only get a singe digit. In this case it would just be an 8

Does anybody know why??

Thanks
 
Yes. Using that example only an 8 (single digit) would be sent to the database.

 
What are the values in all your scratch variables: $yr, $left, $right and $var?


I've also noticed some other things about your code snippet that interest me.

One is that you're performing an if-clause using $yr as the expression. Since explode() always returns something, even if just a one-element array with the original string in that element, the if clause will always evaluate to true. It's therefore superfluous.

Another is that you're invoking implode() inside a for-loop. That's unnecessary, since your code overwrites the array on each iteration. It's also superfluous.

The third is that your code doesn't have loops to react to differences in input. For example, what will your loop do if you input 1980?

I've taken the liberty of rewriting your script snippet:

Code:
<?php
$year = '1980-85';
$yr = explode (&quot;-&quot;, $year);
switch (count($yr))
{
	case 1:
		$year_list = substr ($yr[0], -2);
		break;
	case 2:
		$left =  substr ($yr[0], -2);
		$right = substr ($yr[1], -2);
		if ($left > $right)
		{
			$temp = $left;
			$left = $right;
			$right = $temp;
		}
		$var = range ($left, $right);
		$year_list = implode (&quot;,&quot;, $var);
		break;
	default:
		die (&quot;Error on input&quot;);
}

print $year_list;
?>



Want the best answers? Ask the best questions: TANSTAAFL!!
 
I just ran this code, but the result is the same. Using the range 1980-1988, for example, the result is still just a single &quot;8&quot; being sent to the database.
 
I narrowed the problem down to the range() function

Using the years 1980-1984 as an example:

$left=substr($yr[0],-2); // $yr[0] would be &quot;80&quot;

$right=substr($yr[1],-2);// $yr[0] would be &quot;84&quot;

$var=range(&quot;$left&quot;,&quot;$right&quot;); //$left=80
//$right=84

This is were the problem is. According to the php manual:

Note: Prior to PHP version 4.1.0, range() only generated incrementing integer arrays. Support for character sequences and decrementing arrays was added in 4.1.0. Character sequence values are limited to a length of one. If a length greater than one is entered, only the first character is used.

This does not quite match my situation because it is working on PHP Version 4.3.4, but not on Version 4.2.1

The actual finished application needs to sit on Version 4.2.1

Do you know of a way that I can get the same result from range() without actually using it?

Thanks for all of your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top