Jump to content

First Time Using Arrays


Unholy Prayer

Recommended Posts

Ok, I have all my variables declared with my arrays, but how do I get it to replace the text in included files?  For example:  I used FORUM_ID=>"$fid", to declare the forum's id.  I have a tpl file included after the variables.  How do I get the arrays to be used in the template file so if the contents of the tpl file were <a href='viewforum.php?id=FORUM_ID'>FORUM_NAME</a>?

Link to comment
Share on other sites

Yes, unless you setup a parsing bit.

 

<?php
$tplFileData = '<a href="viewforum.php?id=FORUM_ID">FORUM_NAME</a>';
$fid=1;
$array = array("FORUM_ID" => "$fid", "FORUM_NAME" => "Name");

foreach ($array as $key => $val) {
        $tplFileData = str_replace($key, $val, $tplFileData);
}

print $tplFileData;
?>

Link to comment
Share on other sites

So if my code were:

      $array = array(
                   FORUM_ID=>"$fid",
                   FORUM_NAME=>"$fname",
                   FORUM_DESC=>"$fdescription",
                   FORUM_STATUS=>"$fstatus",
                   FORUM_TYPE=>"$ftype",
                   FLASTPOST_AUTH=>"$lastpostauth",
                   FLASTPOST_DATETIME=>"$lastpostdatetime",
                   FORUM_THREADS=>"$threads"
                   );

                  $template = include('templates/default/forumslist_body.tpl');

 

I would do this?

foreach ($array as $key => $val) {
        $template = str_replace($key, $val, $template);
}

Link to comment
Share on other sites

Give it a try and see if it works.

 

Chances are the include is wrong i would do this:

 

$template = file_get_contents('templates/default/forumslist_body.tpl');

 

I think the include function returns a true or false, not the data.

Link to comment
Share on other sites

That did not work either.  Here is my code. 

<?php
session_start();
require_once('config.php');
$action = $_GET["action"];

$m_time = explode(" ",microtime()); 
$m_time = $m_time[0] + $m_time[1]; 
$starttime = $m_time;

include('constants.php'); 

if(!$_SESSION['username']) {
    $user = "Guest";
} else {
    $user = $_SESSION['username'];
    $uidquery = mysql_query("SELECT * FROM vbb_members WHERE username='".$_SESSION['username']."'");
    $get = mysql_fetch_array($uidquery);
    $uid = $get["uid"];
    $last_login = $get["last_login"];
    $perm = $get["perm"];
}
include('templates/default/header_body.tpl');

if($action = ""){

  $viewing = mysql_query("UPDATE vbb_members SET viewing='Index' WHERE uid='$uid'") or die ("Error: ".mysql_error());

}

