PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > Why doesn't working with array's ever stick in my thick head?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Why doesn't working with array's ever stick in my thick head?

Réponse
 
LinkBack Outils de la discussion
Vieux 09/05/2008, 17h56   #1
Jason Pruim
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Why doesn't working with array's ever stick in my thick head?

Hi Everyone,

SO it's friday, I'm tired, and I can't seem to think straight... I am
attempting to do some very basic math with some arrays... Here's the
pseudo code that I'm working with:

NumberOfPieces * PieceWeight = TotalWeight

explode total weight to get tenth's of pounds, then divide tenth's of
pounds by 16 to grab ounces.

Display TotalWeight # Exploded Ounces.

Which I have working just fine for a single set of boxes...

My problem is I want to be able to automatically add say 50 boxes in a
form to process the weight of each route...

So what I need to do is probably use some kind of a counter to loop
through my $RoutePieces array and multiply by the $PieceWeight?
$PieceWeight is a static number.

Now let me show you some abbreviated code to try and explain what I'm
using right now:
<?PHP
$i= "0";
$num= "2";
$PieceWeight = $_POST['txtPieceWeight'];
$RoutePieces[] = $_POST['txtRoutePieces'];
$RouteNumber[] = $_POST['txtRoute'];

$totalWeight = $PieceWeight * $RoutePieces/16;
$weightExplode = explode('.', $totalWeight);
//$weightOunces = ((float)('.' . $weightExplode[1])) * 16;
// Use stut's method... Seems cleaner
$explodeOunces = ($totalWeight - intval($totalWeight)) * 16;

while($i <= $num){
echo <<<HTML

<p>
Route Number:<input type="text" name="txtRoute[$i]" size="4" value
="{$RouteNumber[$i]}"> Number of pieces: <input type="text"
name="txtRoutePieces[$i]" size="4"
value="{$RoutePieces[$i]}"><label>Total weight of route:
{$weightExplode[0]} # {$explodeOunces} Ounces</label>
</p>


HTML;

$i++;
echo $i;
}
?>

the $_POST array has the proper values stored properly... I just can't
seem to figure out how to work with the values... Any ideas? Or slaps
on the back of the head to wake me up a little?

I need :P



--

Jason Pruim
Raoset Inc.
Technology Manager
MQC Specialist
3251 132nd ave
Holland, MI, 49424-9337
www.raoset.com
japruim@raoset.com




  Réponse avec citation
Vieux 09/05/2008, 18h05   #2
Dan Joseph
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Why doesn't working with array's ever stick in my thick head?

On Fri, May 9, 2008 at 12:56 PM, Jason Pruim <japruim@raoset.com> wrote:

> Hi Everyone,
>
> SO it's friday, I'm tired, and I can't seem to think straight... I am
> attempting to do some very basic math with some arrays... Here's the pseudo
> code that I'm working with:
>
> NumberOfPieces * PieceWeight = TotalWeight
>
> explode total weight to get tenth's of pounds, then divide tenth's of
> pounds by 16 to grab ounces.
>
> Display TotalWeight # Exploded Ounces.
>
> Which I have working just fine for a single set of boxes...
>
> My problem is I want to be able to automatically add say 50 boxes in a form
> to process the weight of each route...
>
> So what I need to do is probably use some kind of a counter to loop through
> my $RoutePieces array and multiply by the $PieceWeight? $PieceWeight is a
> static number.
>
> Now let me show you some abbreviated code to try and explain what I'm using
> right now:
> <?PHP
> $i= "0";
> $num= "2";
> $PieceWeight = $_POST['txtPieceWeight'];
> $RoutePieces[] = $_POST['txtRoutePieces'];
> $RouteNumber[] = $_POST['txtRoute'];
>
> $totalWeight = $PieceWeight * $RoutePieces/16;
> $weightExplode = explode('.', $totalWeight);
> //$weightOunces = ((float)('.' . $weightExplode[1])) * 16;
> // Use stut's method... Seems cleaner
> $explodeOunces = ($totalWeight - intval($totalWeight)) * 16;
>
> while($i <= $num){
> echo <<<HTML
>
> <p>
> Route Number:<input type="text" name="txtRoute[$i]" size="4" value
> ="{$RouteNumber[$i]}"> Number of pieces: <input type="text"
> name="txtRoutePieces[$i]" size="4" value="{$RoutePieces[$i]}"><label>Total
> weight of route: {$weightExplode[0]} # {$explodeOunces} Ounces</label>
> </p>
>
>
> HTML;
>
> $i++;
> echo $i;
> }
> ?>
>
> the $_POST array has the proper values stored properly... I just can't seem
> to figure out how to work with the values... Any ideas? Or slaps on the back
> of the head to wake me up a little?
>
> I need :P
>
>
>
> --
>
> Jason Pruim
> Raoset Inc.
> Technology Manager
> MQC Specialist
> 3251 132nd ave
> Holland, MI, 49424-9337
> www.raoset.com
> japruim@raoset.com
>
>
>
>

