Optimizing this script...

Discussion in 'PHP' started by Al Capone, Oct 16, 2007.

  1. #1
    My host made me move to a dedicated server and I discovered that the only thing using so much CPU was this script:
    http://www.atarcade.com/test123.php

    
    
                        <TR>
                          <TD>
                            <TABLE cellSpacing=0 cellPadding=0 width="100%" 
    border=0>
                              <TBODY>
                              <TR>
                                <TD>
                                  <TABLE cellSpacing=0 cellPadding=0 
                                  width="100%" border=0>
                                    <TBODY>
                                    <TR>
                                    <TD> 
    
    
                                    </TD></TR></TBODY></TABLE></TD></TR>
    
                              <TR>
                                <TD>
    
    <tr><td><table width="100%"  border="0" cellpadding="0" cellspacing="0" class="categHead"> 
    <tr><td>&nbsp; Top Flash Games </td> 
    </tr></table></td></tr><tr><td><table width="100%"  border="0" cellspacing="6" cellpadding="0"> 
     <tr>
    <?
    $sql2 = "SELECT * FROM games Where category!='Classic Nintendo' AND category!='Video' AND category!='Other' AND category!='GBC' ORDER BY played DESC LIMIT 0,2";
    $result2 = mysql_query($sql2); 
    while($row2 = mysql_fetch_array($result2)) {
    ?>
       <td width="50%" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="3" class="game"> 
      <tbody> 
        <tr align="left" valign="top"> 
          <td align="center" width="33%"><span class="normalText">
    	  <a href="play-flash-<? echo $row2["gameid"]; ?>.html">	  
    	  <img src="http://files.atarcade.com/img/<? echo $row2["image"]; ?>" alt="<? echo $row2["name"]; ?>" border="0" height="60" width="60"></a></span></td> 
          <td class="normalText"> <b>
    	  <a href="play-flash-<? echo $row2["gameid"]; ?>.html">        <? echo $row2["name"]; ?> 
            </a></b><br> 
            <?php 
    
    $max_length = 55;
    if (strlen($row2['desc'])>$max_length)
        echo substr($row2['desc'], 0, strrpos(substr($row2['desc'], 0, $max_length), ' ')).'.';
    else
        echo $row2["desc"];
    
    ?>  
    </td> 
        </tr> 
      </tbody> 
    
    </table>
    </td> 
                            <?
    }					
    ?>
    <?
    $sql2 = "SELECT * FROM games";
    $result2 = mysql_query($sql2); 
    $totalplayed = 0;
    while($row2 = mysql_fetch_array($result2)) {
    $totalplayed = $totalplayed+$row2["played"];
    }
    ?>
    <tr>
    <?
    $sql2 = "SELECT * FROM games Where category!='Classic Nintendo' AND category!='Video' AND category!='Other' AND category!='GBC' ORDER BY played DESC LIMIT 2,2";
    $result2 = mysql_query($sql2); 
    while($row2 = mysql_fetch_array($result2)) {
    ?>
       <td width="50%" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="3" class="game"> 
      <tbody> 
        <tr align="left" valign="top"> 
          <td align="center" width="33%"><span class="normalText">
    	  <a href="play-flash-<? echo $row2["gameid"]; ?>.html">	  
    	  <img src="http://files.atarcade.com/img/<? echo $row2["image"]; ?>" alt="<? echo $row2["name"]; ?>" border="0" height="60" width="60"></a></span></td> 
          <td class="normalText"> <b>
    	  <a href="play-flash-<? echo $row2["gameid"]; ?>.html">        <? echo $row2["name"]; ?> 
            </a></b><br> 
            <?php 
    
    $max_length = 55;
    if (strlen($row2['desc'])>$max_length)
        echo substr($row2['desc'], 0, strrpos(substr($row2['desc'], 0, $max_length), ' ')).'.';
    else
        echo $row2["desc"];
    
    ?>  
            <br> 
        </tr> 
      </tbody> 
    
    </table>
    </td> 
                            <?
    }					
    ?>
    <?
    $sql2 = "SELECT * FROM games";
    $result2 = mysql_query($sql2); 
    $totalplayed = 0;
    while($row2 = mysql_fetch_array($result2)) {
    $totalplayed = $totalplayed+$row2["played"];
    }
    ?>
    
    
    
    
    
    </tr>
     
    </table></td></tr><tr> <td><table width="100%"  border="0" cellpadding="0" cellspacing="0" class="categFoot"> 
    <tr><td><div align="right"> 
     
    <a href="top-100-games.html" class="blackLink">View Top 100 Flash Games ...</a> 
     
    </div></td></tr></table></td></tr></table></td></tr></table>
    
    
    PHP:

    does anyone know how I can optimize this to use less server resources, I want it to look the same but just use less resources, this code gets the job done but it uses so much CPU seconds.
     
    Al Capone, Oct 16, 2007 IP
  2. jnestor

    jnestor Peon

    Messages:
    133
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The script isn't doing a whole lot. Make sure you have an index on your games table.
     
    jnestor, Oct 16, 2007 IP
  3. TlcAndres

    TlcAndres Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It looks like it's counting all the games twice and not using that data for anything

    $sql2 = "SELECT * FROM games";
    $result2 = mysql_query($sql2);
    $totalplayed = 0;
    while($row2 = mysql_fetch_array($result2)) {
    $totalplayed = $totalplayed+$row2["played"];
    }

    Unless I'm mistaken that there is a waste of resources, is there more to the script?
     
    TlcAndres, Oct 16, 2007 IP