About the calculation process

Hi,
I need to take action on the values shown in the picture below, but I couldn’t.
Şaft => Shaft, Cable space in the building
Fiber Optik => Fiber Optic Points Per Shaft

A Block uses 2 shafts, 4 optical points are requested per shaft
Total optical points of the A block 4x2 = 8 optical points

B Block uses 1 shafts, 4 optical points are requested per shaft
Total optical points of the 1 B block 4x1 = 4 optical points

I Block uses 2 shafts, 3 optical points are requested per shaft
Total optical points of the I block 3x2 = 6 optical points

How should I calculate the total number of optical points for all blocks?
Seems simple but I just couldn’t get the job done :grinning: :grinning: :grinning:

Keys are the order of blocks

[number_of_shafts_in_the_block] => Array
    (
        [1] => 2
        [2] => 1
        [3] => 2
        [4] => 2
        [5] => 1
        [6] => 2
        [7] => 2
        [8] => 1
        [9] => 2
        [10] => 2
    )
[number_of_optics_per_shaft] => Array
    (
        [1] => 4
        [2] => 4
        [3] => 2
        [4] => 4
        [5] => 4
        [6] => 1
        [7] => 4
        [8] => 4
        [9] => 3
        [10] => 4
    )

This would be a fairly straight-forward loop (from 1 to 10), a multiplication of corresponding values from each array, and a sum to get the total. It would take seeing your code to be able to help with what is wrong with it.

The following, while not using select/option menus for the choices, shows the logic involved -

<?php

// echo '<pre>'; print_r($_POST); echo '</pre>';
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
	// cast inputs to integers
	$_POST['number_of_shafts_in_the_block'] = array_map('intval',$_POST['number_of_shafts_in_the_block']);
	$_POST['number_of_optics_per_shaft'] = array_map('intval',$_POST['number_of_optics_per_shaft']);

	$product = []; // array to hold the product (shafts x optics) of each row
	
	foreach(range(1,10) as $i)
	{
		$product[$i] = $_POST['number_of_shafts_in_the_block'][$i] * $_POST['number_of_optics_per_shaft'][$i];
	}
	// examine the result
	echo '<pre>'; print_r($product); echo '</pre>';
	// to get the total
	echo array_sum($product);
}
?>

<form method='post'>
<table>
<tr><th>num shafts</th><th>num optics per shaft</th></tr>
<?php
foreach(range(1,10) as $i)
{
	// these can be integer inputs to submit the same values
	$shafts = $_POST['number_of_shafts_in_the_block'][$i] ?? 0;
	$optics = $_POST['number_of_optics_per_shaft'][$i] ?? 0;
	echo "<tr><td><input type='number' name='number_of_shafts_in_the_block[$i]' value='$shafts'></td>
		<td><input type='number' name='number_of_optics_per_shaft[$i]' value='$optics'></td></tr>";
}
?>
</table>
<input type='submit'>
</form>

First A block fixed
Other blocks are added with jquery via “Yeni Blok Ekle” => “Add New Block” button.
inputs and select options are as follows
I use this data as it is in other codes.
10 lines not standard, visitor can add more or less

<input type="text" name="number_of_shafts_in_the_block[1]" value="2">
<input type="text" name="number_of_shafts_in_the_block[2]" value="1">
<input type="text" name="number_of_shafts_in_the_block[3]" value="2">
<input type="text" name="number_of_shafts_in_the_block[4]" value="2">
<input type="text" name="number_of_shafts_in_the_block[5]" value="1">
<input type="text" name="number_of_shafts_in_the_block[6]" value="2">
<input type="text" name="number_of_shafts_in_the_block[7]" value="2">
<input type="text" name="number_of_shafts_in_the_block[8]" value="1">
<input type="text" name="number_of_shafts_in_the_block[9]" value="2">
<input type="text" name="number_of_shafts_in_the_block[10]" value="2">

<input type="text" name="number_of_optics_per_shaft[1]" value="4">
<input type="text" name="number_of_optics_per_shaft[2]" value="4">
<input type="text" name="number_of_optics_per_shaft[3]" value="2">
<input type="text" name="number_of_optics_per_shaft[4]" value="4">
<input type="text" name="number_of_optics_per_shaft[5]" value="4">
<input type="text" name="number_of_optics_per_shaft[6]" value="1">
<input type="text" name="number_of_optics_per_shaft[7]" value="4">
<input type="text" name="number_of_optics_per_shaft[8]" value="4">
<input type="text" name="number_of_optics_per_shaft[9]" value="3">
<input type="text" name="number_of_optics_per_shaft[10]" value="4">

I think this has been working
Let me try a little more
Do you think it makes sense?

  $total_number_of_optics = 0;

$number_of_shafts_in_the_block = json_decode('{"1":"1","2":"2","3":"1","4":"2","5":"1","6":"2","7":"1","8":"2","9":"1","10":"2"}', true);
$number_of_optics_per_shaft = json_decode('{"1":"4","2":"2","3":"4","4":"2","5":"4","6":"2","7":"4","8":"2","9":"4","10":"2"}', true);


   foreach ( $number_of_shafts_in_the_block AS $key1 => $value1 ){

    foreach ($number_of_optics_per_shaft AS $key2 => $value2){

      if($key1 == $key2){

      $total_number_of_optics += ($value1*$value2);

      }  
    }
   }
   
   echo $total_number_of_optics;

You are looping 100 times (10 shaft values X 10 optic values). Use the keys to directly access the corresponding elements in each array -

<?php

$total_number_of_optics = 0;

$shafts = json_decode('{"1":"1","2":"2","3":"1","4":"2","5":"1","6":"2","7":"1","8":"2","9":"1","10":"2"}', true);
$optics = json_decode('{"1":"4","2":"2","3":"4","4":"2","5":"4","6":"2","7":"4","8":"2","9":"4","10":"2"}', true);

foreach(array_keys($shafts) as $key)
{
	$total_number_of_optics += $shafts[$key]*$optics[$key];
}
  
echo $total_number_of_optics;
1 Like

Adding this if condition gave the correct result

if($key1 == $key2){

My Beginner job :grinning: :grinning:
Thank you for your skillful work

Sponsor our Newsletter | Privacy Policy | Terms of Service