• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 184
  • Last Modified:

help shorten this code

how could i make this code short


$allrows = mysql_num_rows($alltable);
//as we are only interested in 1 and 2 which are yes and No respectively, we just set 1 and 2 of every column to count //from zer0
$i=0;
$B1_arr[1]=0;
$B1_arr[2]=0;
$B2_arr[1]=0;
$B2_arr[2]=0;
$B5_arr[1]=0;
$B5_arr[2]=0;
$B17_arr[1]=0;
$B17_arr[2]=0;
$B19_arr[1]=0;
$B19_arr[2]=0;
$B22_arr[1]=0;
$B22_arr[2]=0;
$C6_arr[1]=0;
$C6_arr[2]=0;
$C9_arr[1]=0;
$C9_arr[2]=0;
 
while ($i < $allrows)
{
$B1 = mysql_result($alltable, $i, "B1");
$B2 = mysql_result($alltable, $i, "B2");
$B5 = mysql_result($alltable, $i, "B5");
$B17 = mysql_result($alltable, $i, "B17");
$B19 = mysql_result($alltable, $i, "B19");
$B22 = mysql_result($alltable, $i, "B22");
$C6 = mysql_result($alltable, $i, "C6");
$C9 = mysql_result($alltable, $i, "C9");
 
 
 
 
if($B1==1){
$B1_arr[$B1]++;
}
if($B1==3){
$B1t_arr[$B1]++;
}
if($B1==0){
$B1N_arr[$B1]++;
}
if($B2==1){
$B2_arr[$B2]++;
}
if($B2==3){
$B2t_arr[$B2]++;
}
if($B5==1){
$B5_arr[$B5]++;
}
if($B5==3){
$B5t_arr[$B5]++;
}
if($B17==1){
$B17_arr[$B17]++;
}
if($B17==3){
$B17t_arr[$B17]++;
}
if($B19==1){
$B19_arr[$B19]++;
}
if($B19==3){
$B19t_arr[$B19]++;
}
if($B22==1){
 
$B22_arr[$B22]++;
}
if($B22==3){
$B22t_arr[$B22]++;
}
 
if($C6==1){
 
$C6_arr[$C6]++;
}
if($C6==3){
$C6t_arr[$C6]++;
}
 
if($C9==1){
 
$C9_arr[$C9]++;
}
if($C9==3){
$C9t_arr[$C9]++;
}
 
$i++;
 
 
 
}
//echo "</table><br>";
 
 
 
$B1trows = $allrows - $B1t_arr[3];
$B1pre = ($B1_arr[1] / $B1trows) * 100;
$B1 = number_format($B1pre, 2)." %"; 
 
 
$B2trows = $allrows - $B2t_arr[3];
$B2pre = ($B2_arr[1] / $B2trows) * 100;
$B2 = number_format($B2pre, 2)." %";
 
 
$B5trows = $allrows - $B5t_arr[3];
$B5pre = ($B5_arr[1] / $B5trows) * 100;
$B5 = number_format($B5pre, 2)." %";
 
$B17trows = $allrows - $B17t_arr[3];
$B17pre = ($B17_arr[1] / $B17trows) * 100;
$B17 = number_format($B17pre, 2)." %";
 
$B19trows = $allrows - $B19t_arr[3];
$B19pre = ($B19_arr[1] / $B19trows) * 100;
$B19 = number_format($B19pre, 2)." %";
 
$B22trows = $allrows - $B22t_arr[3];
$B22pre = ($B22_arr[1] / $B22trows) * 100;
$B22 = number_format($B22pre, 2)." %";
 
 
$C6trows = $allrows - $C6t_arr[3];
$C6pre = ($C6_arr[1] / $C6trows) * 100;
$C6 = number_format($C6pre, 2)." %";
 
$C9trows = $allrows - $C9t_arr[3];
$C9pre = ($C9_arr[1] / $C9trows) * 100;
$C9 = number_format($C9pre, 2)." %";

Open in new window

0
syedasimmeesaq
Asked:
syedasimmeesaq
  • 7
  • 3
  • 3
  • +1
1 Solution
 
Ray PaseurCommented:
syedasimmeesaq: Tell us what you're trying to do - I can't figure out your objective from the code here.  Depending on the objectives of the script, you might be able to use multi-dimensional arrays.  Thanks, ~Ray
0
 
syedasimmeesaqAuthor Commented:
ok I have these fields in db B1, B2, ....C6, C9....

In the first part I am setting there values to 0,

then in this part
if($B1==1){
$B1_arr[$B1]++;
}
if($B1==3){
$B1t_arr[$B1]++;
}
if($B1==0){
$B1N_arr[$B1]++;
}

I am adding it by one everytime a value is 1 , 0 or

now since I have around 100 fields I want to make it short so I don't have to write all those fields and formulas manually

Thanks
0
 
Steve BinkCommented:
So you're trying to get a count of how many '1's vs how many '0's?  What about the '3's?

Explain more about your goal, not your code.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
Ray PaseurCommented:
I'm sure we can condense the code by using functions, variable references ("$$" variables) and /or multidimensional arrays or objects.  But as we said, explain more about your goals - we don't need to see the code, just want to get an idea of the 100 inputs and expected outputs.  Thanks, ~Ray
0
 
syedasimmeesaqAuthor Commented:
yes I am trying to get a count of how many 1s, 2s, 3s and 0s are in a field. Then I after I get those below in the formula I am doing some calculations.
After I know how many 1s in B1 for example, I hold that in $B1_arr[1], so if there are five 1s in B1 field , $B1_arr[1] is 5. If there are three 0's in there, I will have $B1_arr[0] = 3

