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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing Array Value

Status
Not open for further replies.

bam720

Technical User
Sep 29, 2005
289
US
Code:
$ModelMFR[] = $_POST['ModelMfr'];
print_r($ModelMFR);

$Fail = PrintOrError($ModelMFR[$i],$Fail);

function PrintOrError($Print,$Fail){
		$Print = stripslashes($Print);
		if($Print == ""){
			echo'<b><font color=red> ERROR </font></b>';
			$Fail++;
		}
		else
			print($Print);
		return $Fail;
	}

outputs:
Array ( [0] => Array ( [0] => Dell [1] => Dell [2] => Dell [3] => Dell ) ) //from print_r

Model Manufacturer: Array //from PrintOrError

My PrintorError works fine with all the other array values just not thsi one. Any ideas?
 
sorry an example of the html where the post data comes from
Code:
  <th scope="row">Model Manufacturer</th>
      <td><center>
          <input name="ModelMfr[0]" type="text" id="ModelMfr" onFocus="if(this.value=='Dell')this.value='';" value="Dell" size="20">
      </center></td>
      <td><center>
          <input name="ModelMfr[1]" type="text" id="ModelMfr" onFocus="if(this.value=='Dell')this.value='';" value="Dell" size="20">
      </center></td>
      <td><center>
          <input name="ModelMfr[2]" type="text" id="ModelMfr" onFocus="if(this.value=='Dell')this.value='';" value="Dell" size="20">
      </center></td>
      <td><center>
          <input name="ModelMfr[3]" type="text" id="ModelMfr" onFocus="if(this.value=='Dell')this.value='';" value="Dell" size="20">
      </center></td>
    </tr>
 
I changed my instantiation from
Code:
$ModelMFR[] = $_POST['ModelMfr'];
to
Code:
$ModelMFR = $_POST['ModelMfr'];
and it works fine now, but i don't understand how it became a multidimensional array when all my other varibles are declared the first way and come out being 1d array's.
 
Yes I understand that but
Code:
$ModelNum[] = $_POST['ModelNum'];
reads all the values from the previous page (see above post with same form elements just named ModelNum) into a 1d array. I am happy that it works now but it bothers me that one is working one way and another is working differently. Thanks
 
This line:

$ModelNum[] = $_POST['ModelNum'];

copies all the elements of $_POST into a single element of the array PHP automatically creates for $ModelNum. The line above is the functional equivalent of saying:

$ModelNum = array()
$ModelNum[0] = $_POST['ModelNum'];

And when you copy an entire array $A into a single element if the array $B, $B then becomes multidimensional.



Want the best answers? Ask the best questions! TANSTAAFL!
 
OK I don't know what I was looking at yesterday. I apologize for the confusion. I tested
Code:
print_r($ModelNum);
and the result was this
Code:
Array ( [0] => SX270 [1] => SX270 [2] => SX270 [3] => SX270 [4] => Array ( [0] => SX270 [1] => SX270 [2] => SX270 [3] => SX270 ) )
which just means I was getting lucky that the only values i cared about were values at index's 0,1,2,3. I understand what you are saying I just didn't believ you becuase it wasn't happening the same way it was for $ModelMFR. I am now going through and testing each case just to be sure and making the appropriate changes. Thanks fr the help and I apologize for the stubbornness
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top