Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to compile an SQL query out of this complicated array system
    primarykey
    data
    text
    <p>At first glance, I had thought this would be pretty simple, just construct an sql query by imploding the arrays, but i just cant wrap my mind around how to get to that point. Basically i have a program that breaks down user input into a way that is needed for my table, and now i need to get that data into an SQL command, you can see how i am attempting to do it, needless to say im at my wits end.</p> <p>HTML:</p> <pre><code>&lt;form action="go.php" method="post"&gt; &lt;h1&gt;Enter port data to view how your table will look&lt;/h1&gt; &lt;label for="ports"&gt;Number of Ports:&lt;/label&gt;&lt;input type="text" id="ports" name="ports" size="30"&gt;&lt;br&gt; &lt;label for="super"&gt;Super Columns:&lt;/label&gt;&lt;input type="text" id="super" name="super" size="30"&gt;&lt;br&gt; &lt;label for="columns"&gt;Columns:&lt;/label&gt;&lt;input type="text" id="columns" name="columns" size="30"&gt;&lt;br&gt; &lt;label for="rows"&gt;Rows:&lt;/label&gt;&lt;input type="text" id="rows" name="rows" size="30"&gt;&lt;br&gt; </code></pre> <p><br> </p> <p>PHP:</p> <pre><code>//post vars $port = (int)$_POST['ports']; $super = (int)$_POST['super']; $col = (int)$_POST['columns']; $row = (int)$_POST['rows']; //rest of vars $halfPort=$port/2;//12 $colHalf = $col / $super;//6 $half=$colHalf-1;//5 $i=1; $count=1; $and=1; $one=1; $s=1; echo "&lt;h1&gt;here goes nothen&lt;/h1&gt;&lt;br&gt;&lt;br&gt;"; //sperate port $finalArray = array(); for ($i = $port; $i &gt;= 1; $i -= $colHalf) { $tempArray = array(); for ($j = 0; $j &lt; $colHalf; $j++) { $tempArray[] = $i - $j; } $tempArray[]= sort($tempArray); $finalArray[] = implode(",", $tempArray); } $finalArray = array_reverse($finalArray); echo "&lt;pre&gt;" . print_r($finalArray, true) . "&lt;/pre&gt;"; echo "&lt;br&gt;&lt;br&gt;"; //sql for insert $sqlinsert=' INSERT INTO '.$tbl_name2.' (PortNumber) VALUES '; $start='('; $end=')'; $c=1; $b=0; //while to finish SQL $queryStart=array(); $queryEnd=array(); while($c&lt;$half){ echo "&lt;br&gt;c:".$c."&lt;br&gt;"; $queryStart[]=explode(',',$finalArray[$b]); echo "&lt;pre&gt;" . print_r($queryStart, true) . "&lt;/pre&gt;"; $queryEnd[]= $c+=$and; $b+=$and; unset($queryStart); } </code></pre> <p>This is the ouptut of my first print:</p> <pre><code>Array ( [0] =&gt; 1,2,3,4,5,6,1 [1] =&gt; 7,8,9,10,11,12,1 [2] =&gt; 13,14,15,16,17,18,1 [3] =&gt; 19,20,21,22,23,24,1 ) </code></pre> <p>And the second:</p> <pre><code>c:1 Array ( [0] =&gt; Array ( [0] =&gt; 1 [1] =&gt; 2 [2] =&gt; 3 [3] =&gt; 4 [4] =&gt; 5 [5] =&gt; 6 [6] =&gt; 1 ) ) c:2 Array ( [0] =&gt; Array ( [0] =&gt; 7 [1] =&gt; 8 [2] =&gt; 9 [3] =&gt; 10 [4] =&gt; 11 [5] =&gt; 12 [6] =&gt; 1 ) ) c:3 Array ( [0] =&gt; Array ( [0] =&gt; 13 [1] =&gt; 14 [2] =&gt; 15 [3] =&gt; 16 [4] =&gt; 17 [5] =&gt; 18 [6] =&gt; 1 ) ) c:4 Array ( [0] =&gt; Array ( [0] =&gt; 19 [1] =&gt; 20 [2] =&gt; 21 [3] =&gt; 22 [4] =&gt; 23 [5] =&gt; 24 [6] =&gt; 1 ) ) </code></pre> <p>I feel like I am very close, just cant bring myself to slap on the last little bit i suppose. Any ideas?</p> <p>Also as a side note, ignore that trailing 1 at the end of the arrays, it seems to have come from running <code>sort()</code></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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