Solved

How to make this code shorter with while or for loop

Posted on 2009-05-15
4
291 Views
Last Modified: 2013-12-12
I ahve this code below and I was wondering if there is a way to make it shorter

Thanks
while ($i < $allrows)
{
$T_3_1 = mysql_result($alltable, $i, "T_3_1");
$T_3_2 = mysql_result($alltable, $i, "T_3_2");
$T_3_3 = mysql_result($alltable, $i, "T_3_3");
$T_3_4 = mysql_result($alltable, $i, "T_3_4");
$T_3_5 = mysql_result($alltable, $i, "T_3_5");
$T_3_6 = mysql_result($alltable, $i, "T_3_6");
$T_3_7 = mysql_result($alltable, $i, "T_3_7");
$T_4_1 = mysql_result($alltable, $i, "T_4_1");
 
 
 
 
if($T_3_1==1){
$T_3_1_arr[$T_3_1]++;
}
if($T_3_2==1){
$T_3_2_arr[$T_3_2]++;
}
if($T_3_3==1){
$T_3_3_arr[$T_3_3]++;
}
if($T_3_4==1){
$T_3_4_arr[$T_3_4]++;
}
if($T_3_5==1){
$T_3_5_arr[$T_3_5]++;
}
if($T_3_6==1){
$T_3_6_arr[$T_3_6]++;
}
if($T_3_7==1){
$T_3_7_arr[$T_3_7]++;
}
if($T_4_1==1){
$T_4_1_arr[$T_4_1]++;
}
 
$i++;
 
 
}//closing while loop
 
// calculating the first page
 
$T_3_1trows = $allrows - $T_3_1[1];
$T_3_1pre = ($T_3_1_arr[1] / $allrows) * 100;
$T_3_1n = number_format($T_3_1pre, 2)." %";
 
$T_3_2trows = $allrows - $T_3_2[1];
$T_3_2pre = ($T_3_2_arr[1] / $allrows) * 100;
$T_3_2n = number_format($T_3_2pre, 2)." %";
 
$T_3_3trows = $allrows - $T_3_3[1];
$T_3_3pre = ($T_3_3_arr[1] / $allrows) * 100;
$T_3_3n = number_format($T_3_3pre, 2)." %";

Open in new window

0
Comment
Question by:syedasimmeesaq
  • 2
4 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 24399816
I lack data to test it, but something along these lines might do it
<?php
 
function checkValue( & $arr, $val, $i, $alltable ) {
     $v = mysql_result( $alltable, $i, $val );
     if ( $v == 1 )
          $arr[1]++;
}
 
 
while ($i < $allrows) {
 
     checkValue( $T_3_1_arr, "T_3_1", $i, $alltable );
     checkValue( $T_3_2_arr, "T_3_2", $i, $alltable );
     checkValue( $T_3_3_arr, "T_3_2", $i, $alltable );
     checkValue( $T_3_4_arr, "T_3_2", $i, $alltable );
     checkValue( $T_3_5_arr, "T_3_2", $i, $alltable );
     checkValue( $T_3_6_arr, "T_3_2", $i, $alltable );
     checkValue( $T_3_7_arr, "T_3_2", $i, $alltable );
     checkValue( $T_4_1_arr, "T_4_1", $i, $alltable );
 
	$i++;
 
} //closing while loop
 
// calculating the first page
 
$T_3_1trows = $allrows - $T_3_1[1];
$T_3_1pre = ($T_3_1_arr[1] / $allrows) * 100;
$T_3_1n = number_format($T_3_1pre, 2) . " %";
 
$T_3_2trows = $allrows - $T_3_2[1];
$T_3_2pre = ($T_3_2_arr[1] / $allrows) * 100;
$T_3_2n = number_format($T_3_2pre, 2) . " %";
 
$T_3_3trows = $allrows - $T_3_3[1];
$T_3_3pre = ($T_3_3_arr[1] / $allrows) * 100;
$T_3_3n = number_format($T_3_3pre, 2) . " %";
?>

Open in new window

0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24399829
LOL - just noticed that I missed some stuff on the cut'n'paste. The middle section should be

     checkValue( $T_3_3_arr, "T_3_3", $i, $alltable );
     checkValue( $T_3_4_arr, "T_3_4", $i, $alltable );
     checkValue( $T_3_5_arr, "T_3_5", $i, $alltable );
     checkValue( $T_3_6_arr, "T_3_6", $i, $alltable );
     checkValue( $T_3_7_arr, "T_3_7", $i, $alltable );
 
0
 
LVL 7

Expert Comment

by:ycTIN
ID: 24401157
many statement are waste in you code

if($T_4_1==1){
   $T_4_1_arr[$T_4_1]++; //$T_4_1  = 1
}


:) shortest code:

<?php
$collection = array("T_3_1","T_3_2","T_3_3","T_3_4","T_3_5","T_3_6","T_3_7","T_4_1");
while ($i < $allrows) foreach ($collection as $key) if (1 == mysql_result($alltable, $i++, $key)) @${$key."_arr"}[1]++;
?>

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 24403944
Those repeated numbers in the variable names indicate to me that this might be a good place to use a multidimensional array instead of explicitly coded scalar variable names.  Just a thought.  

Also, have a look at the code snippet.  If I understand correctly, you have just done a MySQL query where you returned a results set containing columns named like T_3_1, T_3_2, etc.  The two segments below are likely to give equivalent results, and the latter will be substantially faster.

HTH, ~Ray
// FROM THE OP
$T_3_1 = mysql_result($alltable, $i, "T_3_1");
$T_3_2 = mysql_result($alltable, $i, "T_3_2");
$T_3_3 = mysql_result($alltable, $i, "T_3_3");
$T_3_4 = mysql_result($alltable, $i, "T_3_4");
$T_3_5 = mysql_result($alltable, $i, "T_3_5");
$T_3_6 = mysql_result($alltable, $i, "T_3_6");
$T_3_7 = mysql_result($alltable, $i, "T_3_7");
$T_4_1 = mysql_result($alltable, $i, "T_4_1");
 
// AN ALTERNATIVE
$row = mysql_fetch_assoc($alltable);
extract($row);

Open in new window

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 47
Why do people dis php? 5 45
how can i gnupg encrypt a PDF document? 12 24
How do I fix this UPDATE error? 7 20
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
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…

815 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

8 Experts available now in Live!

Get 1:1 Help Now