I am trying to find the proper way to turn this...

$totalqty = 0;
$totalqty = $tireqty + $oilqty + $sparkqty + $coolqty + $wiperqty;
echo 'Items ordered: '.$totalqty.'<br />';

if( $totalqty == 0)
{
echo 'You did not order anything on the previous page!<br />';
}
else
{
if ( $tireqty>0 )
echo $tireqty.' tires<br />';
if ( $oilqty>0 )
echo $oilqty.' bottles of oil<br />';
if ( $sparkqty>0 )
echo $sparkqty.' spark plugs<br />';
if ( $coolqty>0 )
echo $coolqty.' coolant<br />';
if ($wiperqty>0 )
echo $wiperqty.' wiper blades<br />';
}

Into a FOREACH array.

Does anyone have a stupid-simple example for a newbie?

    How about this?

    $quants = array('tireqty' => 'tires', 'oilqty' => 'bottles of oil', 'etc' => 'etc');
    foreach($quants as $variable => $label) if($$variable > 0) echo $$variable." $label<br />";

    Not tested, but should work.

      I just tested that, it appears to have worked perfectly. I am really struggling to understand arrays at the moment... I am hoping that is easier to write a program using arrays from scratch rather than modify existing code as this assignment required me to do.

        What would be the syntax to add the "else echo 'You didn't oder anything!' if $$variable is < 0?

          Array is just a fancy name for list. For example, in the array I made, you had a list of items with labels and values.

          1. "tires" with the label "tireqty"
          2. "bottles of oil" with the label "oilqty"

          Then the foreach loop just goes through each one of the lists, separating them into two variables - the label and the value it holds. The trick comes when I use "$$variable". That's a special notation, it means to look at the value of $variable, and then take the value of the variable with THAT name. That's a complicated sentence, so here's what I mean.

          Let's say $variable = "tireqty". $$variable would then look at the value of $variable (which is "tireqty") and then get the value of $tireqty. The foreach loop just does that for all of the labels in the array.

          I hope that all makes sense.

            joviyach wrote:

            What would be the syntax to add the "else echo 'You didn't oder anything!' if $$variable is < 0?

            $quants = array('tireqty' => 'tires', 'oilqty' => 'bottles of oil', 'etc' => 'etc');
            
            $totalqty = 0;
            foreach($quants as $variable => $label) 
            {
            	if($$variable > 0) echo $$variable." $label<br />";
            	$totalqty += $$variable;
            }
            
            if($totalqty == 0) echo 'You did not order anything on the previous page!<br />';

            Once again, not tested, but should work.

              I think it's making a little more sense now. I think I need better textbooks. :-)

                btw... that worked as expected.

                Are there any array tutorials for complete morons out there? I really need to learn this stuff.

                  Not sure. I always learn from books, and I recommend that everyone else does too. Arrays are a pretty universal topic, though. They're in just about every programming language you'll find. I don't know of any good tutorials off-hand, I'd recommend trying Google.

                    Write a Reply...