Smarty Loop Help

Smarty is a Saddist

Page 1 of 1

5 Replies - 1561 Views - Last Post: 20 January 2011 - 01:43 AM Rate Topic: -----

#1 iMundy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-September 09

Smarty Loop Help

Posted 14 September 2009 - 01:00 PM

Hi,

I can't get it to display the data from my SQL table called "ig_servers". basically i just want to display everything in it. I'm new to php and even newer to smarty so I must be missing something huge. The closet i came following other forum posts was having the first character displayed in every field.


The actual php part worked when I wasn't using smarty. But I don't want to violate the tho shall not mix presentation and business logic together rule.

Any suggestions?


Script



$result = $mysqli->query("SELECT * FROM ig_servers") or mysqli_error('Theres been a horrible horrible error probably relating to the SQL query');
if ($result->num_rows >= 1)
{
	while ($row=$result->fetch_array(MYSQL_ASSOC))
	{
		$data = array(
			"ig_sid"=>$row['ig_sid'],
			"ig_masterserverip"=>$row['ig_masterserverip'],
			"ig_serverip"=>$row['ig_serverip'],
			"ig_uid"=>$row['ig_uid']);
			
	}
	$smarty->assign('data', $data);

}

$smarty->display('addserver.tpl');

if(isset($_POST['submit'])){
 
//Will add stuff here after sleep.

}

?>




Template Schtuff:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

{section name="id" loop="data"}
<li>{$data[id]} {$data[id]} {$data[id]} : {$data[id]}</li><br>
{/section}


	   
</body>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: Smarty Loop Help

#2 przemass  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 166
  • Joined: 18-July 09

Re: Smarty Loop Help

Posted 14 September 2009 - 01:18 PM

{foreach from=$data item=row}
<li>{$row.ig_sid}</li>
{/foreach}


Was This Post Helpful? 1

#3 iMundy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-September 09

Re: Smarty Loop Help

Posted 15 September 2009 - 05:56 PM

Hey, Thanks

So it shows me the information, but weirdly enough, it only shows the first character of the IP addresses fields. Does this have to do with the periods in the IP format?

Anyone have any ideas how this could be happening. I tried using the HTML special characters in the SQL table, to no avail.
Was This Post Helpful? 0
  • +
  • -

#4 przemass  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 166
  • Joined: 18-July 09

Re: Smarty Loop Help

Posted 16 September 2009 - 03:57 AM

{foreach from=$data item=row}
<li>{$row.ig_sid} {$row.ig_masterserverip} {$row.ig_serverip} {$row.ig_uid}</li>
{/foreach}

and
$data = array(
			"ig_sid"=>$row['ig_sid'],
			"ig_masterserverip"=>$row['ig_masterserverip'],
			"ig_serverip"=>$row['ig_serverip'],
			"ig_uid"=>$row['ig_uid']);

change to
$data[] = array(
			"ig_sid"=>$row['ig_sid'],
			"ig_masterserverip"=>$row['ig_masterserverip'],
			"ig_serverip"=>$row['ig_serverip'],
			"ig_uid"=>$row['ig_uid']);

Was This Post Helpful? 1

#5 flipmedia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-January 11

Re: Smarty Loop Help

Posted 19 January 2011 - 11:41 PM

Hi pals,

so in essence it can conclude code as:
smartyindex.php
=================
<?php

require('Smarty.class.php');
$smarty = new Smarty;

$smarty->template_dir = 'C:/wamp/www/smarty/templates';
$smarty->config_dir = 'C:/wamp/www/smarty/config';
$smarty->cache_dir = 'C:/wamp/smarty/cache';
$smarty->compile_dir = 'C:/wamp/smarty/templates_c';

//db connection
$con = mysql_connect("localhost","root","") or die("connection Error");
mysql_select_db("data",$con) or die("database selection Error");

//query
$result = mysql_query("SELECT * FROM ig_servers",$con) or mysql_error('Theres been a horrible horrible error probably relating to the SQL query');
if (mysql_num_rows($result) >= 1)
{
	while ($row=mysql_fetch_array($result,MYSQL_ASSOC))
	{
		//echo "enter fetch";
		$data[] = array(
			"ig_sid"=>$row['ig_sid'],
			"ig_masterserverip"=>$row['ig_masterserverip'],
			"ig_serverip"=>$row['ig_serverip'],
			"ig_uid"=>$row['ig_uid']);
			
	}
	$smarty->assign('data', $data);

}
$smarty->display('addserver.tpl');
?>


and Template as :
addserver.tpl
==============
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Usage of Looping in Smarty</title>
</head>

<body>

<h2>Reached Here </h2>
{foreach from=$data item=row}
<li>{$row.ig_sid} {$row.ig_masterserverip} {$row.ig_serverip} {$row.ig_uid}</li>
{/foreach}

</body>
</html>



a sample database table details to run

Quote

-
-- Table structure for table `ig_servers`
--

CREATE TABLE `ig_servers` (
`ig_sid` int(9) NOT NULL auto_increment,
`ig_masterserverip` varchar(33) NOT NULL,
`ig_serverip` varchar(33) NOT NULL,
`ig_uid` varchar(9) NOT NULL,
PRIMARY KEY (`ig_sid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;

--
-- Dumping data for table `ig_servers`
--

INSERT INTO `ig_servers` (`ig_sid`, `ig_masterserverip`, `ig_serverip`, `ig_uid`) VALUES
(1, '192.168.78.90', '11.23.677.89', '22'),
(2, '192.168.78.91', '11.24.786.56', '34'),
(3, '192.168.78.95', '11.23.666.89', '37'),
(4, '192.168.78.96', '11.24.786.99', '56'),
(5, '129.99.90.76', '11.90.987.098', '123'),
(6, '22.999.88.90', '11.222.55.78', '78'),
(7, '999.999.00.78', '987.54.212.44', '409');



I attach the Preview of same page as attachment below....

Attached Image


Thanks for all

Regards
Anes P.A
Was This Post Helpful? 0
  • +
  • -

#6 flipmedia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-January 11

Re: Smarty Loop Help

Posted 20 January 2011 - 01:43 AM

Hi pals,
FYI Instead of foreach there in smarty we can also use section like

 
{section name=loop_name loop=$data}
<li>{$data[loop_name].ig_sid} {$data[loop_name].ig_masterserverip} {$data[loop_name].ig_serverip} {$data[loop_name].ig_uid}</li>
{/section}



Thankfully
Anes P.A

This post has been edited by flipmedia: 20 January 2011 - 01:44 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1