|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
w|six-nine Smarty n00b
Joined: 12 Jan 2004 Posts: 3
|
Posted: Mon Jan 12, 2004 2:11 pm Post subject: SQL query SELECT DISTINCT and a problem with nested sections |
|
|
Well, I'm spanish, and my english is very bad...
I have a problem when i want show a query with a clausule DISTINCT:
this is my table (`messages`):
Code: | +----+--------+---------------------------+------------+
| id | name | text | date |
+----+--------+---------------------------+------------+
| 1 | juan | hola, hay alguien ahi? | 02/01/2004 |
+----+--------+---------------------------+------------+
| 2 | pedro | si, toi yo, jeje. | 02/01/2004 |
+----+--------+---------------------------+------------+
| 3 | juan | pos yo me tengo que pirar | 02/01/2004 |
+----+--------+---------------------------+------------+
| 4 | sergio | buenas tardes a todos!!! | 03/01/2004 |
+----+--------+---------------------------+------------+
| 5 | sergio | maņana nos vamos a cenar | 05/01/2004 |
+----+--------+---------------------------+------------+ |
and my code:
Code: | <?
// connection and initialize the SmartyClass
$query = "SELECT DISTINCT `name` FROM `messages`";
$resp = mysql_query($query);
while ($datos = mysql_fetch_array($resp)) {
$smarty->append("DATOS", array(
"NAME" => $datos[name]
));
$query = "SELECT * FROM `messages` WHERE `name`='$datos[name]'";
$resp2 = mysql_query($query);
while ($datos2 = mysql_fetch_array($resp2)) {
$smarty->append("DATOS2", array(
"DATE" => $datos2[date],
"TEXT" => $datos2[text]
));
}
}
?> |
and my template:
Code: | {section name=autor loop=$DATOS}
<h3>{$DATOS[autor].NAME}</h3>
{section name=msj loop=$DATOS2}
<p>{$DATOS2[msj].DATE}</p>
<p>{$DATOS2[msj].TEXT}</p>
{/section}
<hr />
{/section} |
but don't work as I want. I am beginner with Smarty, but the PHP code is correct, because i replace the smarty append x echo, de code result succes.
I need help, please. I think that the error is in the way of make the arrays(), but I don't know how make the second array() include in the first...
I don't explain very well, sorry ;) |
|
Back to top |
|
Gerald Smarty Regular
Joined: 26 Nov 2003 Posts: 53 Location: Lyon [France]
|
Posted: Mon Jan 12, 2004 3:44 pm Post subject: |
|
|
I would do something like this.
Code: |
// connection and initialize the SmartyClass
$query = "SELECT DISTINCT `name` FROM `messages`";
$resp = mysql_query($query);
while ($datos = mysql_fetch_array($resp)) {
$new_line = array ();
$new_line['NAME'] = $datos['name'];
$query = "SELECT * FROM `messages` WHERE `name`='$new_line['NAME']'";
$resp2 = mysql_query($query);
while ($datos2 = mysql_fetch_array($resp2)) {
$new_line['DATAS'][] = array ('DATE'=>$datos2['date'], 'TEXT'=>$datos2['text']);
}
$smarty->append ('DATOS', $new_line);//stores the new line.
}
?>
|
Now your template.
Code: |
{foreach from=$DATOS item=line}
<h3>{$line.NAME}</h3>
{foreach from=$line.DATAS item=infos}
<p>{$infos.DATE}</p>
<p>{$infos.TEXT}</p>
{/foreach}
{/foreach}
|
I'm not sure I got it well, but this should work.
EDIT: Codewise, you may want to do only one request to get name & messages. While writing my answer, I hadn't realize there was only one table. |
|
Back to top |
|
w|six-nine Smarty n00b
Joined: 12 Jan 2004 Posts: 3
|
Posted: Mon Jan 12, 2004 4:31 pm Post subject: |
|
|
don't work it :(
Parse error: parse error, unexpected T_STRING, expecting ']' in c:\Apache2\htdocs\toxico\plugins\smarty\templates_c\pruebaphp.html on line 4
and this is the template_compiled:
[php:1:b3b74fefb0]<?php /* Smarty version 2.5.0, created on 2004-01-12 17:25:06
compiled from pruebaphp.html */ ?>
<?php if (count((array)$this->_tpl_vars['DATOS'])):
foreach ((array)$this->_tpl_vars['DATOS'] as $this->_tpl_vars['$this->_tpl_vars['line']']): //this is the line 4
?>
<h3><?php echo $this->_tpl_vars['line']['NAME']; ?>
</h3>
<?php if (count((array)$this->_tpl_vars['line']['DATAS'])):
foreach ((array)$this->_tpl_vars['line']['DATAS'] as $this->_tpl_vars['$this->_tpl_vars['infos']']):
?>
<p><?php echo $this->_tpl_vars['infos']['DATE']; ?>
</p>
<p><?php echo $this->_tpl_vars['infos']['TEXT']; ?>
</p>
<?php endforeach; endif; ?>
<?php endforeach; endif; ?>[/php:1:b3b74fefb0]
i don't know why insert in a simple quot this... |
|
Back to top |
|
Gerald Smarty Regular
Joined: 26 Nov 2003 Posts: 53 Location: Lyon [France]
|
Posted: Mon Jan 12, 2004 4:41 pm Post subject: |
|
|
Weird, I've tested this exact template on my system......
{foreach from=$DATAOS item=line}
<h3>{$line.NAME}</h3>
{foreach from=$line.DATAS item=infos}
<p>{$infos.DATE}</p>
<p>{$infos.TEXT}</p>
{/foreach}
{/foreach}
Did you put a "$" in "item=hereline" ? (wich is wrong) |
|
Back to top |
|
w|six-nine Smarty n00b
Joined: 12 Jan 2004 Posts: 3
|
Posted: Mon Jan 12, 2004 5:12 pm Post subject: |
|
|
YaAahoOoOoOo...!!!! thanx, thanx, thanx, thanx... XDDD |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|