Jump to content

problem with arrays


Lostnode

Recommended Posts

Here is my code snippets:

 

<form name="form2" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<?php   
     $i=1;
   while($i<=floor($_REQUEST['numpack']))
  {
  ?>
  <table width="325" border="0" cellspacing="0" cellpadding="0" style="float: left;" class="aluminium">
  <tr>
    <td align="center" rowspan="2"><span class="transparent" style="color: #FFF; font-weight: bold;"><?=$i; ?>.</span></td>
    <td align="center">L</td>
    <td align="center">x</td>
    <td align="center">W</td>
    <td align="center">x</td>
    <td align="center">H</td>
  </tr>
  <tr>
    <td align="center"><input name="length[]" type="text"  size="4" maxlength="3" /></td>
    <td align="center">x</td>
    <td align="center"><input name="width[]" type="text" size="4" maxlength="3" /></td>
    <td align="center">x</td>
    <td align="center"><input name="height[]" type="text"  size="4" maxlength="3" /></td>
  </tr>
</table>
    <?php
  $i++;
  }
?>
<input type="hidden" name="numarea" value="<?=$_REQUEST['numpack']; ?>" />
<input name="" type="submit" value="Submit" />
</form>

 

The problem is getting the data from the array set in the above form...

 

This is what I have and it comes back with errors:

 

<?php    
}
If (isset($_REQUEST['numarea'])) {
echo $_REQUEST['numarea'];
$vol=0;
$i=1;
   while($i<=ceil($_REQUEST['numarea']))
  {
    $volume[$i] = $_REQUEST['width'] * $_REQUEST['height'] * $_REQUEST['length'];
    echo $volume[$i];
    $vol = $vol + $volume[$i];  
    $i++;
  }
  echo $vol;  
}
?>

 

The echos are to put out the data when its calculated...

Link to comment
Share on other sites

What errors?  :psychic:

 

2 Fatal error: Unsupported operand types in C:\Program Files (x86)\EasyPHP-5.3.5.0\www\sessions_test.php on line 87 Call Stack: 0.0003 350080 1. {main}() C:\Program Files (x86)\EasyPHP-5.3.5.0\www\sessions_test.php:0 Dump $_SERVER $_SERVER['REMOTE_ADDR'] = '127.0.0.1' $_SERVER['REQUEST_METHOD'] = 'POST' $_SERVER['REQUEST_URI'] = '/sessions_test.php' Variables in local scope (#1): $error = *uninitialized* $i = 1 $vol = 0 $volume = *uninitialized*

 

Line 87 is as follows:

 

   $volume[$i] = $_REQUEST['width'] * $_REQUEST['height'] * $_REQUEST['length'];

Link to comment
Share on other sites

Ok, my knowledge of PHP is extremely limited (learning as I go) how would I be able to do math functions?  The basis is the user put in an amount of rectangles they wish to know the volume of, it then dynamically creates the amount of fields to put in the dimensions of each (my first snipped of code in the first post).  I then need to calculate all the dimensions to get the total volume.

Link to comment
Share on other sites

Ok, however I still have a problem at that point.  Originally I had it set up as follows:

 

<form name="form2" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<?php   
   
   
   
   $i=1;
   while($i<=floor($_REQUEST['numpack']))
  {
  ?>
  <table width="325" border="0" cellspacing="0" cellpadding="0" style="float: left;" class="aluminium">
  <tr>
    <td align="center" rowspan="2"><span class="transparent" style="color: #FFF; font-weight: bold;"><?=$i; ?>.</span></td>
    <td align="center">L</td>
    <td align="center">x</td>
    <td align="center">W</td>
    <td align="center">x</td>
    <td align="center">H</td>
  </tr>
  <tr>
    <td align="center"><input name="length<?=$i;?>" type="text"  size="4" maxlength="3" /></td>
    <td align="center">x</td>
    <td align="center"><input name="width<?=$i;?>" type="text" size="4" maxlength="3" /></td>
    <td align="center">x</td>
    <td align="center"><input name="height<?=$i;?>" type="text"  size="4" maxlength="3" /></td>
  </tr>
</table>
    <?php
  $i++;
  }
?>
<input type="hidden" name="numarea" value="<?=$_REQUEST['numpack']; ?>" />
<input name="" type="submit" value="Submit" />
</form>

 

But I cannot have the following:

 

<?php    
}
If (isset($_REQUEST['numarea'])) {
echo $_REQUEST['numarea'];
$vol=0;
$i=1;
   while($i<=ceil($_REQUEST['numarea']))
  {
    $volume.$i = $_REQUEST['width$i'] * $_REQUEST['height$i'] * $_REQUEST['length$i'];
    echo $volume.$i;
    $vol = $vol + $volume.$i;  
    $i++;
  }
  echo $vol;  
}
?>

 

