RON_ron Posted November 29, 2010 Share Posted November 29, 2010 Can someone help to write this code in a shorter way? $Var = 'name'; $queryU = "SELECT * FROM one WHERE username = '".$Var."'"; $result = mysql_query($query); $score = mysql_fetch_assoc($result); $usera1 = $score['round1']; $usera2 = $score['round2']; $usera3 = $score['round3']; $usera4 = $score['round4']; $usera5 = $score['round5']; $usera6 = $score['round6']; $usera7 = $score['round7']; $usera8 = $score['round8']; //// $nextVar = 'roundx'; $queryx = "SELECT * FROM acs WHERE round1 = '".$nextVar."'"; $resultx = mysql_query($queryx); $scorex = mysql_fetch_assoc($resultx); $win1 = $score['inxa']; $win2 = $score['inxb']; $win3 = $score['inxc']; $win4 = $score['inxd']; $win5 = $score['inxe']; $win6 = $score['inxf']; $win7 = $score['inxg']; $win8 = $score['inxh']; //// $ArrayU1 = array($usera1); $Arrayk1 = array($win1); $ArrayU2 = array($usera2); $Arrayk2 = array($win2); $ArrayU3 = array($usera3); $Arrayk3= array($win3); $ArrayU4 = array($usera4); $Arrayk4 = array($win4); $ArrayU5 = array($usera5); $Arrayk5 = array($win5); $ArrayU6 = array($usera6); $Arrayk6 = array($win6); $ArrayU7 = array($usera7); $Arrayk7 = array($win7); $ArrayU8 = array($usera8); $Arrayk8 = array($win8); $count1 = count( array_intersect($ArrayU1, $Arrayk1) ); $count2 = count( array_intersect($ArrayU2, $Arrayk2) ); $count3 = count( array_intersect($ArrayU3, $Arrayk3) ); $count4 = count( array_intersect($ArrayU4, $Arrayk4) ); $count5 = count( array_intersect($ArrayU5, $Arrayk5) ); $count6 = count( array_intersect($ArrayU6, $Arrayk6) ); $count7 = count( array_intersect($ArrayU7, $Arrayk7) ); $count8 = count( array_intersect($ArrayU8, $Arrayk8) ); echo($count1+$count2+$count3+$count4+$count5+$count6+$count7+$count8); Quote Link to comment https://forums.phpfreaks.com/topic/220118-shorthen-this/ Share on other sites More sharing options...
trq Posted November 29, 2010 Share Posted November 29, 2010 Just look at it. Anywhere where you repeat similar data is redundant. Looks to me like your database design stinks as well though, so I'd start there. Quote Link to comment https://forums.phpfreaks.com/topic/220118-shorthen-this/#findComment-1140837 Share on other sites More sharing options...
sasa Posted November 29, 2010 Share Posted November 29, 2010 $nextVar = 'roundx'; $lett = array(1 => 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'); $count = 0; $queryU = "SELECT * FROM one WHERE username = '".$Var."'"; $result = mysql_query($query); $score = mysql_fetch_assoc($result); //$usera1 = $score['round1']; $nextVar = 'roundx'; $queryx = "SELECT * FROM acs WHERE round1 = '".$nextVar."'"; $resultx = mysql_query($queryx); $scorex = mysql_fetch_assoc($resultx); //$win1 = $score['inxa']; for($i = 1; $i <= 8; $i++) $count += $score['round'.$i]==$score['inx'.$lett[$i]] ? 1 : 0; echo $count; btw for what is $scorex? not tested Quote Link to comment https://forums.phpfreaks.com/topic/220118-shorthen-this/#findComment-1140879 Share on other sites More sharing options...
RON_ron Posted November 30, 2010 Author Share Posted November 30, 2010 There is an error with this code. Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/.../test.php on line 10 Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/.../test.php on line 15 $nextVar = 'roundx'; $lett = array(1 => 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'); $count = 0; $queryU = "SELECT * FROM one WHERE username = '".$Var."'"; $result = mysql_query($query); $score = mysql_fetch_assoc($result); //$usera1 = $score['round1']; $nextVar = 'roundx'; $queryx = "SELECT * FROM acs WHERE round1 = '".$nextVar."'"; $resultx = mysql_query($queryx); $scorex = mysql_fetch_assoc($resultx); //$win1 = $score['inxa']; for($i = 1; $i <= 8; $i++) $count += $score['round'.$i]==$score['inx'.$lett[$i]] ? 1 : 0; echo $count; Quote Link to comment https://forums.phpfreaks.com/topic/220118-shorthen-this/#findComment-1141195 Share on other sites More sharing options...
sasa Posted November 30, 2010 Share Posted November 30, 2010 this two lines is just copied from your code Quote Link to comment https://forums.phpfreaks.com/topic/220118-shorthen-this/#findComment-1141258 Share on other sites More sharing options...
RON_ron Posted November 30, 2010 Author Share Posted November 30, 2010 this two lines is just copied from your code Please ignore this. Sorry! sasa - That code is awesome! Thank you. But it always echo 8 (not the value of the COUNT). Why is that? My previous code echo the COUNT correctly. I appreciate your help. Quote Link to comment https://forums.phpfreaks.com/topic/220118-shorthen-this/#findComment-1141260 Share on other sites More sharing options...
sasa Posted November 30, 2010 Share Posted November 30, 2010 in your code you query another table from database and put result in $scorex but you never use it maybe for($i = 1; $i <= 8; $i++) $count += $score['round'.$i]==$scorex['inx'.$lett[$i]] ? 1 : 0; Quote Link to comment https://forums.phpfreaks.com/topic/220118-shorthen-this/#findComment-1141323 Share on other sites More sharing options...
RON_ron Posted December 1, 2010 Author Share Posted December 1, 2010 oh! I see... :-\ This is what happens in my script. It collects data from 2 different databases (8 fields from each database) then count the matching results and update another database with the count value. Below is the exact script. Please help me shorten this. $VariableA = 'myname'; $queryU1 = "SELECT * FROM answers_db WHERE username = '".$VariableA."'"; $resultU = mysql_query($queryU1); $scoreU = mysql_fetch_assoc($resultU); $userRa1 = $scoreU['rndAa']; $userRa2 = $scoreU['rndAb']; $userRa3 = $scoreU['rndAc']; $userRa4 = $scoreU['rndAd']; $userRa5 = $scoreU['rndAe']; $userRa6 = $scoreU['rndAf']; $userRa7 = $scoreU['rndAg']; $userRa8 = $scoreU['rndAh']; //// $VariableB = 'rndA'; $query1 = "SELECT * FROM results_db WHERE rndx = '".$VariableB."'"; $result = mysql_query($query1); $scoreM = mysql_fetch_assoc($result); $winRa1 = $scoreM['inxa']; $winRa2 = $scoreM['inxb']; $winRa3 = $scoreM['inxc']; $winRa4 = $scoreM['inxd']; $winRa5 = $scoreM['inxe']; $winRa6 = $scoreM['inxf']; $winRa7 = $scoreM['inxg']; $winRa8 = $scoreM['inxh']; $ArrayU1 = array($userRa1); $ArrayM1 = array($winRa1); $ArrayU2 = array($userRa2); $ArrayM2 = array($winRa2); $ArrayU3 = array($userRa3); $ArrayM3 = array($winRa3); $ArrayU4 = array($userRa4); $ArrayM4 = array($winRa4); $ArrayU5 = array($userRa5); $ArrayM5 = array($winRa5); $ArrayU6 = array($userRa6); $ArrayM6 = array($winRa6); $ArrayU7 = array($userRa7); $ArrayM7 = array($winRa7); $ArrayU8 = array($userRa8); $ArrayM8 = array($winRa8); $count1 = count( array_intersect($ArrayU1, $ArrayM1) ); $count2 = count( array_intersect($ArrayU2, $ArrayM2) ); $count3 = count( array_intersect($ArrayU3, $ArrayM3) ); $count4 = count( array_intersect($ArrayU4, $ArrayM4) ); $count5 = count( array_intersect($ArrayU5, $ArrayM5) ); $count6 = count( array_intersect($ArrayU6, $ArrayM6) ); $count7 = count( array_intersect($ArrayU7, $ArrayM7) ); $count8 = count( array_intersect($ArrayU8, $ArrayM8) ); $countF = $count1+$count2+$count3+$count4+$count5+$count6+$count7+$count8; mysql_query("UPDATE points_db SET mypoints=$countF WHERE username = '".$VariableA."'"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/220118-shorthen-this/#findComment-1141665 Share on other sites More sharing options...
kenrbnsn Posted December 1, 2010 Share Posted December 1, 2010 You can try this, but I'm not sure if I've followed your logic correctly: <?php $ArrayU = array(); $ArrayM = array(); $VariableA = 'myname'; $queryU1 = "SELECT * FROM answers_db WHERE username = '".$VariableA."'"; $resultU = mysql_query($queryU1); $scoreU = mysql_fetch_assoc($resultU); foreach (range('a','h') as $ltr) { $ArrayU[] = array($scoreU['rndA' . $ltr]); } //// $VariableB = 'rndA'; $query1 = "SELECT * FROM results_db WHERE rndx = '".$VariableB."'"; $result = mysql_query($query1); $scoreM = mysql_fetch_assoc($result); foreach (range('a','h') as $ltr) { $ArrayM[] = array($scoreU['inxA' . $ltr]); } $count = array(); for($i=0;$i<count($ArrayU);++$i) { count[$i] = count(array_intersect($ArrayU[$i],$ArrayM[$i])); } $countF = array_sum($count); mysql_query("UPDATE points_db SET mypoints=$countF WHERE username = '".$VariableA."'"); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/220118-shorthen-this/#findComment-1141677 Share on other sites More sharing options...
RON_ron Posted December 1, 2010 Author Share Posted December 1, 2010 Thanks Ken. I'm getting an error. Parse error: syntax error, unexpected '[' in /home/.../test.php on line 22 count[$i] = count(array_intersect($ArrayU[$i],$ArrayM[$i])); Also the array_intersect should match all 8 results from answers_db against all 8 results in results_db in the same order (answers_db first result should check results_db first result if it match, answers_db second result should check results_db second result if it match, etc...). E.g. answers_db ($ArrayU) 90, 20, 75, 90, 30, 90, 80, 51 results_db ($ArrayM) 95, 93, 95, 90, 90, 90, 90, 91 $countF = 2 Quote Link to comment https://forums.phpfreaks.com/topic/220118-shorthen-this/#findComment-1141683 Share on other sites More sharing options...
kenrbnsn Posted December 1, 2010 Share Posted December 1, 2010 I forgot the "$" on "count" in this line <?php count[$i] = count(array_intersect($ArrayU[$i],$ArrayM[$i])); ?> it should be <?php $count[$i] = count(array_intersect($ArrayU[$i],$ArrayM[$i])); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/220118-shorthen-this/#findComment-1141695 Share on other sites More sharing options...
dawsba Posted December 1, 2010 Share Posted December 1, 2010 and heres my 2pence for fun <?PHP $Var = array(1=>'name',2=>'roundx'); $alpha = array("a","b","c","d","e","f","g","h"); $q = array(1=>"SELECT * FROM one WHERE username = '".$Var[1]."'",2=>"SELECT * FROM acs WHERE round1 = '".$Var[2]."'"); $resultU = mysql_query($q[1]); $score = mysql_fetch_assoc($resultU); $resultx = mysql_query($q[2]); $scorex = mysql_fetch_assoc($resultx); for($i=1;$i<=8;$i++) { $UAX = "usera".$i; $$UAX = $score['round'.$i]; $UAXX = "ArrayU".$i; $$UAXX = array($$UAX); $UAX = "win".$i;$j=$i-1; $$UAX = $scorex['inx'.$alpha[$j]]; $UAXX = "Arrayk".$i; $$UAXX = array($$UAX); $UAX = "count".$i; $UAXX = "ArrayU".$i; $UAXXX = "Arrayk".$i; $$UAX = count(array_intersect($$UAXX, $UAXXX) ); } echo($count1+$count2+$count3+$count4+$count5+$count6+$count7+$count8); ?> Quote Link to comment https://forums.phpfreaks.com/topic/220118-shorthen-this/#findComment-1141699 Share on other sites More sharing options...
RON_ron Posted December 1, 2010 Author Share Posted December 1, 2010 Hi Ken. Thanks. When I run my script everything happens and update. But when I use yours it doesn't update. Any idea why? Do I have to do any adjustments to make it work? Quote Link to comment https://forums.phpfreaks.com/topic/220118-shorthen-this/#findComment-1141706 Share on other sites More sharing options...
kenrbnsn Posted December 1, 2010 Share Posted December 1, 2010 Since I don't have you data or database, I can't debug the code. You will have to debug it by putting in "print_r"s of the various arrays to check to see if they are getting populated. Also change the update to <?php $q = "UPDATE points_db SET mypoints=$countF WHERE username = '$VariableA'"; mysql_query($q) or die("problem updating using query: $q<br>" . mysql_error(); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/220118-shorthen-this/#findComment-1141710 Share on other sites More sharing options...
RON_ron Posted December 1, 2010 Author Share Posted December 1, 2010 Thanks Ken... I'll try to debug. I'll come back when I'm done. Really appreciate your help. Quote Link to comment https://forums.phpfreaks.com/topic/220118-shorthen-this/#findComment-1141711 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.