...

View Full Version : Help with cutting down script size



Taylor_1978
01-19-2004, 03:56 PM
Ok... First let me say - I have never once used Arrays in my life and every time I attempt to read up on them I just get majorly thrown!

Having said that - what I am after here may have nothing to do with arrays! LOL

Anways - the script I am about to show really isnt that big, but I figure if someone could possibly help me just to shorten down the repetiveness of this variable script, I may just "get it" on how to clean up all of my scripts - as they get quite messy, repetitive and LARGE!

Here it is:




<?
include ("connect.php");
echo "<title>Tourney Variables</title>";


// Tournament Variables
// ------------------------------

$result = mysql_db_query(tourneys, "SELECT * FROM info WHERE tid='$tid'");
$row = mysql_fetch_array($result);
$t_url = $row["url"];
$tname = $row["tname"];
$style = $row["style"];
$format = $row["format"];
$teams = $row["teams"];
$hostid = $row["uid"];
$time = $row["time"];
$status = $row["status"];
$entry = $row["entry"];
$registered = $row["registered"];

// Team 1
$p1 = $row["p1"];
$s1 = $row["s1"];
$r1 = $row["r1"];
$p1a = $row["p1a"];
$p1name = $row["p1name"];
$pass1 = $row["pass1"];

// Team 2
$p2 = $row["p2"];
$s2 = $row["s2"];
$r2 = $row["r2"];
$p2a = $row["p2a"];
$p2name = $row["p2name"];
$pass2 = $row["pass2"];

// Team 3
$p3 = $row["p3"];
$s3 = $row["s3"];
$r3 = $row["r3"];
$p3a = $row["p3a"];
$p3name = $row["p3name"];
$pass3 = $row["pass3"];

// Team 4
$p4 = $row["p4"];
$s4 = $row["s4"];
$r4 = $row["r4"];
$p4a = $row["p4a"];
$p4name = $row["p4name"];
$pass4 = $row["pass4"];

// Team 5
$p5 = $row["p5"];
$s5 = $row["s5"];
$r5 = $row["r5"];
$p5a = $row["p5a"];
$p5name = $row["p5name"];
$pass5 = $row["pass5"];

// Team 6
$p6 = $row["p6"];
$s6 = $row["s6"];
$r6 = $row["r6"];
$p6a = $row["p6a"];
$p6name = $row["p6name"];
$pass6 = $row["pass6"];

// Team 7
$p1 = $row["p7"];
$s1 = $row["s7"];
$r1 = $row["r7"];
$p1a = $row["p7a"];
$p1name = $row["p7name"];
$pass1 = $row["pass7"];

// Team 8
$p8 = $row["p8"];
$s8 = $row["s8"];
$r8 = $row["r8"];
$p8a = $row["p8a"];
$p8name = $row["p8name"];
$pass8 = $row["pass8"];

// Team Positions
$pos1 = $row["pos1"];
$pos2 = $row["pos2"];
$pos3 = $row["pos3"];
$pos4 = $row["pos4"];
$pos5 = $row["pos5"];
$pos6 = $row["pos6"];
$pos7 = $row["pos7"];
$pos8 = $row["pos8"];
$pos9 = $row["pos9"];
$pos10 = $row["pos10"];
$pos11 = $row["pos11"];
$pos12 = $row["pos12"];
$pos13 = $row["pos13"];
$pos14 = $row["pos14"];
$pos15 = $row["pos15"];
$pos31 = $row["pos31"];
$pos32 = $row["pos32"];
$pos33 = $row["pos33"];
$pos34 = $row["pos34"];
$pos35 = $row["pos35"];
$pos36 = $row["pos36"];
$pos37 = $row["pos37"];
$pos38 = $row["pos38"];
$pos39 = $row["pos39"];
$pos40 = $row["pos40"];
$pos41 = $row["pos41"];
$pos42 = $row["pos42"];
$pos43 = $row["pos43"];
$pos44 = $row["pos44"];
$pos45 = $row["pos45"];
$winner = $row["winner"];
$winner2 = $row["winner2"];

?>


Any tips would be greatly appreciated...

Thanks in advance :thumbsup:
Taylor.

firepages
01-19-2004, 04:42 PM
ok , first , there is probably no real need to to $var = $row['var']; in the first place .. you can just as easily use $row['var'] wherever you would use $var ?? (its also more efficient)

however extract() does exactly that for you but read the manual notes ! (http://www.php.net/extract) , its often a good idea to prefix your variables when extracted on mass to stop overwriting existing variables e.g.



<?
$result = mysql_db_query(tourneys, "SELECT * FROM info WHERE tid='$tid'");
$row = mysql_fetch_array($result);
extract( $row , EXTR_PREFIX_ALL , 'db_' ) ;
/*thats it ; ) */

/*e.g., you can now ...*/
echo $db_registered ;
?>


note that plain
extract( $row );
would produce `$registered` etc.

also check out the EXTR_REFS flag if you have php>=4.3.* as its almost as efficient as not extracting at all



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum