Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

help shorten this code

Posted on 2008-10-04
16
Medium Priority
?
182 Views
Last Modified: 2013-12-12
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
Comment
Question by:syedasimmeesaq
  • 7
  • 3
  • 3
  • +1
16 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 22641936
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
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 22641954
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
 
LVL 51

Expert Comment

by:Steve Bink
ID: 22641986
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 22642017
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
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 22642021
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
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 22642033
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
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 22642045
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
 
LVL 51

Expert Comment

by:Steve Bink
ID: 22642134
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
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 22642161
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
 
LVL 51

Expert Comment

by:Steve Bink
ID: 22642172
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
 
LVL 82

Expert Comment

by:hielo
ID: 22643497
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 22645009
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
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 22645498
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
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 22646043
>>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
 
LVL 9

Author Closing Comment

by:syedasimmeesaq
ID: 31503061
thanks
0
 
LVL 82

Expert Comment

by:hielo
ID: 22652410
you are welcome.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question