Home > Blockchain >  php for loop in post
php for loop in post

Time:04-04

I was making a single post in my php code. I wanted to use php for. Didn't work for multi post. Why didn't it work, I couldn't find where is the error..

<?php
   $e = $_POST['e'];
   $b = $_POST['b'];
   $y= $_POST['y'];
   $a = $_POST['a'];

   $e1 = $_POST['e1'];
   $b1 = $_POST['b1'];
   $y1= $_POST['y1'];
   $a1 = $_POST['a1'];

for ($i=1; $i < 10 ; $i  ) { 
if (!empty($a.$i)) {
  
    $package.$i['Description'] = 'nailce';
    $packaging.$i['Code'] = '02';
    $packaging.$i['Description'] = '';
    $package.$i['Packaging'] = $packaging.$i;
    $unit.$i['Code'] = 'CM';
    $unit.$i['Description'] = 'Centimeter';
    $dimensions.$i['UnitOfMeasurement'] = $unit.$i;
    $dimensions.$i['Length'] = $e.$i;
    $dimensions.$i['Width'] = $b.$i;
    $dimensions.$i['Height'] = $y.$i;
    $package.$i['Dimensions'] = $dimensions.$i;
    $unit22['Code'] = 'KGS';
    $unit22['Description'] = 'Kg';
    $packageweight.$i['UnitOfMeasurement'] = $unit22;
    $packageweight.$i['Weight'] = $a.$i;
    $package.$i['PackageWeight'] = $packageweight.$i;
    
    $shipment['Package'][$i] = $package.$i;
}

}

?>

CodePudding user response:

I think that you have used a wrong way to name variable variables. You can use the following code:

<?php
   $e = $_POST['e'];
   $b = $_POST['b'];
   $y= $_POST['y'];
   $a = $_POST['a'];

   $e1 = $_POST['e1'];
   $b1 = $_POST['b1'];
   $y1= $_POST['y1'];
   $a1 = $_POST['a1'];

for ($i=1; $i < 10 ; $i  ) { 

$aVarName = 'a' . $i;

if (!empty($$aVarName)) {
    $package = 'package' . $i;
    $packaging = 'packaging' . $i;
    $unit = 'unit' . $i;
    $dimensions = 'dimensions' . $i;
    $packageweight= 'packageweight' . $i;
    $bVarName = 'b' . $i;
    $eVarName = 'e' . $i;
    $yVarName = 'y' . $i;

    $$package['Description'] = 'nailce';
    $$packaging['Code'] = '02';
    $$packaging['Description'] = '';
    $$package['Packaging'] = $$packaging;
    $$unit['Code'] = 'CM';
    $$unit['Description'] = 'Centimeter';
    $$dimensions['UnitOfMeasurement'] = $$unit;
    $$dimensions['Length'] = $$eVarName;
    $$dimensions['Width'] = $$bVarName;
    $$dimensions['Height'] = $$yVarName;
    $$package['Dimensions'] = $$dimensions;
    $unit22['Code'] = 'KGS';
    $unit22['Description'] = 'Kg';
    $$packageweight['UnitOfMeasurement'] = $unit22;
    $$packageweight['Weight'] = $$aVarName;
    $$package['PackageWeight'] = $$packageweight;
    
    $shipment['Package'][$i] = $$package;
}

}
  • Related