something wrong with number being cut off to 1 digit

Discussion in 'PHP' started by zenite, May 18, 2008.

  1. #1
    ok, I wrote a little php script that seems to be working, but somehow, 1 of the var keeps getting cut off. it is a script to process info from a form.

    here is a part of the php code:

    $episode[0] = $_POST['episode'];
    $pDay[0] = $_POST['day'];
    echo $episode[0]."<br>".$pDay[0];
    PHP:
    this is the form code:
    <input type="text" name="episode" size="3" maxlength="3">
    <input type="text" name="day" size="3" maxlength="2">
    HTML:
    when I input 120 for 'episode' and 22 for 'day', the output should be:
    120
    22

    instead, I got this:
    1
    22

    what happens is that the 120 is being cut to the first number "1", with "20" omitted. I have no idea why this is happening. if there is an error, why is it that $pDay is working fine? pls help. I am out of ideas.
     
    zenite, May 18, 2008 IP
  2. PowerExtreme

    PowerExtreme Banned

    Messages:
    2,118
    Likes Received:
    75
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Strange nothing looks wrong in the code...

    do 1 thing try it without the arrays and see if it works properly or no and post it here
     
    PowerExtreme, May 18, 2008 IP
  3. zenite

    zenite Peon

    Messages:
    240
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    it works if I am using $episode instead of array. any ideas? I need arrays because I am using a for loop with $episode[$n] and $n as a counter. it cant be the for loop fault because I used an echo before the for loop, right at the start of the script.
     
    zenite, May 18, 2008 IP
  4. PowerExtreme

    PowerExtreme Banned

    Messages:
    2,118
    Likes Received:
    75
    Best Answers:
    0
    Trophy Points:
    0
    #4
    yes it would be better if u post the full code
     
    PowerExtreme, May 18, 2008 IP
  5. zenite

    zenite Peon

    Messages:
    240
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    <form action="process.php" method="post">
    <h3>Number of Episodes:</h3>
    <select name="epcount">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    </select>
    <br /><br />
    <h3>First Episode:</h3>
    <input type="text" name="episode" size="3" maxlength="3">
    <br /><br />
    <h3>Month:</h3>
    <select name="month">
    <option value="1">January</option>
    <option value="2">February</option>
    <option value="3">March</option>
    <option value="4">April</option>
    <option value="5">May</option>
    <option value="6">June</option>
    <option value="7">July</option>
    <option value="8">August</option>
    <option value="9">September</option>
    <option value="10">October</option>
    <option value="11">November</option>
    <option value="12">December</option>
    </select>
    <br /><br />
    <h3>Day:</h3>
    <input type="text" name="day" size="3" maxlength="2">
    <br /><br />
    <input type="submit" name="submit" value="Preview">
    <br /><br />
    </form>
    HTML:

    
    <?php
    $textMonth = array( "", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", 
    
    "November", "December");
    
    $pDay[0] = $_POST['day'];				//set the first day
    $episode[0] = $_POST['episode'];			//set the first episode
    $pMonth[0] = $_POST['month'];				//set the first month
    $count = $_POST['epcount'] - 1;
    
    for ($n=0; $n<=$count; $n++) {
    if ($n!=0) {						//calculate the next episode values
    	$i = $n-1;					//i is the previous episode
    	$episode[$n] = $episode[$i]+1;
    	$pDay[$n] = $pDay[$i]+7;
    	if ($pDay[$n]>$dayLimit) {			//check if it exceeds to the next month
    		$pDay[$n] = $pDay[$n]-$dayLimit;
    		$pMonth[$n] = $pMonth[$i]+1;		//change to the next month
    	}
    	else $pMonth[$n] = $pMonth[$i];
    }
    
    							//find the last day of the month
    if (($pMonth[$n]==1)||($pMonth[$n]==5)||($pMonth[$n]==7)||($pMonth[$n]==10)||($pMonth[$n]==12))
    	$dayLimit = 31;
    
    else if (($pMonth[$n]==2)||($pMonth[$n]==3)||($pMonth[$n]==11))
    	$dayLimit = 29;
    
    else
    	$dayLimit = 30;
    }
    ?>
    PHP:
    there it is. I removed the echo commands from the php though.
     
    zenite, May 18, 2008 IP
  6. zenite

    zenite Peon

    Messages:
    240
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    ok, I have fixed the problem. I still do not know why array doesn't work. I removed array $episode[$n] and replaced it with $_POST['episode'] instead, with the command $_POST['episode']++ placed in the if statement. much cleaner coding I think.
     
    zenite, May 18, 2008 IP