Hello,
This is the first time I post in this forum and I'd like to thank everybody for the threads about MVC, DAO, PHP templates and the like... It has been a great help for me! I have been programming in PHP for about one year and with these advices I'm changing my way of scripting.
So, with what I've read I have many question and here is the first one :
I'm curently working on a standard way to transform database results into an array that would be easy to work with in templates.
Here's a simple example :
2 simple tables :
A view that joins the two tablesCode:categories (id, name) articles (id, name, cat_id)
A sample result for select * from v_articles :Code:CREATE VIEW v_articles AS SELECT cat.id AS cat_id, cat.name AS cat_name, art.id AS art_id, art.name AS art_name FROM categories cat LEFT OUTER JOIN articles art ON (cat.id = art.cat_id) ORDER BY cat_name, art_name;
Here is my problem, I'd like to write a function that takes this result and convert it in an array that could be easily used by templatesCode:|cat_id | cat_name | art_id | art_name | +-------+----------+--------+----------+ |1 |'letters' | 1 | 'a' | |1 |'letters' | 2 | 'b' | |2 |'numbers' | 3 | '1' | |2 |'numbers' | 4 | '2' | |2 |'numbers' | 5 | '3' | |3 |'special' | 6 | '(' | |3 |'special' | 7 | '}' | |4 |'other' | NULL | NULL | +-------+----------+--------+----------+
usage example :PHP Code:function break_results ($result_set, $a_breaking)
and the categories array will bePHP Code:$a_breaking = array('cat_id', 'cat_name','articles' => array('art_id', 'art_name') );
$categories = break_results($res, $a_breaking);
This way I can easily work with this array in my templatesPHP Code:array (
1 => array ( 'cat_id' => 1,
'cat_name' => 'letters',
'articles' => array ( 1 => array ('art_id' => 1, 'art_name' => 'a'),
2 => array ('art_id' => 2, 'art_name' => 'b') ) ),
2 => array ( 'cat_id' => 2,
'cat_name' => 'numbers',
'articles' => array ( 3 => array ('art_id' => 3, 'art_name' => '1'),
4 => array ('art_id' => 4, 'art_name' => '2'),
5 => array ('art_id' => 5, 'art_name' => '3') ) ),
3 => array ( 'cat_id' => 3,
'cat_name' => 'special',
'articles' => array ( 6 => array ('art_id' => 6, 'art_name' => '('),
7 => array ('art_id' => 7, 'art_name' => '}') ) ),
4 => array ( 'cat_id' => 4,
'cat_name' => 'other',
'articles' => null )
);
this is a simple example but my function call could be more complex :PHP Code:<?php if ( ! is_array($categories) ) : /* no category */ ?>
<H2>No category</H2>
<?php else : /* at least one category */ ?>
<H2>List of existing categories</H2>
<?php foreach ( $categories as $category ) : /* loop through categories */ ?>
<H3><?php echo ($category['name']) ?></H3>
<?php if ( ! is_array($category['articles'] ) ) : /* no article in the category */ ?>
<H4>No article in this category</H4>
<?php else : /* at least one article */ ?>
<ul>
<?php foreach ( $category['articles'] as $article ) : /* loop through articles */ ?>
<li><?php echo ($article['name'] ?>
<?php endforeach; /* end of loop through articles */ ?>
</ul>
<?php endif; /* end of article block */ ?>
<?php endforeach; /* end loop through categories */ ?>
<?php endif; /* end of category block */ ?>
Where admins may be people allowed to admin a category and users people who can view articles...PHP Code:$a_breaking =
array('cat_id',
'cat_name',
'admins' => array('adm_id',
'adm_name'),
'articles' => array('art_id',
'art_name',
'users' => array('usr_id',
'usr_name')));
I'm currently working on it and think this may be done using classes from the Eclipse Library like RowLoopManipulator, RowLoopManipulatorWatcher and QueryIterator, but I'm a bit puzzled for now...
So if you have already written such function or have a better way to solve this problem I'm waiting for your opinion.
Thanx for reading... and sorry for my english
--c0y0t









Bookmarks