Rather than a counter, you could use foreach:

foreach ( $RoutePieces as $key => $value )
{
$blah = $value * $PieceWeight;
}

Make sense?

--
-Dan Joseph

www.canishosting.com - Plans start @ $1.99/month. Reseller plans and
Dedicated servers available.

"Build a man a fire, and he will be warm for the rest of the day.
Light a man on fire, and will be warm for the rest of his life."

  Réponse avec citation
Vieux 09/05/2008, 18h21   #3
tedd
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Why doesn't working with array's ever stick in my thickhead?

At 12:56 PM -0400 5/9/08, Jason Pruim wrote:
>Hi Everyone,
>
>SO it's friday, I'm tired, and I can't seem to think straight... I
>am attempting to do some very basic math with some arrays... Here's
>the pseudo code that I'm working with:
>
>NumberOfPieces * PieceWeight = TotalWeight
>
>explode total weight to get tenth's of pounds, then divide tenth's
>of pounds by 16 to grab ounces.


What??

Why do you want tenth's of pounds? Just divide pounds by 16 and
you'll get ounces.

---

>Display TotalWeight # Exploded Ounces.
>
>Which I have working just fine for a single set of boxes...
>
>My problem is I want to be able to automatically add say 50 boxes in
>a form to process the weight of each route...


How much does each box weight and how many boxes? That's all you need.

---

>So what I need to do is probably use some kind of a counter to loop
>through my $RoutePieces array and multiply by the $PieceWeight?
>$PieceWeight is a static number.


Is $PieceWeight is a static number?

The last time I checked, the cost per oz for shipping depends upon
the weight. The more weight, the less the cost per ounce. Is that not
true?

---

As for the code, that's pretty simple.

Cheers,

tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
  Réponse avec citation
Vieux 09/05/2008, 18h22   #4
Jason Pruim
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Why doesn't working with array's ever stick in my thick head?


On May 9, 2008, at 1:05 PM, Dan Joseph wrote:

> On Fri, May 9, 2008 at 12:56 PM, Jason Pruim <japruim@raoset.com>
> wrote:
>
>> Hi Everyone,
>>
>> SO it's friday, I'm tired, and I can't seem to think straight... I am
>> attempting to do some very basic math with some arrays... Here's
>> the pseudo
>> code that I'm working with:
>>
>> NumberOfPieces * PieceWeight = TotalWeight
>>
>> explode total weight to get tenth's of pounds, then divide tenth's of
>> pounds by 16 to grab ounces.
>>
>> Display TotalWeight # Exploded Ounces.
>>
>> Which I have working just fine for a single set of boxes...
>>
>> My problem is I want to be able to automatically add say 50 boxes
>> in a form
>> to process the weight of each route...
>>
>> So what I need to do is probably use some kind of a counter to loop
>> through
>> my $RoutePieces array and multiply by the $PieceWeight?
>> $PieceWeight is a
>> static number.
>>
>> Now let me show you some abbreviated code to try and explain what
>> I'm using
>> right now:
>> <?PHP
>> $i= "0";
>> $num= "2";
>> $PieceWeight = $_POST['txtPieceWeight'];
>> $RoutePieces[] = $_POST['txtRoutePieces'];
>> $RouteNumber[] = $_POST['txtRoute'];
>>
>> $totalWeight = $PieceWeight * $RoutePieces/16;
>> $weightExplode = explode('.', $totalWeight);
>> //$weightOunces = ((float)('.' . $weightExplode[1])) * 16;
>> // Use stut's method... Seems cleaner
>> $explodeOunces = ($totalWeight - intval($totalWeight)) * 16;
>>
>> while($i <= $num){
>> echo <<<HTML
>>
>> <p>
>> Route Number:<input type="text" name="txtRoute[$i]" size="4"
>> value
>> ="{$RouteNumber[$i]}"> Number of pieces: <input type="text"
>> name="txtRoutePieces[$i]" size="4"
>> value="{$RoutePieces[$i]}"><label>Total
>> weight of route: {$weightExplode[0]} # {$explodeOunces} Ounces</
>> label>
>> </p>
>>
>>
>> HTML;
>>
>> $i++;
>> echo $i;
>> }
>> ?>
>>
>> the $_POST array has the proper values stored properly... I just
>> can't seem
>> to figure out how to work with the values... Any ideas? Or slaps on
>> the back
>> of the head to wake me up a little?
>>
>> I need :P
>>
>>
>>
>> --
>>
>> Jason Pruim
>> Raoset Inc.
>> Technology Manager
>> MQC Specialist
>> 3251 132nd ave
>> Holland, MI, 49424-9337
>> www.raoset.com
>> japruim@raoset.com
>>
>>
>>
>>