How do I set it up?

Link to comment
Share on other sites

Ok, do you have any links to point me in the right direction?  I have no idea what i am doing with arrays and how to do what I need with them.  My alternative is to make a page with 300 isset options to do my calculations... I was hoping to limit my coding a bit.

Link to comment
Share on other sites

Ok, as I had said before, my PHP know;edge is extremely lacking... I have always used $_REQUEST to pull variables from a form, but this is the first time using it as an array... I just have no clue where to go from here.

 

With the first part of code, if I put in that I want 50 boxes calcualted, I get 150 variables, width1-50, height1-50 and length1-50 (if you put in 35, you get 1-25 for each)

 

How do I pull this into a mathematical equation?  In other words how do I pull Width25 without stating $_REQUEST['width25']? 

Link to comment
Share on other sites

Yes, and its confusing... as I said I am a beginer LOL...

 

From what I have red on other sites you can use this for example:

 

$ width = $_REQUEST['width'];

foreach ($width as $w) {
echo $w."<br";
}

That would list all the widths.

 

However this does not help me any...

 

I am trying to find examples of what i am trying to do, php.net is very general and I am not at that level yet... I know that sounds strange, but I cannot get my head around it.

 

EDIT actually even testing with that didn;t work, gave me only the first variable.  I want them all not just the first one... so lost.

 

Double Edit also, incase you were wondering (as I have notices you have not helped me with any code) this is NOT a school project, it is actually a part of a larger program I have created for my company (Firewebsolutions.com) The rest of he program works fine... but its this multiple box thing I want that has me jammed up.

Link to comment
Share on other sites

I'm not going to write the code for you because you need to learn how to solve the problem :)

 

When you access $_REQUEST['width'], you're accessing the values at the key of 'width' in the array $_REQUEST.

Using that statement, how would you access the values at the key of '25' in the array of $_REQUEST['width']?

Link to comment
Share on other sites

I would assume by using $_REQUEST['width[$i]'] however that doesn't seem to work very well.

 

$vol=0;
$i=1;  
  while($i<=ceil($_REQUEST['numarea']))
{
   
   echo $_REQUEST['width[$i]'];
   $volume.$i = $_REQUEST['width[$i]'] * $_REQUEST['height[$i]'] * $_REQUEST['length[$i]'];
   $vol = $vol + $volume.$i;  
   $i++;
  }
  echo $vol;  

 

It comes back with (and I have never seen this before)

 

'2', 1 => '3') 

 

EDIT

Tried it again and got this message:

