Jump to content

PHP while loop + template help


cheater99

Recommended Posts

Ok, i am designing my new website design and it's all going to be dynamic but i've come across a major problem. Basically, i'm filling a template file with content from a database which is fine until i need to use multiple tables for one section. If you look at the images below, you can see what I'm trying to achieve and what i currently am achieving.

The two images showing what i am trying to achieve are attached. The image named Current is what the code currently generates and the other is what i aim for the code to generate. These can also be found at the imageshack links below.

Current                  :- http://img525.imageshack.us/i/current1.png/

What i am aiming for:- http://img101.imageshack.us/i/perfect1.png/

 

 

My code is as follows.

<?php
  // Connect to database
  include("config.connection.php");
  include("open.connection.php");
  
  // Variables
  $per_page    = $side_articles;
  $links_per_page  = '5';
  $sort      = $side_order;

  // Get Offset
  $offset = $_GET['s'];
  if(!$offset) $offset = 1;
  $limit  = ($offset - 1) * $per_page;

  // Get File Count
  //($count = mysql_query('SELECT * FROM side_section',$conn)) ? $queries++ : die(mysql_error());
  //$total = mysql_num_rows($count);
  $x = 1;
  // 
  // Get side
    ($section_extraction  = mysql_query('SELECT * FROM side_section', $conn)) ? $queries++ : die(mysql_error());

    // Side Has Rows
    if (mysql_num_rows($section_extraction) < 1)
    {
        $sidesectionlist_loop  = il_template_opener('side.section.list.no.side.tpl');
    }
    else
    {
        // Print Files
        while($side  = mysql_fetch_array($section_extraction))
        {
            // Loop Template Replace
            $t_r_array    = Array();
            $t_r_array[]  = array('{SIDE_LIST_SID}', $side['ID']);
            $t_r_array[]  = array('{SIDE_LIST_SECTION_TITLE}', $side['Title']);

            // Second Query
            ($section_info = mysql_query("SELECT * FROM side_title where SectionID='$side[iD]'", $conn)) ? $queries++ : die(mysql_error());

            while($side2=mysql_fetch_array($section_info))
            {
                // Loop Template Replace
                $t_r_s_array    = Array();
                $t_r_s_array[]  = array('{SIDE_LIST_IID}', $side2['ID']);
                $t_r_s_array[]  = array('{SIDE_LIST_ITEM_TITLE}', $side2['Title']);
                $t_r_s_array[]  = array('{SIDE_LIST_FILE}', $side2['File']);
                $t_r_s_array[]  = array('{SIDE_LIST_ARTICLE}', $side2['SectionID']);

			$sideitemlist_loop .= il_template_array_replace('side.item.list.loop.tpl', $t_r_s_array);                
            }

            $sidesectionlist_loop  .= il_template_array_replace('side.section.list.loop.tpl', $t_r_array);
        }
    }



  // Paging Links
  //$sidelist_paging  = paging('side.php?s=','s',$total,$per_page,$links_per_page,1,1,1,1,0,0);

  // Main Template Replace and Print
  $t_r_f_array  = Array();
  $t_r_f_array[]  = array('{SIDE_LIST_LOOP}',$sidesectionlist_loop);
  $t_r_f_array[]  = array('{SIDE_LIST_ITEMLOOP}',$sideitemlist_loop);

  $sidelist_output  = il_template_array_replace('./'.$theme.'/'.$protocol.'side.list.tpl',$t_r_f_array);
  print($sidelist_output);
  //print($sidesectionlist_loop);
  //il_output('side',$sidelist_output,
  //'
  //<script type="text/javascript" src="scripts/misc.js"></script>
  //');
  //include("close.connection.php");

?>

 

The structure of the two tables is as follows:

Side Section

ID
Title

Side Title

ID
Title
File
SectionID

 

This is really holding me back because without this, i lack a way of making the rest of my pages functional, any help will be greatly appreciated.

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/175719-php-while-loop-template-help/
Share on other sites

Well, I'm having a hard time comparing your code to the images you posted.  I'm wondering if the current1.jpg is an actual screen shot of what is being displayed or an image you created to illustrate the problem.

 

I think this is a mater of emptying your array before you start a new loop but I'm not sure.

 

Handy PHP

echo '<pre>'."\r\n";
print_r($t_r_array, true);
echo "\r\n";
print_r($t_r_s_array, true);
echo "\r\n";
print_r($t_r_f_array, true);
echo "\r\n";
echo '</pre>';

 

Put that at the end and paste back the results, also show your il_template_array_replace() userfuncs.

Sorry this reply is so late,

 

Handy PHP: current is what the php currently generates and perfect is what ive made in html to show what should be displayed.

 

Andy-H: I added that and it printed nothing extra, it's just the same as it is without. My functions file is below.

<?php
function il_template_opener($template_soruce) {
    $fh_template   = fopen($template_soruce,'r') or die($php_errormsg);
    $template    = fread($fh_template,filesize($template_soruce));
    fclose($fh_template);
    return $template;
  }

  // Inter-Link Template Parser
  function il_template_array_replace($template_soruce,$template_replace_array) {
    $template    = il_template_opener($template_soruce);

    $template_array_length  = count($template_replace_array);

    for ($i = 0; $i < $template_array_length; $i++) {
      $template  = str_replace($template_replace_array[$i][0],$template_replace_array[$i][1],$template);
    }
    return $template;
  }
?>

 

I think it's a problem with $t_r_array being filled with everything from the 'Side Title' table and then put under the sections all together, rather than section by section.

 

Any more help will be appreciated!

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.