|
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 |
stefaan1 Smarty n00b
Joined: 25 Jul 2003 Posts: 3 Location: Weinviertel/AT
|
Posted: Mon Jul 28, 2003 4:53 pm Post subject: how to handle an array? |
|
|
hi!
I've got some problems with handling an big array. An event calendar I'm working on should be able to show to an event one or more contact options. Data is stored in mysql, I think, there are 3 ways to solve this problem.
Structure looks like this:
Code: |
|- event 1
| +- id
| +- start
| +- end
| +- title
| +- ...
| +- contact
| +- person 1
| | +- name
| | +- phone
| | +- email
| +- person 2
| | +- name
| | +- phone
| | +- email
| +- person n
| +- name
| +- phone
| +- email
|- event 2
| +- id
| +- start
| +- end
| +- title
| +- ...
| +- contact
| +- person 1
| | +- name
| | +- phone
| | +- email
| +- person 2
| | +- name
| | +- phone
| | +- email
| +- person n
| +- name
| +- phone
| +- email
|- event n
+- id
+- start
+- end
+- title
+- ...
+- contact
+- person 1
| +- name
| +- phone
| +- email
+- person 2
| +- name
| +- phone
| +- email
+- person n
+- name
+- phone
+- email
|
All events are stored in one table, contacts in a second one.
In other code I did this with 2 queries, one for "main" (e.g. event) and one for "sub" (e.g. contacts). With some terrible lines of code I got
[php:1:0baa4a7c2d]
$event[0]['title'] => Event 1
$event[0]['start'] => today
$event[1]['title'] => Event 2
$event[1]['start'] => tomorrow
$contact[0][0]['name'] => tom
$contact[0][0]['phone'] => 0555888
$contact[0][1]['name'] => joe
$contact[0][1]['phone'] => 0555488
$contact[1][0]['name'] => andy
$contact[1][0]['phone'] => 0555888
[/php:1:0baa4a7c2d]
and with section I looped it through. Easy when everything is sorted by id (or some data, which you have in both queries).
Second thought was about one query, having one event twice ore more often with the same data except contacts like
[php:1:0baa4a7c2d]
$event[0]['title'] => Event 1
$event[0]['start'] => today
$event[0]['contact'] => tom
$event[1]['title'] => Event 1
$event[1]['start'] => today
$event[1]['start'] => joe
$event[2]['title'] => Event 2
$event[2]['start'] => tomorrow
$event[2]['start'] => andy
[/php:1:0baa4a7c2d]
But is it possible to handle this "doubles" with smarty loopings?
Third idea was of
[php:1:0baa4a7c2d]
$event[0]['title'] => Event 1
$event[0]['start'] => today
$event[0]['contact'][0]['name'] => tom
$event[0]['contact'][0]['phone'] => 0555888
$event[0]['contact'][1]['name'] => joe
$event[0]['contact'][1]['phone'] => 05458888
$event[1]['title'] => Event 2
$event[1]['start'] => tomorrow
$event[1]['contact'][0]['name'] => joe
$event[1]['contact'][0]['phone'] => 0557452
[/php:1:0baa4a7c2d]
But no idea how to create such an array in php or display it with smarty.
Any ideas or code to one of my "solutions"?
Thanks.
Stefan |
|
Back to top |
|
roguetech Smarty n00b
Joined: 24 Jul 2003 Posts: 3
|
Posted: Tue Jul 29, 2003 2:10 am Post subject: |
|
|
Had the same problem, and have struggled with it all day, but I think I solved it...
In PHP Code: | $result = $db->sql_query($query);
$i=0;
while ($line = $db->sql_fetchrow($result)) {
if($i=0){
$lineoutput=array(array('field1'=>$line["field1"], 'field2'=>$line["field2"], 'field3'=>append_sid("index.php?page=".$line["field3"])));
} else {
$lineoutput2=array(array('field1'=>$line["field1"], 'field2'=>$line["field2"], 'field3'=>append_sid("index.php?page=".$line["field3"])));
}
$lineoutput=array_merge($lineoutput,$lineoutput2);
$i++;
}
$smarty->assign('lineoutput',$lineoutput); |
In the template Code: | {section name=loopit loop=$lineoutput}
{if $lineoutput[loopit].field1 == 1}
<i>
{/if}
<a href="{$line[loopit].field3}">
{$line[loopit].field2}
</a>
{if $line[loopit].field1 == 1}
</i>
{/if}
{/section} |
Put the extra stuff in there to further illustrate how it works; hope it doesn't confuse you. The real trick here is the array_merge statement - I know it can be done better than the clunky if($i==0) thing, but...it works.
I would like to know if this is the "correct" way to do it, since it seems this would be deadly on a large data-set, with dumping each result row to an array, merging it with an array holding the previous result rows, then dumping the whole thing to Smarty's array, then looping through it again to actually display it. _________________ John's Mediocre Pages
http://www.roguetech.org |
|
Back to top |
|
stefaan1 Smarty n00b
Joined: 25 Jul 2003 Posts: 3 Location: Weinviertel/AT
|
Posted: Wed Jul 30, 2003 4:36 pm Post subject: |
|
|
hi,
my way, to combine 2 queries:
[php:1:75bba79e13]<?php
sql_query("
select tbl_artikel.id as artikel_id,
tbl_artikel.bestnr,
tbl_artikel.foto,
tbl_artikel.beschreibung,
tbl_eigenschaften.id,
tbl_eigenschaften.main_id,
tbl_eigenschaften.groesse,
tbl_eigenschaften.preis
from tbl_artikel, tbl_gruppen, tbl_eigenschaften
where (tbl_artikel.gruppe=$gruppen[0] $where_gruppen)
and tbl_artikel.gruppe=tbl_gruppen.id
and tbl_eigenschaften.main_id = tbl_artikel.id and tbl_artikel.visible='Y'
order by main_id asc");
if($sql_count > 0)
{
$i = 0; // 1. Array
$j = 0; // 2. Array
// $temp_id = $main_id[0];
while($sql_data = mysql_fetch_array($sql_result))
{
if($sql_data['artikel_id'] == $kerzen[$i]['id']) {
$eigenschaften[$i][$j]['bestnr'] = $sql_data['bestnr'];
$eigenschaften[$i][$j]['foto'] = $sql_data['foto'];
$eigenschaften[$i][$j]['beschreibung'] = $sql_data['beschreibung'];
$eigenschaften[$i][$j]['groesse'] = $sql_data['groesse'];
$eigenschaften[$i][$j]['preis'] = $sql_data['preis'];
// echo $i . " " . $j . " " . $sql_data['artikel_id'] . " " . $sql_data['preis'] . " " . "<br>";
$j++;
$k = 0;
}
else {
$k=0;
// $i++;
// if($j <= $sql_data['artikel_id']) $j = 0;
if($sql_data['artikel_id'] == $kerzen[$i]['id'] +1 ) {
if($k == 0) $j = 0;
$i++;
$eigenschaften[$i][$j]['bestnr'] = $sql_data['bestnr'];
$eigenschaften[$i][$j]['foto'] = $sql_data['foto'];
$eigenschaften[$i][$j]['beschreibung'] = $sql_data['beschreibung'];
$eigenschaften[$i][$j]['groesse'] = $sql_data['groesse'];
$eigenschaften[$i][$j]['preis'] = $sql_data['preis'];
// echo "<b>" . $i . " " . $j . " " . $sql_data['artikel_id'] . " " . $sql_data['preis'] . " " . "</b><br>";
// $j++;
}
else {
if($k == 0) $j = 0;
$i++; // = $sql_data['artikel_id'] - 1;
$eigenschaften[$i][$j]['bestnr'] = $sql_data['bestnr'];
$eigenschaften[$i][$j]['foto'] = $sql_data['foto'];
$eigenschaften[$i][$j]['beschreibung'] = $sql_data['beschreibung'];
$eigenschaften[$i][$j]['groesse'] = $sql_data['groesse'];
$eigenschaften[$i][$j]['preis'] = $sql_data['preis'];
// echo "<i>" . $i . " " . $j . " " . $sql_data['artikel_id'] . " " . $sql_data['preis'] . " " . "</i><br>";
}
$j++;
$k = 1;
}
}
}
?>[/php:1:75bba79e13]
Not brilliant, but it works...
Stefan |
|
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
|