Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use multidimensional session arrays:</p> <pre><code>$_SESSION['wz_saving_b'][$_POST['wz_saving_b']] = $_POST['wz_saving_b']; </code></pre> <p>Or, just use [] to add new keys, but you are going to have duplicated values</p> <pre><code>$_SESSION['wz_saving_b'][] = $_POST['wz_saving_b']; </code></pre> <p>Let's say the user types in wz_saving_b the following things:</p> <p><code>1</code>, <code>2</code>, <code>3</code></p> <pre><code>&lt;?php session_start(); ?&gt; &lt;form method="POST" action=""&gt; &lt;label&gt;A:&lt;/label&gt; &lt;input name="wz_saving_a" type="text" /&gt; &lt;label&gt;B:&lt;/label&gt; &lt;input name="wz_saving_b" type="text" /&gt; &lt;input name="wz_submit_saving_1" type="submit" class="add_button" value="Add" /&gt; &lt;/form&gt; &lt;?php if(isset($_POST['wz_submit_saving_1'])): $_SESSION['wz_saving_b'][$_POST['wz_saving_b']] = $_POST['wz_saving_b']; ?&gt; &lt;div id="wz_config_1" class="wz_config"&gt; &lt;ul&gt; &lt;li&gt;Your dimensions:&lt;/li&gt; &lt;li&gt;B: &lt;?php if(isset($_SESSION['wz_saving_b'])): foreach($_SESSION['wz_saving_b'] as $k =&gt; $v) { echo "$v "; } endif; ?&gt; mm&lt;/li&gt; &lt;/ul&gt; &lt;?php endif; ?&gt; &lt;?php var_dump($_SESSION); ?&gt; </code></pre> <p>Output:</p> <pre><code> Your dimensions: B: 1 2 3 mm array (size=1) 'wz_saving_b' =&gt; array (size=3) 1 =&gt; string '1' (length=1) 2 =&gt; string '2' (length=1) 3 =&gt; string '3' (length=1) </code></pre> <hr> <p><strong>The requested abstraction:</strong></p> <pre><code>&lt;?php session_start(); ?&gt; &lt;form method="POST" action=""&gt; &lt;label&gt;A:&lt;/label&gt; &lt;input name="wz_saving_a" type="text" /&gt; &lt;label&gt;B:&lt;/label&gt; &lt;input name="wz_saving_b" type="text" /&gt; &lt;label&gt;C:&lt;/label&gt; &lt;input name="wz_saving_c" type="text" /&gt; &lt;label&gt;D:&lt;/label&gt; &lt;input name="wz_saving_d" type="text" /&gt; &lt;input name="wz_submit_saving_1" type="submit" class="add_button" value="Add" /&gt; &lt;/form&gt; &lt;?php if(isset($_POST['wz_submit_saving_1'])) { foreach($_POST as $key =&gt; $value) { if($key != 'wz_submit_saving_1') { $_SESSION[$key][] = $value; } } } ?&gt; &lt;div id="wz_config_1" class="wz_config"&gt; &lt;ul&gt; &lt;li&gt;Your dimensions:&lt;/li&gt; &lt;?php foreach($_SESSION as $k =&gt; $v): ?&gt; &lt;?php foreach($v as $saving =&gt; $wz): ?&gt; &lt;li&gt;&lt;?= strtoupper(substr($k, 10));?&gt; : &lt;?=$wz;?&gt; mm&lt;/li&gt; &lt;?php endforeach; ?&gt; &lt;?php endforeach; ?&gt; &lt;/ul&gt; &lt;?php var_dump($_SESSION); ?&gt; </code></pre> <p>I did some random inputs here with some numbers. The output is:</p> <pre><code> Your dimensions: A : 1 mm A : 6 mm A : 5 mm B : 1 mm B : 6 mm B : 5 mm C : 4 mm C : 8 mm C : 5 mm D : 4 mm D : 7 mm D : 5 mm array (size=4) 'wz_saving_a' =&gt; array (size=3) 0 =&gt; string '1' (length=1) 1 =&gt; string '6' (length=1) 2 =&gt; string '5' (length=1) 'wz_saving_b' =&gt; array (size=3) 0 =&gt; string '1' (length=1) 1 =&gt; string '6' (length=1) 2 =&gt; string '5' (length=1) 'wz_saving_c' =&gt; array (size=3) 0 =&gt; string '4' (length=1) 1 =&gt; string '8' (length=1) 2 =&gt; string '5' (length=1) 'wz_saving_d' =&gt; array (size=3) 0 =&gt; string '4' (length=1) 1 =&gt; string '7' (length=1) 2 =&gt; string '5' (length=1) </code></pre> <hr> <p>Based on the provided code, this should work, I tried to input one time <code>1</code> and <code>2</code> mm on position <code>x</code>, and then <code>4</code> and <code>5</code> mm on position <code>y</code>, the output was:</p> <pre><code>Rechte sparing 1 Formaat van de sparing: A: 1 mm B: 2 mm Positionering van de sparing: x Rechte sparing 1 Formaat van de sparing: A: 4 mm B: 5 mm Positionering van de sparing: y </code></pre> <p>Code:</p> <pre><code>&lt;?php if(isset($_POST['wz_submit_saving_1'])) : $straight_saving = array( 'wz_str_saving' =&gt; $_POST['wz_str_saving'], 'wz_saving_a' =&gt; $_POST['wz_saving_a'], 'wz_saving_b' =&gt; $_POST['wz_saving_b'], 'wz_submit_saving_1' =&gt; $_POST['wz_submit_saving_1'] ); $_SESSION['straight_saving'][] = $straight_saving; endif; if(isset($_SESSION['straight_saving'])) : foreach($_SESSION['straight_saving'] as $sav) { ?&gt; &lt;div class="wz_config"&gt; &lt;h3&gt;Rechte sparing 1&lt;/h3&gt; &lt;ul&gt; &lt;li&gt; &lt;ul&gt; &lt;li&gt;Formaat van de sparing:&lt;/li&gt; &lt;li&gt;A: &lt;?php echo $sav['wz_saving_a']; ?&gt; mm&lt;/li&gt; &lt;li&gt;B: &lt;?php echo $sav['wz_saving_b']; ?&gt; mm&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li&gt; &lt;ul&gt; &lt;li&gt;Positionering van de sparing:&lt;/li&gt; &lt;li&gt;&lt;?php echo $sav['wz_str_saving']; ?&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;/ul&gt; &lt;div class="clear"&gt;&lt;/div&gt; &lt;/div&gt;&lt;!--End wz_config_1--&gt; &lt;?php } endif; ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload