Jump to content

Recommended Posts

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);

Link to comment
https://forums.phpfreaks.com/topic/220118-shorthen-this/
Share on other sites

$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

Link to comment
https://forums.phpfreaks.com/topic/220118-shorthen-this/#findComment-1140879
Share on other sites

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;

Link to comment
https://forums.phpfreaks.com/topic/220118-shorthen-this/#findComment-1141195
Share on other sites

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."'");
?>

Link to comment
https://forums.phpfreaks.com/topic/220118-shorthen-this/#findComment-1141665
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/220118-shorthen-this/#findComment-1141677
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/220118-shorthen-this/#findComment-1141683
Share on other sites

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);
?>

Link to comment
https://forums.phpfreaks.com/topic/220118-shorthen-this/#findComment-1141699
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/220118-shorthen-this/#findComment-1141710
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.