Jump to content

using Smarty 2 to show loop in template


Go to solution Solved by Twysted,

Recommended Posts

Ok so i have made the page to load the data and i have my tpl file loading i just can't figure out for the life of me why it is not displaying correctly.

 

what it is displaying

fHmfhMH.png

php for the page

<?php
if($Auth->isLogged())
{
	$aid = $Auth->loggedAid();
	$limit = 10;
	//grab available PTC links from db output in json
	$db->select('*', 'ptc', 'active = 1');
	if($db->returned_rows >= 1)
	{
		$username = $Auth->loggedUsername();
		$time = time();
		//$row = $db->fetch_assoc_all();
		$smarty->assign('row', $db->fetch_assoc());
			
		$smarty->assign('username', $username);
		$smarty->assign('time', $time);
		
	}
	else
	{
		//set var not to display ptc ads
		$smarty->assign("row", "No PTC are available");
	}
}
$smarty->display('ptc_view.tpl');
?>

tpl file

<!-- START content-container -->
      <section id="content-container" class="clearfix">
         <div id="main-wrap" class="clearfix">
            <div class="page_content">
            	<table class="table table-hover">
            		<tr>
            			<th>Ad Link</th>
            			<th>Earnings | Points</th>
            			<th>Times Completed</th>
            			<th>Total Visits</th>
            			<th>Date Added</th>
            		</tr>
                	{foreach key=pid item=ri from=$row}
   						<tr>
   							<td><a href="Pages/ptcview/{$ri[0]}">{$ri[1]}</a><br/>{$ri[3]}</td>
   							<td>{$ri[5]} | {$ri[6]} pts</td>
   							<td>{$ri[8]}</td>
   							<td>{$ri[7]}</td>
   							<td>{$ri[13]}</td>
   						</tr>
   					{foreachelse}
   						<tr>
   							<td colspan="5">Sorry there are no PTC ads available right now</td>
   						</tr>
   					{/foreach}

can someone please show me where i have gone wrong?

 

BTW the db table does exist and there is 1 record in it.

Link to comment
https://forums.phpfreaks.com/topic/292431-using-smarty-2-to-show-loop-in-template/
Share on other sites

I have fixed the php and it is showing there should be a row but it's not showing the data.

 

i have not changed the tpl file anymore so what you see above is how it still is but here is the update i made to the php and also an image showing what i see.

 

php

<?php
if($Auth->isLogged())
{
	$aid = $Auth->loggedAid();
	$limit = 10;
	$row = array();
	$db->query('SELECT * FROM ptc WHERE active = 1');
	if($db->returned_rows >= 1)
	{
		$username = $Auth->loggedUsername();
		$time = time();
		$row = $db->fetch_assoc_all();
		$smarty->assign('row', $row);
			
		$smarty->assign('username', $username);
		$smarty->assign('time', $time);
		
	}
	else
	{
		//set var not to display ptc ads
		$smarty->assign("row", "No PTC are available");
	}
}
$smarty->display('ptc_view.tpl');
?>

$row has the entire recordset, of which, you are iterating through it.

 

The first iteration has record 0 of the recordset, and the value is an associated array - namely the names of the columns and the data in that column for that row.

 

Does Smarty2 allow access to an associated (hash) array's elements by an index integer? Or must we use the hash key names (the column names of the table)?

$row has the entire recordset, of which, you are iterating through it.

 

The first iteration has record 0 of the recordset, and the value is an associated array - namely the names of the columns and the data in that column for that row.

 

Does Smarty2 allow access to an associated (hash) array's elements by an index integer? Or must we use the hash key names (the column names of the table)?

http://www.smarty.net/docsv2/en/language.variables.tpl#language.variables.assoc.arrays

http://www.smarty.net/docsv2/en/language.variables.tpl#language.variables.array.indexes

Please do not think that one can access an associative array by a positional index integer, nor an indexed array by anything other than a positional index integer.

 

Try to use the column names. You should have some sort of idea what they are to line up with the HTML table's column headings.

 

If you truly cannot know this, then you can convert the associative array into an indexed array:

{foreach key=pid item=ri from=$row}{$ri = array_values($ri)}

  • Like 1

Please do not think that one can access an associative array by a positional index integer, nor an indexed array by anything other than a positional index integer.

 

Try to use the column names. You should have some sort of idea what they are to line up with the HTML table's column headings.

 

If you truly cannot know this, then you can convert the associative array into an indexed array:

{foreach key=pid item=ri from=$row}{$ri = array_values($ri)}

I will try your suggestion i have tried naming them but it still doesn't work.

 

that didn't work very well did grab data but here is a screenshot

fIBE6Ng.png

Edited by Twysted

I will try your suggestion i have tried naming them but it still doesn't work.

 

that didn't work very well did grab data but here is a screenshot

fIBE6Ng.png

after examining it for some time i have noticed that it is splitting the results between all the tables. I am lost as to how this is happening. some help would be appreciated.

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.