Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    528
    Thanks
    8
    Thanked 8 Times in 8 Posts

    Help with cutting down script size

    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:

    PHP Code:

    <?
    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
    Taylor.

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    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 ! , its often a good idea to prefix your variables when extracted on mass to stop overwriting existing variables e.g.

    PHP Code:
    <?
    $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
    Last edited by firepages; 01-19-2004 at 03:47 PM.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •