Problem with this PHP-script

Discussion in 'PHP' started by Jontish, Jan 11, 2010.

  1. #1
    I'm running a CMS called MemHT. Demo: http://tibiahelp.com

    The site overall is loading relatively fast, except one script, that one takes over 10 seconds to load. The strange thing is, the problem started just some months ago, and got worse and worse for every day (loading time increased). I asked for support at the official site, no response.

    It's not the server. I just bought a new dedicated server, Xeon 2.40 GHZ, 4 GB RAM. All other scripts with the same construction loads fast.
    Windows 2003 Server using Apache & MySQL.

    I'm not a coder, still, I always manage to fix the scripts somehow by using common sense. Anyway, this one just seems impossible, because I can't find where the problem lies. I reckon that it's loading slower and slower due to the database getting bigger and bigger, so with other words, it's really bad optimized. I have tried to run the operation "optimize database" from phpMyAdmin, made no difference at all.

    I would very much appreciate someone to fix it, or just give me a clue what causing it. Thank you.

    The script in browser: http://tibiahelp.com/index.php?page=last-post

    Code:
    	require_once("pages/forum/inc_functions.php");
    	
    	global $dblink,$siteConfig;
    	
    	echo "<table width='100%' border='0' cellspacing='1' cellpadding='0' class='std_nicetable'>";
    	echo "<thead>";
    	echo "<tr><td width='80%'>"._THREAD_."</td><td width='5%' align='center'>"._REPLIES_."</td><td width='5%' align='center'>"._VIEWS_."</td><td width='10%' align='center' nowrap>"._DATE_."</td></tr>";
    	echo "</thead>";
    	echo "<tbody>";
    	
    	$myrank = myRank();
    	
    	$n = 0;
    	$result = $dblink->get_list("SELECT p.id,p.lastchild,p.name,DATE_FORMAT(d.date,'".$siteConfig['timestamp']."') as date,d.author,(SELECT COUNT(*) FROM memht_forum_posts WHERE id=p.id OR parent=p.id) AS pstnum FROM memht_forum_posts AS p JOIN memht_forum_forums AS f JOIN memht_forum_posts AS d ON p.forum=f.id AND p.lastchild=d.id WHERE f.auth_view<=$myrank AND f.auth_read<=$myrank ORDER BY p.lastchild DESC LIMIT 25");
    	foreach ($result as $row) {
    		$bid = intval($row['id']);
    		$blastchild = intval($row['lastchild']);
    		$btitle = outCode($row['name'],0);
    		$bdate = $row['date'];
    		$bauthor = outCode($row['author']);
    		$pstnum = intval($row['pstnum']);
    		
    		$link = "index.php?page=forum&op=viewThread&id=$bid&title=".mem_urlencode($btitle);
    		if ($pstnum>10) { $link .= "&pg=".ceil($pstnum/10); }
    		if ($bid!=$blastchild) { $link .= "#post{$blastchild}"; }
    		
    		$fontcolor_i = "";
    		$fontcolor_e = "";
    		if (isset($_COOKIE['forumtrack'])) {
    			$cookiecontent = inCode($_COOKIE['forumtrack']);
    			$exp = explode("-",$cookiecontent);
    			$first = $exp[0];
    				
    			if ($blastchild>$first AND !in_array($blastchild,$exp)) {
    				$fontcolor_i = "<font color='#15970F'>";
    				$fontcolor_e = "</font>";
    			}
    		}
    			
    		$numbers = getNumbers($bid);
    		$replies = $numbers[0];
    		$views = $numbers[1];
    		
    		$class = (($n++%2)!=0) ? "std_hlight" : "std_clean" ;
    			
    		echo "<tr><td class='$class' style='font-size:90%;'><a href='$link' title='$btitle'>{$fontcolor_i}<b>$btitle</b>{$fontcolor_e}</a><br>{$fontcolor_i}"._LASTPOST_." "._BY_.": $bauthor{$fontcolor_e}</td><td align='center' class='$class' style='font-size:90%;'>{$fontcolor_i}$replies{$fontcolor_e}</td><td align='center' class='$class' style='font-size:90%;'>{$fontcolor_i}$views{$fontcolor_e}</td><td align='center' class='$class' style='font-size:90%;' nowrap>{$fontcolor_i}$bdate{$fontcolor_e}</td></tr>";
    	}
    	echo "</tbody>";
    	echo "</table>";
    
    
    PHP:
     
    Jontish, Jan 11, 2010 IP
  2. astkboy2008

    astkboy2008 Peon

    Messages:
    211
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i know this script
    its use great tpl
    but is this cusing in server load
    i think the error in the larg mysql query
     
    astkboy2008, Jan 11, 2010 IP
  3. Jontish

    Jontish Well-Known Member

    Messages:
    1,565
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    128
    #3
    Ye, so I figured.

    Do you have any idea how I can optimize it better?
     
    Jontish, Jan 11, 2010 IP
  4. Snake Blisken

    Snake Blisken Peon

    Messages:
    179
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Why dont you just look at the query loads and optimise the query that's the problem.

    Or run the query make a separate table and cache the results and pull it from there. preventing the query form running every time.

    Let me know if you need help with that!
     
    Snake Blisken, Jan 11, 2010 IP
  5. vanadent

    vanadent Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I was using MemHT months ago and got the same problem with you. It load high cpu resource. I think the problem is the template load. For now, i'm using joomla.
     
    vanadent, Jan 11, 2010 IP
  6. Jontish

    Jontish Well-Known Member

    Messages:
    1,565
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    128
    #6
    Thank you, I would very much appreciate if you could come up with some practical solution. I'm not good in coding, especially not when sql is involved.. :(
     
    Jontish, Jan 11, 2010 IP
  7. Jenny23

    Jenny23 Peon

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I suggest to start with a different script, one that comes more close to what you need already maybe?
     
    Jenny23, Jan 11, 2010 IP
  8. Jenny23

    Jenny23 Peon

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    edit: especially when you are not that familiar with coding
     
    Jenny23, Jan 11, 2010 IP
  9. Jontish

    Jontish Well-Known Member

    Messages:
    1,565
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    128
    #9
    Jenny, I didn't get you, sorry.
    Do you mean that I should write the same script but in a different, more optimized way?
     
    Jontish, Jan 11, 2010 IP