> Rather than a counter, you could use foreach:
>
> foreach ( $RoutePieces as $key => $value )
> {
> $blah = $value * $PieceWeight;
> }
>
> Make sense?


The idea make sense, and I think it will work. But I'm getting this
error:

[Fri May 9 13:19:33 2008] [error] PHP Fatal error: Unsupported
operand types in /Volumes/RAIDer/webserver/Documents/dev/weightcalc/
index.php on line 22


foreach ($RoutePieces as $key => $value){
$testWeight = $value * $PieceWeight; <-------Line 22
}

Some quick trying tells me that the problem is my "*" I don't remember
having this problem before. Is there another character I should use to
multiply with?

Thanks for looking!



--

Jason Pruim
Raoset Inc.
Technology Manager
MQC Specialist
3251 132nd ave
Holland, MI, 49424-9337
www.raoset.com
japruim@raoset.com



  Réponse avec citation
Vieux 09/05/2008, 18h29   #5
Jason Pruim
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Why doesn't working with array's ever stick in my thick head?

Hey tedd


On May 9, 2008, at 1:21 PM, tedd wrote:

> At 12:56 PM -0400 5/9/08, Jason Pruim wrote:
>> Hi Everyone,
>>
>> SO it's friday, I'm tired, and I can't seem to think straight... I
>> am attempting to do some very basic math with some arrays... Here's
>> the pseudo code that I'm working with:
>>
>> NumberOfPieces * PieceWeight = TotalWeight
>>
>> explode total weight to get tenth's of pounds, then divide tenth's
>> of pounds by 16 to grab ounces.

>
> What??
>
> Why do you want tenth's of pounds? Just divide pounds by 16 and
> you'll get ounces.


It's actually for a weight calculator that we use for some of our
mailings. when you take .226 and multiply that by 464 you get 104.864
ounces.

I need to be able to display that as: 6 # 13.824 Ounces.
>
>
> ---
>
>> Display TotalWeight # Exploded Ounces.
>>
>> Which I have working just fine for a single set of boxes...
>>
>> My problem is I want to be able to automatically add say 50 boxes
>> in a form to process the weight of each route...

>
> How much does each box weight and how many boxes? That's all you need.


the routes can have anywhere from 1 piece to 800+ pieces. and we can
have as few as 1 route to as many as 50 or 60+


>
>
> ---
>
>> So what I need to do is probably use some kind of a counter to loop
>> through my $RoutePieces array and multiply by the $PieceWeight?
>> $PieceWeight is a static number.

>
> Is $PieceWeight is a static number?
>
> The last time I checked, the cost per oz for shipping depends upon
> the weight. The more weight, the less the cost per ounce. Is that
> not true?


Normally yes, but not in this case I'm using it so I don't have to
sit there and count out 464 postcards. I can just throw them on my
scale and get the total weight.

>
>
> ---
>
> As for the code, that's pretty simple.


Then it's a simple "It's a friday thing" for me... My wife keeps
rubbing in that today is her last day of work until mid august... :P


--

Jason Pruim
Raoset Inc.
Technology Manager
MQC Specialist
3251 132nd ave
Holland, MI, 49424-9337
www.raoset.com
japruim@raoset.com



  Réponse avec citation
Vieux 09/05/2008, 18h38   #6
tedd
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Why doesn't working with array's ever stick in my thickhead?

At 1:29 PM -0400 5/9/08, Jason Pruim wrote:
>Hey tedd
>
>On May 9, 2008, at 1:21 PM, tedd wrote:
>
>>At 12:56 PM -0400 5/9/08, Jason Pruim wrote:
>>>Hi Everyone,
>>>
>>>SO it's friday, I'm tired, and I can't seem to think straight... I
>>>am attempting to do some very basic math with some arrays...
>>>Here's the pseudo code that I'm working with:
>>>
>>>NumberOfPieces * PieceWeight = TotalWeight
>>>
>>>explode total weight to get tenth's of pounds, then divide tenth's
>>>of pounds by 16 to grab ounces.

>>
>>What??
>>
>>Why do you want tenth's of pounds? Just divide pounds by 16 and
>>you'll get ounces.

>
>It's actually for a weight calculator that we use for some of our
>mailings. when you take .226 and multiply that by 464 you get
>104.864 ounces.
>
>I need to be able to display that as: 6 # 13.824 Ounces.


Well, that explains it. Now, it's Friday for me.

You know, if you take the total weight and divide that by the number
of gorillas per lamp-pole you'll go back in time. I'm totally lost.

Sorry I couldn't be more .

Cheers,

