Jump to content

How can i cut down this code??


runnerjp

Recommended Posts

Hey guys,

 

Im trying to cut down my opening page as i repeat alot of the code on several occasions with only changing 1 or 2 pieces of information .

 

<?php
  require_once '../settings.php';
  checkLogin('1 2');
  //Your ip address is...;
  include "checkinfo.php";
  // records user view on page
  include "../getuser.php";
?>
<div class="c1">
<center><strong><u>Welcome to the RunningProfiles forum.</u></strong></center>
</div>
<center><pre>The terms and condition for the forum can be found <a href="index.php?page=forumrules">here</a>.</pre></center>
<table width="99%" class='forum'>
    <tr>
      <td>
        <table width="99%" class='maintable'>
          <tr>
            <td>
              <table class='maintable' width="100%">
                <tr>
                  <td class = "border">
                       <div class="c1"><center><b>Status</b></center></div>                
	  </td>
                  <td class = "border">
                    <div class="c1">
                      <center><b>Topic</b></center>
                    </div>
                  </td>

                  <td class = "border">
	  	<center><b>Topics/Posts</b></center>
	  </td>
                  <td class = "border" width="28%">
                    <div class="c1">
                      <center><b>Last Post</b></center>
                    </div>
                  </td>
                </tr>
                <tr class="mainrow">
                  <td width="4%">
    		 	 <div class="c1">
			<?php
  					$getthreads3 = mysql_fetch_assoc(mysql_query("Select * from forumtutorial_posts where parentid='0' and forum = 'general' ORDER BY lastrepliedto DESC LIMIT 1"));
  
  					$getthreads_result = mysql_query("Select * from forumtutorial_posts where parentid='0' and forum = 'general' ORDER BY lastrepliedto DESC ") 					or die("Could not get threads");
  
					$getthreads = mysql_fetch_array($getthreads_result);
  
  					$getlastpost = mysql_fetch_assoc(mysql_query("SELECT * FROM users WHERE Username = '$username' "));
  
					printf("<center><img src=\"http://www.runningprofiles.com/images/postforum.%s\" alt=\"My\" /></center>", ($getlastpost['General'] <= 					$getthreads['lastrepliedto']) ? "gif" : "jpg");
			?>
                         </div>
                  </td>
	  <td width="65%">
	  <p><a href="index.php?page=forum&forum=General"><b>General</b></a> 
		 <?php
			 $result = mysql_query("SELECT * FROM useronline WHERE(file='http://www.runningprofiles.com/members/index.php?page=forum&forum=general')");
  			 while ($row = mysql_fetch_array($result)) {
   			   $last_active = time() - $row['timestamp'];
		  }
			 if ($last_active < 300) {
			     echo '(', $result_count, ' viewing)';
			  }
		 ?>

	 </p>                   
	 <p>General chit chat about the athletic world!</p>
                 </td>
                 <td width="8%" align="center" valign ="middle">
		 <?php
			 if ($result = mysql_query("SELECT COUNT(title) as cnt, SUM(numreplies) as sm FROM forumtutorial_posts WHERE `forum` = 'general'")) {
     		 	if (mysql_num_rows($result)) {
        		 	$row = mysql_fetch_assoc($result);
         		 	$topic = $row['cnt'];
          			$posts = $row['cnt'] + $row['sm'];
	?>
        	<div class="c2">
                <?php echo $topic; ?> Topics <br />
	<?php echo $posts; ?> Posts
                </div>
	<?php
		 		      }
                                              													    }
                ?>
        </td>
                <td colspan="3">
	<?php
                  $dbtime = $getthreads3['lastrepliedto'];
                  $time = date("F j, Y, g:i a", $dbtime);
                  echo '<strong>Last post</strong> by <a href="'.$getthreads3['lastposter'].'">'.$getthreads3['lastposter'].'</a>';
	?>
	<br /> 
	<?php 
	echo 'in <a 	   	href="http://www.runningprofiles.com/members/index.php?page=message&forum='.$getthreads3['forum'].'&id='.$getthreads3['postid'].'&pagenum=last">'.substr($getthreads3[title], 0, 15);
                  if (strlen($getthreads3[title]) >= 15)
                  echo '...'; 
                  echo '</a> <br /> on ';gettheTime($dbtime, $time);
	?>

	</td>
                </tr>
                <tr class="mainrow">
                  <td>
                    <div class="c1">	
	        <?php

                     		$getthreads_result = mysql_query("Select * from forumtutorial_posts where parentid='0' and forum = 'races' ORDER BY lastrepliedto DESC LIMIT 1 ") 				or
                                die("Could not get threads");

                       		$getthreads = mysql_fetch_array($getthreads_result);

                     		$getlastpost = mysql_fetch_assoc(mysql_query("SELECT * FROM users WHERE Username = '$username' "));
                       

                       		printf("<center><img src=\"http://www.runningprofiles.com/images/postforum.%s\" alt=\"My\" /></center>",($getlastpost['races'] <= 							$getthreads['lastrepliedto']) ? "gif" : "jpg");
                         ?>
                    </div>
                  </td>
	  <td>
                    <p><a href="index.php?page=forum&forum=races"><b>Races</b></a>
	    <?php
                    $result = mysql_query("SELECT * FROM useronline WHERE(file='http://www.runningprofiles.com/members/index.php?page=forum&forum=races')");
                    while ($row = mysql_fetch_array($result))
                    {
                                                    $last_active = time() - $row['timestamp'];
                    }
                    if ($last_active < 300)
                    {
                                                    echo '(', $result_count, ' viewing)';
                    }
                    ?>

	    </p>

                    <p>Share you races, or even find some yourself here!</p>
                  </td>

                  <td align="center">
		  <?php

                    if ($result = mysql_query("SELECT COUNT(title) as cnt, SUM(numreplies) as sm FROM forumtutorial_posts WHERE `forum` = 'races'"))
                    {
                                                    if (mysql_num_rows($result))
                                                    {
                                                                                    $row = mysql_fetch_assoc($result);
                                                                                    $topic = $row['cnt'];
                                                                                    $posts = $row['cnt'] + $row['sm']; 
	  ?>
	  <div class="c2">
                  <?php echo $topic; ?> Topics
	  <?php echo $posts; ?> Posts</pre>
                  </div>
	  <?php 
	                                    }
                    }

                    ?>

                  </td>

                  <td>
	  	<?php


                 	 $getthreads3 = mysql_fetch_assoc(mysql_query("Select * from forumtutorial_posts where parentid='0' and forum = 'races' ORDER BY lastrepliedto DESC LIMIT 1"));


                  	 $dbtime = $getthreads3['lastrepliedto'];
                 	 $time = date("F j, Y, g:i a", $dbtime);

                 	 echo '<strong>Last post</strong> by <a href="'.$getthreads3['lastposter'].'">'.$getthreads3['lastposter'].'</a>'; 
		 ?>
		<br /> 
		<?php 
		echo 'in <a href="http://www.runningprofiles.com/members/index.php?page=message&forum='.$getthreads3['forum'].'&id='.$getthreads3['postid'].'&pagenum=last">'.substr($getthreads3[title], 0, 15);
                  		if (strlen($getthreads3[title]) >= 15)
                                   echo '...'; 
                                   echo '</a>
			   <br /> on ';
			gettheTime($dbtime, $time); 
		?>

		</td>  
               		</tr>

                	<tr class="mainrow">
                 	<td>
                    		<div class="c1">

                     		   <?php $getthreads_result = mysql_query("Select * from forumtutorial_posts where parentid='0' and forum = 'training' ORDER BY lastrepliedto DESC LIMIT 1 ") or
                     		     die("Could not get threads");

                                     $getthreads = mysql_fetch_array($getthreads_result);

                                     $getlastpost = mysql_fetch_assoc(mysql_query("SELECT * FROM users WHERE Username = '$username' "));
                       
                                     printf("<center><img src=\"http://www.runningprofiles.com/images/postforum.%s\" alt=\"My\" /></center>",($getlastpost['training'] <= 				     $getthreads['lastrepliedto']) ? "gif" : "jpg");
                                   ?>
				</div>
              		</td>

                 	<td>
                    	<p><a href="index.php?page=forum&forum=training"><b>Training</b></a> 
		<?php
                   	  $result = mysql_query("SELECT * FROM useronline WHERE(file='http://www.runningprofiles.com/members/index.php?page=forum&forum=training')");
                   	  while ($row = mysql_fetch_array($result))
                    		{
                                   $last_active = time() - $row['timestamp'];
                   		}
                   	  if ($last_active < 300)
                   	 {
                           echo '(', $result_count, ' viewing)';
                    	 }
                   	?>
		</p>

                   	<p>Share any tips, articles and killer sessions</p>
                  	</td>

                  	<td align="center">
                    	<?php
                  	  if ($result = mysql_query("SELECT COUNT(title) as cnt, SUM(numreplies) as sm FROM forumtutorial_posts WHERE `forum` = 'training'"))
                    		{
                                   if (mysql_num_rows($result))
                                {
                                $row = mysql_fetch_assoc($result);
                                $topic = $row['cnt'];
		?>
  			<?php echo $topic; ?> Topics
		<?php echo $posts; ?> Posts<br />
		<?php	
			 }
                   		 }

                   	?>
                 	</td>
	        <td>
		<?php
			$getthreads3 = mysql_fetch_assoc(mysql_query("Select * from forumtutorial_posts where parentid='0' and forum = 'training' ORDER BY lastrepliedto DESC LIMIT 1"));
                  		$dbtime = $getthreads3['lastrepliedto'];
                 
                 		echo '<strong>Last post</strong> by <a href="'.$getthreads3['lastposter'].'">'.$getthreads3['lastposter'].'</a>'; 
		?>
		<br />
		<?php echo 'in <a href="http://www.runningprofiles.com/members/index.php?page=message&forum='.$getthreads3['forum'].'&id='.$getthreads3['postid'].'&pagenum=last">'.substr($getthreads3[title], 0, 15);
                  		if (strlen($getthreads3[title]) >= 15)
                                echo '...'; 
                                echo '</a> <br /> on ';gettheTime($dbtime, $time);
		 ?>
		</td>
              		</tr>

               		<tr class="mainrow">
                 	 <td>
                    	    <div class="c1">
                                <?php
			 $getthreads_result = mysql_query("Select * from forumtutorial_posts where parentid='0' and forum = 'injuries' ORDER BY lastrepliedto DESC LIMIT 1 ") or
                      		 die("Could not get threads");

                    		 $getthreads = mysql_fetch_array($getthreads_result);

                     		 $getlastpost = mysql_fetch_assoc(mysql_query("SELECT * FROM users WHERE Username = '$username' "));
                       

                      		 printf("<center><img src=\"http://www.runningprofiles.com/images/postforum.%s\" alt=\"My\" /></center>",($getlastpost['injuries'] <= $getthreads['lastrepliedto']) ? "gif" : "jpg");
                     		?>
                  	   </div>
                 	 </td>
                	<td>
                    	   <p><a href="index.php?page=forum&forum=injuries"><b>Injuries</b></a> 
		   <?php
                    		$result = mysql_query("SELECT * FROM useronline WHERE(file='http://www.runningprofiles.com/members/index.php?page=forum&forum=injuries')");
                   		while ($row = mysql_fetch_array($result))
                    		{
                                 	$last_active = time() - $row['timestamp'];
                   		}
                   		if ($last_active < 300)
                   		{
                                        echo '(', $result_count, ' viewing)';
                   		}
                   	   ?>
		   </p>

                   	   <p>Articles on injury prevention and rehabilitation</p>
                  	</td>

                       <td align="center">
                       <?php

                       if ($result = mysql_query("SELECT COUNT(title) as cnt, SUM(numreplies) as sm FROM forumtutorial_posts WHERE `forum` = 'injuries'"))
                       {
                            if (mysql_num_rows($result))
                               {
                                       $row = mysql_fetch_assoc($result);
                                       $topic = $row['cnt'];
                                       $posts = $row['cnt'] + $row['sm']; 
	      ?>
		      <?php echo $topic; ?> Topics <br />
                      <?php echo $posts; ?> Posts</pre>
	      <?php    }
                       }

                      ?>
                  </td>

                  <td>
	  <?php 
		$getthreads3 = mysql_fetch_assoc(mysql_query("Select * from forumtutorial_posts where parentid='0' and forum = 'injuries' ORDER BY lastrepliedto DESC LIMIT 1"));
                $dbtime = $getthreads3['lastrepliedto'];
                        $time = date("F j, Y, g:i a", $dbtime);
	 echo '<strong>Last post</strong> by <a href="'.$getthreads3['lastposter'].'">'.$getthreads3['lastposter'].'</a>'; 
	 ?>
	 <br /> 
	 <?php echo 'in <a href="http://www.runningprofiles.com/members/index.php?page=message&forum='.$getthreads3['forum'].'&id='.$getthreads3['postid'].'&pagenum=last">'.substr($getthreads3[title], 0, 15);
                  	if (strlen($getthreads3[title]) >= 15)
                        echo '...'; 
                        echo '</a> <br /> on ';gettheTime($dbtime, $time);
         ?>
	 </td>               
	 </tr>
                 <tr class="mainrow">
                  <td>
                    <div class="c1">
                      <?php 
		$getthreads_result = mysql_query("Select * from forumtutorial_posts where parentid='0' and forum = 'QA' ORDER BY lastrepliedto DESC LIMIT 1 ") or
                        die("Could not get threads");
                $getthreads = mysql_fetch_array($getthreads_result);
                        $getlastpost = mysql_fetch_assoc(mysql_query("SELECT * FROM users WHERE Username = '$username' "));
                        printf("<center><img src=\"http://www.runningprofiles.com/images/postforum.%s\" alt=\"My\" /></center>",($getlastpost['QA'] <= $getthreads['lastrepliedto']) ? "gif" : "jpg");
                      ?>
                    </div>
                  </td>

                  <td>
                    <p><a href="index.php?page=forum&forum=QA"><b>Q and A</b></a>
	 	 <?php
                    	      $result = mysql_query("SELECT * FROM useronline WHERE(file='http://www.runningprofiles.com/members/index.php?page=forum&forum=QA')");
                   	      while ($row = mysql_fetch_array($result))
                   		 {
                                                    $last_active = time() - $row['timestamp'];
                   		 }
                   	      if ($last_active < 300 || $result_count > 0)
                   		 {
                                                    echo '(', $result_count, ' viewing)';
                   		 }
                    	?>
	    </p>

                    <p>Post your questions and someone out there will know the answer</p>
                  </td>

                  <td align="center">
                    <?php
                    if ($result = mysql_query("SELECT COUNT(title) as cnt, SUM(numreplies) as sm FROM forumtutorial_posts WHERE `forum` = 'QA'"))
                    {
                                                    if (mysql_num_rows($result))
                                                    {
                                                                                    $row = mysql_fetch_assoc($result);
                                                                                    $topic = $row['cnt'];
                                                                                    $posts = $row['cnt'] + $row['sm']; ?>
			<?php echo $topic; ?> Topics
		<?php echo $posts; ?> Posts</pre>
		<?php 
					    }
                    }

               		 ?>
                  </td>

                  <td>
	  <?php 
		$getthreads3 = mysql_fetch_assoc(mysql_query("Select * from forumtutorial_posts where parentid='0' and forum = 'QA' ORDER BY lastrepliedto DESC LIMIT 1"));
                $dbtime = $getthreads3['lastrepliedto'];
                 	$time = date("F j, Y, g:i a", $dbtime);
                  ?>
                  <?php 
		echo '<strong>Last post</strong> by <a href="'.$getthreads3['lastposter'].'">'.$getthreads3['lastposter'].'</a>'; 
	  ?>
		<br /> 
	 <?php echo 'in <a href="http://www.runningprofiles.com/members/index.php?page=message&forum='.$getthreads3['forum'].'&id='.$getthreads3['postid'].'&pagenum=last">'.substr($getthreads3[title], 0, 15);
                  	if (strlen($getthreads3[title]) >= 15)
                        echo '...'; 
                        echo '</a> <br /> on ';gettheTime($dbtime, $time);
	 ?>
	</td>   
                </tr>
              </table>
            </td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
