Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First I will take the freedom to change $row1, $row2... for the <a href="http://php.net/manual/en/language.types.array.php" rel="nofollow">array</a> $group[1], $group[2]... not to confuse them with the actual rows. They are not rows, they are actually groups of columns.</p> <p>First, the query. I wouldn't do it in that way. You have clearly a progression, so why not to take advantage of this?</p> <pre><code>//First, a couple of definitions. $group=array(); $default=0; //If there are no results, the default is 0 $k=0; // The following query looks like this: SELECT AVG(fb1), [...], AVG(fb50) FROM `table` $queryA="SELECT "; for ($i=1; $i&lt;50; $i++) $queryA.="AVG(fb".$i."), " $queryA.="AVG(fb50) FROM `table`"; $resultA=mysql_query($queryA); //I don't know all the other table's names, so I assume table2, table3 etc. //EDIT THIS. Add the code in brackets as many times as tables (with the respective names $queryC, $queryD... and the $resultC, $resultD...): { $queryB="SELECT "; for ($i=1; $i&lt;50; $i++) $queryB.="AVG(fb".$i."), " $queryB.="AVG(fb50) FROM `table2'`"; $resultB=mysql_query($queryB); } $num=0; //EDIT THIS. Add as many as needed. Gets which result is longer. if (mysql_num_rows($resultA)&gt;=$num) $num=mysql_num_rows($resultA); if (mysql_num_rows($resultB)&gt;=$num) $num=mysql_num_rows($resultB); //Then, the while loop: for($a=0; $a&lt;$num; $a++) { //EDIT THIS. Add as many as needed $rowA = mysql_fetch_array($resultA) $rowB = mysql_fetch_array($resultB); // Repeat it 50 times for ($j=0; j&lt;50;j++) { //EDIT THIS. Add as many as tables suming up. $group[$k]+=$rowA["AVG(fb".$j.")"]+$rowB["AVG(fb".$j.")"]; //EDIT THIS. Get the number to divide for the average if (!empty($rowA["AVG(fb".$j.")"])) $group["n".$k]+=1; if (!empty($rowB["AVG(fb".$j.")"])) $group["n".$k]+=1; //Checks if $j is 4, 9, 14, 19 etc (groups of 5 as 0 is included) if ($j%5==4) { $k++; } } for ($i=1; $i&lt;=5; $i++) { $group=(float) $group[$i] $div=(float) $group["n".$i]; if ($div!=0) { $group[$i]=$group/$div; } else $group[$i]=$default; echo "&lt;br&gt;Group ".$i.": ".$group[$i]; } </code></pre> <p>As you can see, <strong>it won't work with copy/paste</strong>. I assume that you enter manually all data in few different places. ONLY edit the lines just below where the comments say <strong>'EDIT THIS'</strong>. The rest of comments are explanations and require no further work. I haven't tested it but I believe it'll work if you input manually all the tablenames and require fields I detailed.</p> <p>Thought I have to tell you, this is not a 'good' practice. It'd be much better to put all the data in one table <code>values</code> adding a field called <code>creator_id</code> and to create another table called <code>creators</code> with the same <code>creator_id</code> field and a <code>name</code> field. Then you use <code>WHERE creator_id=$whatever</code> for seeing individual surveys.</p>
    singulars
    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. VO
      singulars
      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