tedd


--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
  Réponse avec citation
Vieux 09/05/2008, 21h20   #7
Roberto Mansfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Why doesn't working with array's ever stick in my thickhead?

Jason Pruim wrote:
>> Why do you want tenth's of pounds? Just divide pounds by 16 and you'll
>> get ounces.

>
> It's actually for a weight calculator that we use for some of our
> mailings. when you take .226 and multiply that by 464 you get 104.864
> ounces.
>
> I need to be able to display that as: 6 # 13.824 Ounces.


Then I think you want to use the mod operator:

(I'm assuming .226 is the cost per ounce and 464 is the total number of
ounces)

$pounds = intval( .226 * 464 / 16 );
$ounces = ( .226 * 464 ) % 16;


Roberto
  Réponse avec citation
Vieux 09/05/2008, 21h23   #8
Roberto Mansfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Why doesn't working with array's ever stick in my thickhead?

Roberto Mansfield wrote:

> (I'm assuming .226 is the cost per ounce and 464 is the total number of
> ounces)


It sure is Friday. This assumption doesn't make any sense!
  Réponse avec citation
Vieux 09/05/2008, 21h29   #9
Jason Pruim
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Why doesn't working with array's ever stick in my thickhead?


On May 9, 2008, at 4:23 PM, Roberto Mansfield wrote:

> Roberto Mansfield wrote:
>
>> (I'm assuming .226 is the cost per ounce and 464 is the total
>> number of
>> ounces)

>
> It sure is Friday. This assumption doesn't make any sense!


Yeah... and my work day is almost done! SO Happy Friday to you all!

Basically though, .226 is the weight of a single piece. we get it by
weighing 100 pieces, and then dividing the total weight by 100. that
gives us the weight of a single piece in a tenth of a pound. And I
need to display in pounds and ounces. I have that part figured out, I
just need to fight with it so I can do it without having to type out
in the code 50 fields to add the routes together


--

Jason Pruim
Raoset Inc.
Technology Manager
MQC Specialist
3251 132nd ave
Holland, MI, 49424-9337
www.raoset.com
japruim@raoset.com



  Réponse avec citation
Vieux 13/05/2008, 16h58   #10
Jason Pruim
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Why doesn't working with array's ever stick in my thick head?

Hi Everyone,

I am taking another stab at this problem, complete with code and an
example...

Here is the issue:

I need to be able to display pounds and ounces of a given number of
pieces, the code I have does this just fine for a single route... But
what I can't seem to get through my head is how to do it for a
variable number of routes?

Here is the code I'm working with...

<?PHP
//Rural Route Weight Calculator by Jason Pruim 2008
$c = "0";
$num= "1";
//$num = $_POST['txtNum'];
$PieceWeight = $_POST['txtPieceWeight'];
$RoutePieces = $_POST['txtRoutePieces'];
$RouteNumber = $_POST['txtRoute'];


//self submitting forms
$self = $_SERVER['PHP_SELF'];
$totalWeight = ($PieceWeight * $RoutePieces) /16;
$weightExplode = explode('.', $totalWeight);
$weightOunces = ((float)('.' . $weightExplode[0])) * 16;
// Use stut's method... Seems cleaner
$explodeOunces = ($totalWeight - intval($totalWeight)) * 16;
if($_POST) {
echo <<<HTML
<div class="weightbox">
<h1>Weight calculator</h1>
<form method ='post' action='{$self}'>
<p>Weight of a single piece:<input type="text" name="txtPieceWeight"
size="5" value="{$PieceWeight}"></p>

HTML;

while ($c != $num) {
echo <<<HTML

Route Number:<input type="text" name="txtRoute" size="5"
value="{$RouteNumber}"> Number of pieces: <input type="text"
name="txtRoutePieces" value="{$RoutePieces}"size="5"><label>Total
weight of route: {$weightExplode[0]}# {$explodeOunces} Ounces</
label><BR>
HTML;




$c++;

}
echo "<BR><input type='submit' value='Calculate'></form></div>";
//DEBUG
dump_debug($RoutePieces);
dump_debug($RouteNumber);
dump_debug($_POST);
print_debug($explodeOunces);
dump_debug($weightExplode);
//END DEBUG


}else{
echo "<form method='post' action='{$self}'>";
echo "Number of routes: <input type='text' name='txtNum' size='2'><BR>
<input type='submit' value='GO'>";
echo "</form>";
}



?>

The website is: HTTP://www.raoset.com/dev/weightcalc/

I need ! My brain is going to die tonight without it




--

Jason Pruim
Raoset Inc.
Technology Manager
MQC Specialist
3251 132nd ave
Holland, MI, 49424-9337
www.raoset.com
japruim@raoset.com



  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 22h57.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,23732 seconds with 18 queries