//If the user is viewing the regular index...
if($action == ""){

    //Display category head
    echo "<table align='center' width='75%' cellspacing='1' cellpadding='1' border='0'>
    <tr>";

      //Fetch categories
       $categories = mysql_query("SELECT * FROM vbb_categories");

              while($c=mysql_fetch_array($categories))

               {

                  //Display categories in loop
                   $cid=$c["cid"];
                   $cname=$c["cname"];
                   $cdescription=$c["cdescription"];
                   $cstatus=$c["cstatus"];

                     include('templates/default/cat_body.tpl');

          //Fetch forums for the current category
          $forums = mysql_query("SELECT * FROM vbb_forums where cid = $cid");
     
                while($f=mysql_fetch_array($forums))

                   {

                      $fid=$f["fid"];
                      $cid=$f["cid"];
                      $fname=$f["fname"];
                      $fdescription=$f["fdescription"];
                      $lastpostauth=$f["lastpost_auth"];
                      $lastpostdatetime=$f["lastpost_datetime"];
                      $fstatus=$f["fstatus"];
                      $ftype=$f["ftype"];

                      $views=$f["views"];
                       $replies=$f["replies"];

  $lastpost = mysql_query("SELECT * FROM vbb_threads WHERE fid = $fid LIMIT 1");

    $lp=mysql_fetch_array($lastpost);

          $lastthreadsubject=$lp["tsubject"];
     
     $get_threads = "SELECT * FROM vbb_threads where fid = $fid";
        $threads_result = mysql_query($get_threads);
           $threads = mysql_num_rows($threads_result);

               $get_posts = "SELECT * FROM vbb_threads WHERE tdatetime > $last_login AND fid = $fid";
        $posts_result = mysql_query($get_posts);
           $newposts = mysql_num_rows($posts_result);

          $subforums = mysql_query("SELECT * FROM vbb_subforums where fid = $fid");

          $sf=mysql_fetch_array($subforums);

                  $sfid=$sf["sfid"];
                  $fid2=$sf["fid"];
                  $sfname=$sf["sfname"];

         $csubforums = "SELECT * FROM vbb_subforums where fid = $fid";
        $c_result = mysql_query($csubforums);
           $numsubs = mysql_num_rows($c_result);     

      $threadquery = "SELECT * FROM vbb_threads WHERE fid='$fid'";
              $threadresult = mysql_query($threadquery);
              $num_threads = mysql_num_rows($threadresult);

    if($num_threads > 0){

      $memberid = mysql_query("SELECT * FROM vbb_members WHERE uid='$uid'");

        $m=mysql_fetch_array($memberid);
              $name=$m["username"];
     
         $threadname = mysql_query("SELECT * FROM vbb_threads WHERE fid='$fid' ORDER BY tid desc LIMIT 1");
              $tn=mysql_fetch_array($threadname);
                 $lastthreadtitle=$tn["tsubject"];

}

  $threadsquery = "SELECT * FROM vbb_threads";
        $threadsresult = mysql_query($threadsquery);
           $numthreads = mysql_num_rows($threadsresult);

             $repliesquery = "SELECT * FROM vbb_replies";
              $repliesresult = mysql_query($repliesquery);
           $numreplies = mysql_num_rows($repliesresult);


         $array = array(
                   "FORUM_ID"=>"$fid",
                   FORUM_NAME=>"$fname",
                   FORUM_DESC=>"$fdescription",
                   FORUM_STATUS=>"$fstatus",
                   FORUM_TYPE=>"$ftype",
                   FLASTPOST_AUTH=>"$lastpostauth",
                   FLASTPOST_DATETIME=>"$lastpostdatetime",
                   FORUM_THREADS=>"$numthreads",
                   FORUM_REPLIES=>"$numreplies"
                   );

                  foreach ($array as $key => $val){
              
        $buildforums = file_get_contents('templates/default/forumlist_body.tpl');
        $buildforums= str_replace($key, $val, $buildforums);

          }

         print $buildforums;

        

    }

  }

   echo "</table>";

       $round = 3;// The number of decimal places to round the micro time to.
$m_time = explode(" ",microtime()); 
$m_time = $m_time[0] + $m_time[1]; 
$endtime = $m_time; 
$totaltime = ($endtime - $starttime); 

$load = round($totaltime,$round);


           }

  if($action == "search"){

  echo "<br><br><table align='center' cellspacing='1' cellpadding='1' border='0'>
  <tr>
     <td align='center' colspan='2' class='header'>Search Results</td>
  </tr><tr>
     <td align='center' class='title_posts'>Thread Name</td>
     <td class='title_posts'>Thread Creator</td>
  </tr><tr>";

  if(empty($_POST['thread'])){

    echo "<td class='light1' colspan='2'>You did not enter anything into the search field!</td>";

   }
// We will need to get the search keyword and put it into a varaible.

     if(isset($_POST['go'])){

   $search = $_POST['thread'];

    $searchResults = mysql_query("SELECT * FROM vbb_threads WHERE tsubject LIKE '$search'");
           $numresults = mysql_num_rows($searchResults);

     if($numresults < 1){

       echo "<td class='light1' colspan='2'>Your search returned $numresults results.</td>"; 

   }

   // This while loop will loop through the results until we get to the end. It will print out the title and
//the description of the returned results.

while($row = mysql_fetch_array($searchResults)){

    $tsubject=$row['tsubject'];
    $tid=$row['tid'];
    $tcreator=$row['tcreator'];

       include('templates/default/searchresults_body.tpl');

   }

   echo "</table>";

}

}

$usersquery = "SELECT * FROM vbb_members";
              $usersresult = mysql_query($usersquery);
              $num_users = mysql_num_rows($usersresult);

include('templates/default/stats_body.tpl');

include('templates/default/footer_body.tpl');

?> 



   

Link to comment
Share on other sites

It's kind of working now, but it's not displaying the html of the tpl file, just the variables.  It doesn't display it in the tables it's supposed to.  I'm using this code instead:

 $array = array(
                   'FORUM_ID' => $fid,
                   'FORUM_NAME' => $fname,
                   'FORUM_DESC' => $fdescription,
                   'FORUM_STATUS' => $fstatus,
                   'FORUM_TYPE' => $ftype,
                   'FLASTPOST_AUTH' => $lastpostauth,
                   'FLASTPOST_DATETIME' => $lastpostdatetime,
                   'FORUM_THREADS' => $numthreads,
                   'FORUM_REPLIES' => $numreplies
                   );

$buildforums = file_get_contents('templates/default/forumlist_body.tpl');

                  foreach ($array as $buildforums) {
    echo htmlspecialchars($buildforums);
              
        }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.