Solved

help shorten this code

Posted on 2008-10-04
16
174 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 3
  • +1
16 Comments
 
LVL 110

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 50

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 110

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 50

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 50

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 110

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 500 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

733 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