Jump to content


Photo

Repeating a Pattern


  • Please log in to reply
3 replies to this topic

#1 homchz

homchz
  • Members
  • PipPipPip
  • Advanced Member
  • 93 posts
  • LocationLee, New Hampshire

Posted 15 May 2006 - 12:43 AM

While creating my blog I have stumbled opun a problem.

I want to keep the code and the design seperate for the most part so I am trying to do this:

Post Function:
function blog_index()
        {
            $this->table = "blog_posts";
            $this->query = mysql_query("SELECT * FROM $this->table ORDER BY 'id' DESC");
        
            if(mysql_num_rows($this->query) > "0")
            {
                while($this->result = mysql_fetch_array($this->query))
                {
                    global $topic, $cat1, $cat2, $post, $img, $image_text, $date, $count;
                     
                    $topic = $this->result['topic'];
                    $cat1  = $this->result['category1'];
                    $cat2  = $this->result['category2'];
                    $post  = nl2br($this->result['post']);
                    $img   = $this->result['image'];
                    $image_text = $this->result['image_text'];
                    $date = $this->result['date'];
                    $count = count($this->result['id']);
                    
                    
                }
            }
        }

Then the function is called in the index page where the design template is called, the template looks like this

<div id="left_col">
        <h2 class="blog_header"><?php print $topic ?></h2>
        <h3 class="blog_date"><?php print $date ?></h3>
        <p><?php print $post ?></p>
 </div>

As you see I call the global variables within the template, but as you can also guess there is no repeating region, so it will only print the first record.

I want to try and keep all the design info in the template and only call my variables when i need them. Is this possible??

Thanks for any info,

Josh



#2 jeremywesselman

jeremywesselman
  • Members
  • PipPipPip
  • Advanced Member
  • 154 posts
  • LocationIndependence, KY

Posted 15 May 2006 - 05:53 AM

I know you're trying to keep the code and the content separate, but you are going to need your loop in your display file. You should load up arrays with the info you need and then loop through them in your display file.

[!--coloro:#990000--][span style=\"color:#990000\"][!--/coloro--]Jeremy[!--colorc--][/span][!--/colorc--]

#3 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 15 May 2006 - 07:19 AM

[a href=\"http://smarty.php.net/\" target=\"_blank\"]Smarty[/a] is a common solution to this problem. With it, you can write design code such as:

<table>
{foreach from=$my_array item=$row}
  <tr>
    <td>{$row.item1}</td>
    <td>{$row.item2}</td>
  </tr>
{/foreach}
</table>


#4 jeremywesselman

jeremywesselman
  • Members
  • PipPipPip
  • Advanced Member
  • 154 posts
  • LocationIndependence, KY

Posted 15 May 2006 - 07:31 AM

[!--quoteo(post=373938:date=May 15 2006, 03:19 AM:name=btherl)--][div class=\'quotetop\']QUOTE(btherl @ May 15 2006, 03:19 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
[a href=\"http://smarty.php.net/\" target=\"_blank\"]Smarty[/a] is a common solution to this problem. With it, you can write design code such as:

<table>
{foreach from=$my_array item=$row}
  <tr>
    <td>{$row.item1}</td>
    <td>{$row.item2}</td>
  </tr>
{/foreach}
</table>
[/quote]

That is pretty much what I said above. Except now you are telling them to use Smarty and learn another syntax.

[!--coloro:#990000--][span style=\"color:#990000\"][!--/coloro--]Jeremy[!--colorc--][/span][!--/colorc--]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users