Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there a better way to do this? PHP --> MYSQL code help.

Status
Not open for further replies.
May 13, 2005
56
US
Hi all,

First off ***DISCLAIMER*** Still very green to PHP/MYSQL, all help is appriciated.

Ok, I am working on an app that acts as a ranking system. What I currently am doing is:
The app will track up to 30 places in a tournament, it tracks place, position, assigns points, and calculates win and power rank %..
Since I am tracking 30 possible places, I have a TON of code when I do it the way I do.. Please let me know if there is a better/more streamlined way...

// Pulls all info for player one from players db and does proper math for 1st place
mysql_connect('localhost',$username,$password);
mysql_select_db($database) or die("Unable to select database");
$query="SELECT * FROM players WHERE name= '$one'";
$result=mysql_query($query);
mysql_close();
$onename=mysql_result($result,0,"name");
$onegames=mysql_result($result,0,"games");
$oneentered=mysql_result($result,0,"entered");
$one1st=mysql_result($result,0,"1st");
$one2nd=mysql_result($result,0,"2nd");
$one3rd=mysql_result($result,0,"3rd");
$onepoints=mysql_result($result,0,"points");
$newonegames=$onegames+1;
$newoneentered=$oneentered+$players;
$newone1st=$one1st+1;
$newonepoints=$onepoints+$onepts;
$newonepercent=(($newone1st+$one2nd+$one3rd)/$newonegames)*100;
if ($newonegames >= $powerlimit) {
$newonepower=($newonepoints/$newoneentered)*100;
echo "<input name=powerone type=hidden value=$newonepower>";
}
echo "<input name=nameone type=hidden value=$onename>";
echo "<input name=gamesone type=hidden value=$newonegames>";
echo "<input name=enteredone type=hidden value=$newoneentered>";
echo "<input name=1stone type=hidden value=$newone1st>";
echo "<input name=pointsone type=hidden value=$newonepoints>";
echo "<input name=percentone type=hidden value=$newonepercent>";
echo "<input name=powerone type=hidden value=$newonepower>";

All I do for the next place is change one to two, then three etc...
Now, this pulls the data from the table that I need to update, does some math, assigns new variables and then passes the new variables to the next form. But currently I am doing this 30 times and have almost 1000 lines of code.. I thing that some type of a loop with array would be a good way to do this, but I am not sure how I would do it, it seems to be working fine, but I am concerned that it may be a problem down the road..

Any ideas?
 
Have a look at the faq434-3850 - it is about the basic steps for getting data from the table.

Also:
Use mysql_fetch_assoc() which assigns the reutrned row into an associative array. It makes more sense to refer to $row['name'] than assigning it to another variable or using the mysql_result($result,0,'name').

Try to learn how to think abstract. How can you abstract the procedure you have and make it into a function that receives input arguments and then processes the data. The processing is always the same, the arguments change.

Easy example:
Code:
# non abstract:
$one = 1;
$nextone = 1+1;
print "$one plus 1 is $nextone";

$tow = 2;
$nexttwo = 1+1;
print "$two plus 1 is $nexttwo";

$three = 1;
$nextthree = 1+1;
print "$three plus 1 is $nextthree";
You can abstratc this into something like:
Code:
show(1);
show(2);
show(3);

function show($value){
   print "$value plus 1 is ". $value+;
}

This is a real dumb example, but it should give you the gist of what you need to do to get from 1000 lines of code to a smaller, much smaller number.
 
DRJ478

Thanks for the advise, I will look into this and try it out tonight..

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top