Solved

help shorten this code

Posted on 2008-10-04
16
169 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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 9

Author Comment

by:syedasimmeesaq
Comment Utility
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
Comment Utility
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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
thanks
0
 
LVL 82

Expert Comment

by:hielo
Comment Utility
you are welcome.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now