Fatal error: Maximum execution time of 30 seconds exceeded in C:\Program Files (x86)\EasyPHP-5.3.5.0\www\sessions_test.php on line 91 Call Stack: 0.0003 349920 1. {main}() C:\Program Files (x86)\EasyPHP-5.3.5.0\www\sessions_test.php:0 Dump $_SERVER $_SERVER['REMOTE_ADDR'] = '127.0.0.1' $_SERVER['REQUEST_METHOD'] = 'POST' $_SERVER['REQUEST_URI'] = '/sessions_test.php' Variables in local scope (#1): $error = *uninitialized* $i = 0 $vol = '00' $volume = *uninitialized* 

Link to comment
Share on other sites

Imagine if you had spent some time and read the link I posted. It's not even halfway down the page. (but it is the last example they give on how to use arrays) You got close, but just by reading the link I posted, you could have learned. I personally don't coddle people. If someone gives you the exact answer, it's cheating you out of learning. Here's the link again: http://us.php.net/manual/en/language.types.array.php Googling "PHP Arrays" also provides at least 3 pages right off the bat that explain the way to access values in multi-dimensional arrays.

 

And by the deadline you set, I'm sure now you mean your professor set right? ;)

 

One of the members here has a great signature, about how programming is just problem solving in a different language. You have to learn the language. You also need to learn problem solving skills. I showed this page to someone who doesn't program or do anything with computers, and they figured out the solution. By reading the page. I'm not insulting you here, I'm explaining that the tools to solve the problem were given to you. If you wasted your time, it's because you didn't use the tools.

Link to comment
Share on other sites

I did read the doc you linked to, and unfortunately no where in there can I find anything similar to what I am trying to achieve...  I also could not find anything to do with retrieving a value from a form other than this post - http://us.php.net/manual/en/language.types.array.php#77236 which I have no idea how to read as I have never seen anything like $_POST['id']['other'] = 'val1';  which to me is not getting a value, its setting it somehow... Again, not much help...

 

And if you read my project site you would know I am not in School, hence there is no professor to submit too... Its a join project between myself and another company  I can program it the isset way no problem and it would function fine.  However I am trying to cut down the code so the server doesn't have a heart attack while trying to process shits load of code.  If you have an example that pertains directly to what i am trying to achieve, then please post... I.E. Getting a variable from an HTML form using $_POST or $_REQUEST to out put the array.

 

I do not mean to be harsh, I am just extremely frustrated that I cannot grip what you are talking about because my knowledge of PHP is extremely basic, and reading pure oho when its out of contect to what I am looking for confuses the hell of me making me more frustrated.

Link to comment
Share on other sites

I did read the doc you linked to, and unfortunately no where in there can I find anything similar to what I am trying to achieve...  I also could not find anything to do with retrieving a value from a form other than this post - http://us.php.net/manual/en/language.types.array.php#77236 which I have no idea how to read as I have never seen anything like $_POST['id']['other'] = 'val1';  which to me is not getting a value, its setting it somehow... Again, not much help...

 

And if you read my project site you would know I am not in School, hence there is no professor to submit too... Its a join project between myself and another company  I can program it the isset way no problem and it would function fine.  However I am trying to cut down the code so the server doesn't have a heart attack while trying to process shits load of code.  If you have an example that pertains directly to what i am trying to achieve, then please post... I.E. Getting a variable from an HTML form using $_POST or $_REQUEST to out put the array.

 

I do not mean to be harsh, I am just extremely frustrated that I cannot grip what you are talking about because my knowledge of PHP is extremely basic, and reading pure oho when its out of contect to what I am looking for confuses the hell of me making me more frustrated.

 

If $_POST['id']['other'] = 'val1'; is setting the value, how do you retrieve it?

 

check out print_r() and var_dump() as well. Use <pre> first.

Link to comment
Share on other sites

you could have told me my $i was in the wrong place.

after reading http://www.webcheatsheet.com/PHP/multidimensional_arrays.php i figured it out,

 

If (isset($_REQUEST['numarea'])) {

$vol=0;
$i=0;  
  while($i<=ceil($_REQUEST['numarea']))
{
   $volume = $_REQUEST['width'][$i] * $_REQUEST['height'][$i] * $_REQUEST['length'][$i];
   $vol = $vol + $volume;  
   $i++;
  }
  echo $vol;  
}

 

works like a charm

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.