Jump to content

SOLVED - DB, Arrays and counting.


silverbullet126

Recommended Posts

[code]Surely this can be done in your query. Something like...

[code]
<?php
for ($i = 1; $i == 7; $i++) {
    if ($result = mysql_query("SELECT COUNT(R6) AS R6_total WHERE R6 = '0$i'")) {
        $R6_totals[$i] = mysql_fetch_array($result);
    }
}
?>
[/code]

This should now give you an array ($R6_totals) containing all your totals. Needs some error checking and cleaning up but I hope this helps.[/code]
Link to comment
Share on other sites

Thanks, i just got it now ...

$R6_1=0;
$R6_2=0;
$R6_3=0;
$R6_4=0;
$R6_5=0;
$R6_6=0;
$R6_7=0;

$query2="SELECT R6 FROM bpm_survey_2";
$result2 = mysql_query($query2);
while ($row=mysql_fetch_array($result2, MYSQL_ASSOC)){

$array = spliti ("V", $row['R6']);
#print_r($array);

for ($i_R6 = 0; $i_R6 < count($array); ++$i_R6) {

if ($array[$i_R6]=="01") { $R6_1++; }
if ($array[$i_R6]=="02") { $R6_2++; }
if ($array[$i_R6]=="03") { $R6_3++; }
if ($array[$i_R6]=="04") { $R6_4++; }
if ($array[$i_R6]=="05") { $R6_5++; }
if ($array[$i_R6]=="06") { $R6_6++; }
if ($array[$i_R6]=="07") { $R6_7++; }

}

} # close while R6
Link to comment
Share on other sites

Try the following. It is a much easier way (fewer lines of code, too):
[code]<?php
$R6 = array_fill(1,7,0);
$q = "SELECT R6 FROM bpm_survey_2";
$rs = mysql_query($q);
while ($rw = mysql_fetch_assoc($rs)) {
    $arr = explode('V',$rw['R6']);
    foreach ($arr as $val)
          if ($val != '') $R6[(int)$val]++;
}
echo '<pre>' . print_r($R6,true) . '<pre>';
?>[/code]

Ken
Link to comment
Share on other sites

Thanks Ken,

--

[b]Got another problem [/b] now (keep digging myself holes :))

This one kinda kinda hard to explain, but here i go :)

I have strings coming like before, but this time each V## corresponds to one answer of the 5 part question (rate how you like each of the 5 shows listed), you can see some example of the form code below (the 5 V## are combined b4 being entered into the database).  

  1   2    3   4   5
V01V02V04V05V03
V04V04V04V05V01
V02V05V03V01V04
----
1 x V01
1 x V04
1 x V02

--------------

HOT 20 -

<select name="R14a" id="R14a">
  <option value="V05">5 - Favourite</option>
  <option value="V04">4 - Above Average</option>
  <option value="V03">3 - Average</option>
  <option value="V02">2 - Below Average</option>
  <option value="V01">1 - Least Favourite</option>
</select>

----------

Here is what i have so far, it's not working, but again I think I'm close:

$R14a_1=0;
$R14a_2=0;
$R14a_3=0;
$R14a_4=0;
$R14a_5=0;
$R14b_1=0;
$R14b_2=0;
$R14b_3=0;
$R14b_4=0;
$R14b_5=0;
$R14c_1=0;
$R14c_2=0;
$R14c_3=0;
$R14c_4=0;
$R14c_5=0;
$R14d_1=0;
$R14d_2=0;
$R14d_3=0;
$R14d_4=0;
$R14d_5=0;
$R14e_1=0;
$R14e_2=0;
$R14e_3=0;
$R14e_4=0;
$R14e_5=0;



$query2="SELECT R14 FROM bpm_survey_2";
$result2 = mysql_query($query2);
while ($row=mysql_fetch_array($result2, MYSQL_ASSOC)){

$array = spliti ("V", $row['R14']);
for ($i_R14 = 0; $i_R14 < count($array); ++$i_R14) {

if ($array[1]=="01") { $R14a_1++; }
if ($array[1]=="02") { $R14a_2++; }
if ($array[1]=="03") { $R14a_3++; }
if ($array[1]=="04") { $R14a_4++; }
if ($array[1]=="05") { $R14a_5++; }

if ($array[2]=="01") { $R14b_1++; }
if ($array[2]=="02") { $R14b_2++; }
if ($array[2]=="03") { $R14b_3++; }
if ($array[2]=="04") { $R14b_4++; }
if ($array[2]=="05") { $R14b_5++; }

if ($array[3]=="01") { $R14c_1++; }
if ($array[3]=="02") { $R14c_2++; }
if ($array[3]=="03") { $R14c_3++; }
if ($array[3]=="04") { $R14c_4++; }
if ($array[3]=="05") { $R14c_5++; }

if ($array[4]=="01") { $R14d_1++; }
if ($array[4]=="02") { $R14d_2++; }
if ($array[4]=="03") { $R14d_3++; }
if ($array[4]=="04") { $R14d_4++; }
if ($array[4]=="05") { $R14d_5++; }

if ($array[5]=="01") { $R14e_1++; }
if ($array[5]=="02") { $R14e_2++; }
if ($array[5]=="03") { $R14e_3++; }
if ($array[5]=="04") { $R14e_4++; }
if ($array[5]=="05") { $R14e_5++; }

}

} # close while R14

Link to comment
Share on other sites

Probably not the pretty way of getting it to work .. but it does now :)

$R21a_1=0;
$R21a_2=0;
$R21a_3=0;
$R21a_4=0;
$R21a_5=0;
$R21b_1=0;
$R21b_2=0;
$R21b_3=0;
$R21b_4=0;
$R21b_5=0;
$R21c_1=0;
$R21c_2=0;
$R21c_3=0;
$R21c_4=0;
$R21c_5=0;
$R21d_1=0;
$R21d_2=0;
$R21d_3=0;
$R21d_4=0;
$R21d_5=0;
$piece=3;


$query2="SELECT R21 FROM pn_survey_2";
$result2 = mysql_query($query2);
while ($row=mysql_fetch_array($result2, MYSQL_ASSOC)){

$piece1 = substr($row['R21'],0,$piece);
$piece2 = substr($row['R21'],3,$piece);
$piece3 = substr($row['R21'],6,$piece);
$piece4 = substr($row['R21'],9,$piece);
$piece5 = substr($row['R21'],12,$piece);

if ($piece1=="V01") { $R21a_1++; }
if ($piece1=="V02") { $R21a_2++; }
if ($piece1=="V03") { $R21a_3++; }
if ($piece1=="V04") { $R21a_4++; }
if ($piece1=="V05") { $R21a_5++; }

if ($piece2=="V01") { $R21b_1++; }
if ($piece2=="V02") { $R21b_2++; }
if ($piece2=="V03") { $R21b_3++; }
if ($piece2=="V04") { $R21b_4++; }
if ($piece2=="V05") { $R21b_5++; }

if ($piece3=="V01") { $R21c_1++; }
if ($piece3=="V02") { $R21c_2++; }
if ($piece3=="V03") { $R21c_3++; }
if ($piece3=="V04") { $R21c_4++; }
if ($piece3=="V05") { $R21c_5++; }

if ($piece4=="V01") { $R21d_1++; }
if ($piece4=="V02") { $R21d_2++; }
if ($piece4=="V03") { $R21d_3++; }
if ($piece4=="V04") { $R21d_4++; }
if ($piece4=="V05") { $R21d_5++; }

} # close while R21
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.