<br />
    <table width="100%" class='loggedin'>
    <tr>
      <td height="55" valign="top">
        <div class="c3"></div>

        <table width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td colspan="2" bgcolor="#99B3B4"><?php $search_query =
            "SELECT ID FROM users";
            $result = mysql_query($search_query) or die(mysql_error());
            $result_count = mysql_num_rows($result);
            $search_query2 = "SELECT postid FROM forumtutorial_posts";
            $result2 = mysql_query($search_query2) or die(mysql_error());
            $result_count2 = mysql_num_rows($result2); ?><strong>Forum Stats!</strong></td>
          </tr>

          <tr>
            <td width="3%"><img src="http://www.runningprofiles.com/images/stats.gif" width="30" height="30" alt="stats" /></td>

            <td width="97%">There are <?php echo $result_count; ?> registered users who have posted a total of <?php echo
            $result_count2; ?> posts.</td>
          </tr>

          <tr>
            <td valign="top"><img src="http://www.runningprofiles.com/images/CHAT.gif" width="30" height="30" alt="stats" /></td>

            <td><?php
            $getusersonline = "SELECT user_id,user FROM useronline 
               WHERE 
             (file LIKE 'http://www.runningprofiles.com/members/index.php?page=message%' or
              file LIKE 'http://www.runningprofiles.com/members/index.php?page=reply%' or
              file LIKE 'http://www.runningprofiles.com/members/index.php?page=mainforums%' or
             file LIKE 'http://www.runningprofiles.com/members/index.php?page=message%' or
              file LIKE 'http://www.runningprofiles.com/members/index.php?page=post%') AND
              timestamp > " . (time() - 900); //grab from sql users on in last 15 minutes
            $getusersonline2 = mysql_query($getusersonline) or die("Could not get users");
            $num = mysql_num_rows($getusersonline2);

            echo "<b>There " . ($num != 1 ? "are" : "is") . " $num user" . ($num != 1 ? "s" :
                                            "") . " currently viewing the forums. </b>";

   
        

            ?><br /></td>
          </tr>

          <tr>
            <td colspan="2" valign="top" bgcolor="#99B3B4"><strong>Icons</strong></td>
          </tr>

          <tr>
            <td colspan="2" valign="top"><img src="http://www.runningprofiles.com/images/postforum2.jpg" alt="" border="0" /> No New Posts<br />
            <img src="http://www.runningprofiles.com/images/postforum2.gif" alt="" border="0" /> New Posts</td>
          </tr>
        </table>
      </td>
    </tr>
  </table><?php $result = mysql_query("SELECT * FROM useronline WHERE(file='http://www.runningprofiles.com/members/index.php?page=mainforum')");
  while ($row = mysql_fetch_array($result))
  {

                                  $last_active = time() - $row['timestamp'];
                                  $onlineuser = $row['user'];
  }

  if ($last_active < 300)
  {
                                  echo $onlineuser;
  }
  ?>












 

 

 

 

 

I hope someone can help me out with this page so im able to apply it to the rwest of my site.

 

Regards,

 

Jarratt

Link to comment
https://forums.phpfreaks.com/topic/198267-how-can-i-cut-down-this-code/
Share on other sites

Okay, you should definitely re-design how you store things in the database. I don't recommend storing URLs like that and querying them. If they ever change, you have to change a lot!

 

You can easily do a few loops. First, store all the forum categories (training, races, general, etc.) in a table.

 

Table forums

id  |  category_name

----------------------------

1  |  training

2  |  races

3  |  general

 

Then in your other table, posts

 

Table posts:

id  |  post_title  |  forum_id  | ...

------------------------------------- ...

1    |  blah          |  1

2    |  testings    |  2

3    |  hm            |  1

 

So the forum_id references the forum id in the forums table. 1 = training, 2 = races, etc..

 

Then when you query it, just query all the forums, then make a query for all the posts in that forum. This can be done in a few loops rather than repeating it manually.

 

I hope this helps.

 

Good luck!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.