hope this makes sense.
0
 
syedasimmeesaqAuthor Commented:
so for starter, if you guys see this part

$B1_arr[1]=0;
$B1_arr[2]=0;
$B1_arr[0]=0;
$B1_arr[3]=0;
$B2_arr[0]=0;
$B2_arr[1]=0;
$B2_arr[2]=0;
$B2_arr[3]=0;
$B5_arr[0]=0;
$B5_arr[1]=0;
$B5_arr[2]=0;
$B5_arr[3]=0;
$B17_arr[0]=0;
$B17_arr[1]=0;
$B17_arr[2]=0;
$B17_arr[3]=0;
$B19_arr[0]=0;
$B19_arr[1]=0;
$B19_arr[2]=0;
$B19_arr[3]=0;
$B22_arr[0]=0;
$B22_arr[1]=0;
$B22_arr[2]=0;
$B22_arr[3]=0;
$C6_arr[0]=0;
$C6_arr[1]=0;
$C6_arr[2]=0;
$C6_arr[3]=0;
$C9_arr[0]=0;
$C9_arr[1]=0;
$C9_arr[2]=0;
$C9_arr[3]=0;

where I am setting all the values to zero, we can short this one so I don't have to manually type all the fields and so on
thanks
0
 
syedasimmeesaqAuthor Commented:
so for above, I think we can shorten it something like this
for ($y=0; $y<=3; $y++)
{
$B1_arr[$y]=0;

$B5_arr[$y]=0;


$B17_arr[$y]=0;

$B19_arr[$y]=0;

$B22_arr[$y]=0;

$C6_arr[$y]=0;

$C9_arr[$y]=0;
}
$y++;


but I am looking for the best way to approach this
0
 
Steve BinkCommented:
So you know you have a set number of fields to check.  Put those an array like this:

$field_arr=array("B1","B5","B17");  // add however many you need here

When you pull the resultset, go through each record and tally the scores as shown below.  This will generate an array like this:

$field_arr = array(
   "B1" = array( 0=><total>, 1=><total>, 2=><total>, 3=<total>),
 // and so on
)

Some may not be populated, which counts as a '0' total.
<?
$tally_arr = array();
while ($row=mysql_fetch_assoc($result)) {
  foreach ($field_arr as $val) {
    $tally_arr[$val][$row[$val]]++;
  }
}
?>

Open in new window

0
 
syedasimmeesaqAuthor Commented:
hmm..I have no clue where would I put this....could you give me a clue by putting it in the code above.
Thanks
0
 
Steve BinkCommented:
You would not insert it into your current code...you would replace your current code (up to about line 100 in your post) with this system.  At line 100, instead of going through each individual variable, you would go through the 1st-level elements of the $tally_arr array.
0
 
hieloCommented:
try:
<?php
$allrows = mysql_num_rows($alltable);
//as we are only interested in 1 and 2 which are yes and No respectively, we just set 1 and 2 of every column to count //from zer0
$i=0;
$temp=array('B'=>'1,2,5,17,19,22', 'C'=>'6,9');
foreach($temp as $k => $v)
{
	$temp2=explode(",",$v);
	foreach($temp2 as $v2)
	{
		${$k.$v2.'_arr'}=0;
	}
}
 
 
while ($i < $allrows)
...

Open in new window

0
 
Ray PaseurCommented:
It still seems like we can find a more generalized example that is easy for you to adapt.  Can you please post your query and show us the structure of the tables?  Thanks, ~Ray
0
 
syedasimmeesaqAuthor Commented:
heilo what about the part after I execute the while loop? Also in your above suggestion it was B. Should it not be $B as we are trying to set the value of variable $B to zero.

@ray

here is the query




$alltable = mysql_query("select * from Table where District_ID='".$_SESSION['user']."' ") or die ("union err: ".mysql_error());

$allrows = mysql_num_rows($alltable);
//as we are only interested in 1 and 2 which are yes and No respectively, we just set 1 and 2 of every column to count //from zer0



$i=0;
....
..


Thanks
0
 
hieloCommented:
>>Should it not be $B
No.
<?php
 
$allrows = mysql_num_rows($alltable);
//as we are only interested in 1 and 2 which are yes and No respectively, we just set 1 and 2 of every column to count //from zer0
 
$temp=array('B'=>'1,2,5,17,19,22', 'C'=>'6,9');
foreach($temp as $k => $v)
{
	$temp2=explode(",",$v);
	foreach($temp2 as $v2)
	{
		${$k.$v2.'_arr'}=0;
	}
}
 
while($data=mysql_fetch_assoc($alltable))
{
	foreach($data as $k=>$v){
		if( $k=='B1' && (int)$v==0)
		{
			$B1N_arr[$B1]++;
		}
		elseif( (int)$v==1 )
		{
			${$k.'_arr'}[$k]++;
		}
		elseif( (int)$v==3 )
		{
			${$k.'t_arr'}[$k]++;
		}
	}
}
 
foreach($temp as $k => $v)
{
	$temp2=explode(",",$v);
	foreach($temp2 as $v2)
	{
		${$k.$v2.'trows'}=$allrows - ${$k.$v2.'t_arr'}[3];
		${$k.$v2.'pre'}=(${$k.$v2.'_arr'}[1] / ${$k.$v2.'trows'})*100;
		${$k.$v2}=number_format(${$k.$v2.'pre'}, 2) . " %";
	}
}

Open in new window

0
 
syedasimmeesaqAuthor Commented:
thanks
0
 
hieloCommented:
you are welcome.
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 7
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now