While Loop & Smarty (I don't know how to do it)

A

Anonymous

Guest
It is my first time to use template engines in my coding so I don't know how to make even sample things work.

index.php

Code:
<?php
// Coded by S.Moh'd Ameen | in: 30/03/2006
// e-mail: ameenov@gmail.com
// include smarty
/////////////////////////////////
require '../libs/Smarty.class.php';

$smarty = new Smarty;

$smarty->compile_check = true;
$smarty->debugging = true;

//////////////////////////////
$smarty->assign("Title","News Page");

//////////////////////////////////////// DB CODES
/// include DB connection
include ('includes/config.php');
////

$query = mysql_query("SELECT * FROM news ORDER BY ID DESC") Or die(mysql_error()); 
/////////// check if there is result
$show_num = mysql_num_rows($query);
if ($show_num <=0) {
$smarty->assign("Error","No news add yet!");
}else {
//////////// END check if there is result
while($show = mysql_fetch_array($query)){ 
extract($show);
$Text= stripslashes ($show ['Text']);
///////////// THIS PART I DON"T know if it is right
$smarty->assign("message", array (array("ID" => "$ID", "Text" => "$Text")));
////////////
} // end while
} // end if no result
//////////////////////////////////////// END CODES
$smarty->display('index.tpl');

////////////////////////////////////////////////////////////////////////////
?>


index.tpl

Code:
<title>{$Title}</title>
{$Error}
{section name=sec1 loop=$message}
	ID: {$message[sec1].ID}<br>
	Text: {$message[sec1].Text}<br>
{/section}

It is showing one row ONLY while there is 200 rows in the database!
 
You should replace code
Code:
while($show = mysql_fetch_array($query)){ 
extract($show);
$Text= stripslashes ($show ['Text']);
///////////// THIS PART I DON"T know if it is right
$smarty->assign("message", array (array("ID" => "$ID", "Text" => "$Text")));
////////////
}
with
Code:
$dataIn = Array();
while($show = mysql_fetch_array($query)){ 
extract($show);
// I think you don't need stripslashes here...
$Text= $show ['Text'];
$dataIn[] = array("ID" => "$ID", "Text" => "$Text");
////////////
}
$smarty->assign("message", $dataIn);

It should work (still don't know whats wrong with number 200)...
 
Thank you very much it works perfectly

But I'm getting this notice right now
Code:
Notice: Undefined index: Error in c:\appserv\www\sites\news\templates_c\%%80^80A^80ABA8C3%%index.tpl.php on line 6

It is because the error will show only if there is not result
so I'm facing conditioning problem
 
It is OK i found a solution for that ... correct me if I'm wrong

index.php

Code:
<?php
// Coded by S.Moh'd Ameen | in: 30/03/2006
// e-mail: ameenov@gmail.com
// include smarty
/////////////////////////////////
require 'includes/smarty/Smarty.class.php';

$smarty = new Smarty;

$smarty->compile_check = true;
$smarty->debugging = true;

//////////////////////////////
$smarty->assign("Title","News Page");

//////////////////////////////////////// DB CODES
/// include DB connection
include ('includes/config.php');
////

$query = mysql_query("SELECT * FROM newsORDER BY ID ASC") Or die(mysql_error());
/////////// check if there is result
$show_num = mysql_num_rows($query);
$smarty->assign("show_num","$show_num");
if ($show_num <=0) {
$smarty->assign("Error","No news added yet!");
}else {
//////////// END check if there is result
$dataIn = array();
while($show = mysql_fetch_array($query)){ 
extract($show);
$Text= stripslashes ($show ['Text']);
$dataIn[] = array("ID" => "$ID", "Text" => "$Text");
////////////
}
$smarty->assign("message", $dataIn);
} // end if no result
//////////////////////////////////////// END CODES
$smarty->display('index.tpl');

////////////////////////////////////////////////////////////////////////////
?>


index.tpl
Code:
<title>{$Title}</title>

{if $show_num le "0"}

{$Error} <br />

{else}

{section name=sec1 loop=$message}
ID: {$message[sec1].ID}
Text: {$message[sec1].Text}
{/section}

{/if}
 
Never worked with smarty, but I think that
Code:
$smarty->assign("message", Array());
after
Code:
$smarty = new Smarty;
could also help.
 
Back
Top