php-general Digest 9 Mar 2009 12:32:32 -0000 Issue 6001

Topics (messages 289614 through 289632):

Re: Database Abstraction Class
        289614 by: Shawn McKenzie
        289615 by: Micah Gersten
        289616 by: Chris
        289620 by: Paul M Foster

Re: insert array values
        289617 by: PJ

stdin, stdout, stderr, 3
        289618 by: Richard Lynch

PHP Frameworks
        289619 by: HallMarc Websites
        289621 by: 9el
        289622 by: Paul M Foster
        289623 by: Micah Gersten
        289624 by: Micah Gersten

php fop (XSLFO)
        289625 by: Tom Sparks

Re: assign array values to variables
        289626 by: Jim Lucas

Re: DOM recursion
        289627 by: Jochem Maas

Re: Opendir on site root directory?
        289628 by: Stuart

Line Break Problem
        289629 by: Alice Wei
        289630 by: Stuart
        289631 by: Jochem Maas
        289632 by: Alice Wei

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
Micah Gersten wrote:
> Michael A. Peters wrote:
> 
>> Anywhoo, that being said, does anyone have a suggestion for a good
>> database abstraction class?
>>
>> Preferably one that already has decent support for several open source
>> databases?
> 
> Try Doctrine:
> http://www.doctrine-project.org/
> 
> From the website:
> What is Doctrine?
> Doctrine is an object relational mapper (ORM) for PHP 5.2.3+ that sits
> on top of a powerful database abstraction layer (DBAL). One of its key
> features is the option to write database queries in a proprietary object
> oriented SQL dialect called Doctrine Query Language (DQL), inspired by
> Hibernates HQL. This provides developers with a powerful alternative to
> SQL that maintains flexibility without requiring unnecessary code
> duplication.
> 

Wow, a proprietary language that I get to learn on top of the two that
do the job and that I already know!  Bonus!

Not trying to be an ass, I haven't used doctrine, but they need some
marketing help.  My point is that the ability to write queries in a
"proprietary dialect" is not really a "feature".

-- 
Thanks!
-Shawn
http://www.spidean.com

--- End Message ---
--- Begin Message ---
Shawn McKenzie wrote:
> Micah Gersten wrote:
>> Michael A. Peters wrote:
>>
>>> Anywhoo, that being said, does anyone have a suggestion for a good
>>> database abstraction class?
>>>
>>> Preferably one that already has decent support for several open source
>>> databases?
>> Try Doctrine:
>> http://www.doctrine-project.org/
>>
>> From the website:
>> What is Doctrine?
>> Doctrine is an object relational mapper (ORM) for PHP 5.2.3+ that sits
>> on top of a powerful database abstraction layer (DBAL). One of its key
>> features is the option to write database queries in a proprietary object
>> oriented SQL dialect called Doctrine Query Language (DQL), inspired by
>> Hibernates HQL. This provides developers with a powerful alternative to
>> SQL that maintains flexibility without requiring unnecessary code
>> duplication.
>>
> 
> Wow, a proprietary language that I get to learn on top of the two that
> do the job and that I already know!  Bonus!
> 
> Not trying to be an ass, I haven't used doctrine, but they need some
> marketing help.  My point is that the ability to write queries in a
> "proprietary dialect" is not really a "feature".
> 

I think the point is, that you can use a DB engine neutral query
language which improves portability.

-- 
Micah

--- End Message ---
--- Begin Message ---

Wow, a proprietary language that I get to learn on top of the two that
do the job and that I already know!  Bonus!

Not trying to be an ass, I haven't used doctrine, but they need some
marketing help.  My point is that the ability to write queries in a
"proprietary dialect" is not really a "feature".

It is if you write the query once using their language and it supports mysql, postgres, oracle, mssql and sqlite - and it supports all the weird & wonderful stuff (eg limit/offset differences) for you.

I use the ezcomponents stuff to do this (http://ezcomponents.org/docs/tutorials/Database) for mysql/postgres stuff and it works fine.

--
Postgresql & php tutorials
http://www.designmagick.com/


--- End Message ---
--- Begin Message ---
On Sun, Mar 08, 2009 at 08:25:25PM -0500, Shawn McKenzie wrote:

> Micah Gersten wrote:

<snip>

> >
> > Try Doctrine:
> > http://www.doctrine-project.org/
> >
> > From the website:
> > What is Doctrine?
> > Doctrine is an object relational mapper (ORM) for PHP 5.2.3+ that sits
> > on top of a powerful database abstraction layer (DBAL). One of its key
> > features is the option to write database queries in a proprietary object
> > oriented SQL dialect called Doctrine Query Language (DQL), inspired by
> > Hibernates HQL. This provides developers with a powerful alternative to
> > SQL that maintains flexibility without requiring unnecessary code
> > duplication.
> >
> 
> Wow, a proprietary language that I get to learn on top of the two that
> do the job and that I already know!  Bonus!

ROTFL!

Paul

-- 
Paul M. Foster

--- End Message ---
--- Begin Message ---
Ashley Sheridan wrote:
> On Sun, 2009-03-08 at 17:52 -0500, PJ wrote:
>   
>> Ashley Sheridan wrote:
>>     
>>> On Sun, 2009-03-08 at 16:52 -0500, PJ wrote:
>>>   
>>>       
>>>> Well, I have everything more or less under control... just one little
>>>> thingie...
>>>> I was able to get everything working quite well... basically as I
>>>> thought it should go with a little help from my friends, here on the
>>>> list :-)   many of my problems were small omissions and /or typos and
>>>> misplaced brackets and ;
>>>> The only  thing (probably not) that stymies me is how to get the several
>>>> categories printed out for each book . If there is only one category,
>>>> everything is great... but when there are more than one category, the
>>>> book listing repeats the listing of a book as many times as there are
>>>> categories listed for it... fir instance, there are 10 books listed in
>>>> my test table; i have the book_category table listing 1 book as having 1
>>>> category and another just 2 categories. When running the script with the
>>>> category stuff commented out, I get 10 entries... with the categories on
>>>> I get 11 entries with the one that has 2 categories repeated once.
>>>> What should I post to analyze the problem?
>>>>
>>>> Jim Lucas wrote:
>>>>     
>>>>         
>>>>> PJ wrote:
>>>>>       
>>>>>           
>>>>>> Jim Lucas wrote:
>>>>>>         
>>>>>>             
>>>>>>> PJ wrote:
>>>>>>>  
>>>>>>>           
>>>>>>>               
>>>>>>>> 9el wrote:
>>>>>>>>    
>>>>>>>>             
>>>>>>>>                 
>>>>>>>>> On Sat, Mar 7, 2009 at 5:37 AM, PJ <af.gour...@videotron.ca
>>>>>>>>> <mailto:af.gour...@videotron.ca>> wrote:
>>>>>>>>>
>>>>>>>>>     I've done some rethingking and this may be the direction to go:
>>>>>>>>>
>>>>>>>>>     What I need to wind up with is something like this:
>>>>>>>>>
>>>>>>>>>     $sql = "INSERT INTO book_categories ( book_id, category )
>>>>>>>>>        VALUES( '$autoID', '$categoriesID[0]' ),
>>>>>>>>>        ( '$autoID', '$categoriesID[1]' ),
>>>>>>>>>        ( '$autoID', '$categoriesID[2]' ),
>>>>>>>>>        ( '$autoID', '$categoriesID[3]' )
>>>>>>>>>        ( '$autoID', '$categoriesID[4]' )";
>>>>>>>>>
>>>>>>>>>     Does it make sense?
>>>>>>>>>     How do I pass the values to the query?
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> You can run a php loop inside the insert  clause.
>>>>>>>>> But if its already existing record then it woud be UPDATE clause.
>>>>>>>>>
>>>>>>>>>       
>>>>>>>>>               
>>>>>>>>>                   
>>>>>>>> Could I have a small hint as to how to do that. I'll check the manual
>>>>>>>> and search G in the meantime.  Right now, it's Friday night and I'm
>>>>>>>> about to get whammied by the wife... ;-)      have a good night &
>>>>>>>> let's
>>>>>>>> hope for a brighter tomorrow, tomorrow, tomorrowwww
>>>>>>>>
>>>>>>>>     
>>>>>>>>             
>>>>>>>>                 
>>>>>>> To do something like what you are describing above, you would do the
>>>>>>> following:
>>>>>>>
>>>>>>> <?php
>>>>>>>
>>>>>>> # Setup your DB connection etc...
>>>>>>>
>>>>>>>
>>>>>>> # Build insert statement for your book
>>>>>>> $sql = "INSERT INTO books (title, author, etc...) VALUES ('To kill a
>>>>>>> mocking bird', 'Harper Lee', etc...)";
>>>>>>>
>>>>>>> # Execute insert statement
>>>>>>> if ( ( $results = mysql_query($sql, $dblink) ) !== false ) {
>>>>>>>
>>>>>>>     # Grab last insert ID for my thread
>>>>>>>     $last_id = mysql_insert_id($dblink);
>>>>>>>
>>>>>>>     # Check to see if any categories were choosen
>>>>>>>     if ( $categoriesIN ) {
>>>>>>>         $values = array();
>>>>>>>
>>>>>>>         # Loop through each category and build VALUES entry...
>>>>>>>         foreach ( $categoriesIN as $k => $id ) {
>>>>>>>
>>>>>>>             # Build VALUES entry, plus run $id through escape
>>>>>>> function for sanity
>>>>>>>             $values[] = "( {$last_id},
>>>>>>> ".mysql_real_escape_string($id)." )";
>>>>>>>         }
>>>>>>>
>>>>>>>         # Join the VALUES entries just created and separate them
>>>>>>> with a comma
>>>>>>>         $sql = "INSERT INTO book_categories ( book_id, category )
>>>>>>> VALUES " . join(', ', $values);
>>>>>>>
>>>>>>>           
>>>>>>>               
>>>>> At this point, if you echo'ed this.  You would get this
>>>>>
>>>>> INSERT INTO book_categories ( book_id, category ) VALUES ( 1, 3 ), (
>>>>> 1, 5 ), ( 1, 7 ), etc...
>>>>>
>>>>> This basically takes all your inserts that would be done individually
>>>>> and groups them into one insert call.  This helps because it causes
>>>>> the table not to re-index a zillion times.
>>>>>
>>>>> You will still end up with unique row entries for each category and
>>>>> autoID group.  They will not be grouped together in one row as you
>>>>> suggest below.
>>>>>
>>>>>       
>>>>>           
>>>>>>>         # Execute book 2 categories SQL entry
>>>>>>>         if ( ( $results = mysql_query($sql, $dblink) ) === false ) {
>>>>>>>
>>>>>>>             # If it fails, show me why
>>>>>>>             echo 'Book to category entry failed:
>>>>>>> '.mysql_error($dblink);
>>>>>>>         }
>>>>>>>     }
>>>>>>> } else {
>>>>>>>     # Show why my book SQL entry failed
>>>>>>>     echo "Book insert failed: ".mysql_error($dblink);
>>>>>>> }
>>>>>>>
>>>>>>> ?>
>>>>>>>
>>>>>>> That is about as best as can be explained.
>>>>>>>
>>>>>>> BTW - I took away the quotes around your values in your second
>>>>>>> insert statement.  They are numbers and numbers should not have
>>>>>>> quotes around them.
>>>>>>>
>>>>>>> Hopefully this works, or guides you in the direction of the answer
>>>>>>> you seek.
>>>>>>>
>>>>>>>   
>>>>>>>           
>>>>>>>               
>>>>>> Interesting... but, if I understand right (and I am not wet behind the
>>>>>> ears but a newbie nevertheless), this would just enter a series of comma
>>>>>> separated values in one field (the category field; the book_id is taken
>>>>>> care of by an earlier insert followed by
>>>>>> $autoId = mysql_insert_id($result);
>>>>>> which is then inserted in to the book_id)
>>>>>>
>>>>>> To do what you are suggesting could probably be done in a simpler way by
>>>>>> using the implode function... but what I am really trying to do should
>>>>>> give a result like the one in my original post.
>>>>>> Problem is: the insert is not always going to be the same length; that
>>>>>> is there may be 2 categories selected or 3 or 4 so the insert statement
>>>>>> has to be created on the fly. So, far no one has figured out a way to do
>>>>>> it. :-(
>>>>>>
>>>>>> An alternative, and I'm going to try that now is to do a switch or case
>>>>>> or whatever it is and set up 3 or 4 inserts with 1 to 4 VALUES and then
>>>>>> populate them from the array.
>>>>>>
>>>>>> Or another way may be even simpler: implode the array and save to one
>>>>>> field and when querying explode it. :-)
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>         
>>>>>>             
>>>> -- 
>>>> unheralded genius: "A clean desk is the sign of a dull mind. "
>>>> -------------------------------------------------------------
>>>> Phil Jourdan --- p...@ptahhotep.com
>>>> http://www.ptahhotep.com
>>>> http://www.chiccantine.com/andypantry.php
>>>>
>>>>     
>>>>         
>>> If you want to display all the categories for each book, then maybe the
>>> best way is to first retrieve the list of books sans category join (with
>>> any WHERE clauses to limit the result-set how you wish) and then as
>>> you're in the loop outputting the books, perform a quick query to
>>> retrieve the categories for the current book.
>>>   
>>>       
>> You lose me here as I am rather primitive in my understanding of most of
>> this.
>> What do you mean to "retrieve the list of books sans category join?
>> That's what I can do with no problem. Do you mean to save the results in
>> some way to another table or view (or whatever) and then retrieve the
>> categories separately to another table and then join that?
>>
>> At present, I'm echoing the list of books to an html formatted page (via
>> php). As I looooove things complicated, I thought of adding the
>> categories in the output but if it becomes too too much then I can just
>> forget the categories and deal with them on category pages.
>>
>> I thought if might be possible to output the array (at the point just
>> before echoing) imploded as one string or doing a subquery at thisi
>> point; but using a subquery doesn't look too promising.
>>
>>     
>>> The other way I'd do it is to retrieve the full results set (the 11
>>> results one) and put all of that into an multi-dimensional array, such
>>> as:
>>>   
>>>       
>> This seems a little complicated for my peanut brain, but I may have to
>> try it. :-\
>>     
>>> $books = Array();
>>> while($row = mysql_fetch_array($result))
>>> {
>>>   if(!isset($books[$row['id']]))
>>>   {
>>>     $books[$row['id']] = Array();
>>>     $books[$row['id']]['name'] = $row['name'];
>>>     // add more book info here
>>>
>>>     // categories as an array
>>>     $books[$row['id']]['categories'] = Array();
>>>   }
>>>
>>>   // book categories added here in second level of array
>>>   $books[$row['id']['categories'][] = $row['book_cat'];
>>> }
>>>
>>> the $row['id'] here is just the unique id of the row for the books
>>> table.
>>>
>>>
>>> Ash
>>> www.ashleysheridan.co.uk
>>>
>>>
>>>   
>>>       
>> -- 
>> unheralded genius: "A clean desk is the sign of a dull mind. "
>> -------------------------------------------------------------
>> Phil Jourdan --- p...@ptahhotep.com
>>    http://www.ptahhotep.com
>>    http://www.chiccantine.com/andypantry.php
>>
>>
>>     
> Basically, just get the list of books (the list of 10) and do something
> like this:
>
> while($row = mysql_fetch_array($result))
> {
>   $query2 = "SELECT * FROM `book_categories` WHERE `id`={$row['id']}";
>   $result2 = mysql_query($query2);
>   while($row2 = mysql_fetch_array($result2))
>   {
>     // do stuff with categories here
>   }
> }
>
>
> Ash
> www.ashleysheridan.co.uk
>   
I did try something like this but I had the same mistake in the second
query as I had in the first. I think this puts me in a clearer
perspective. I'll get to it tomorrow morning. It's only 10:30pm here...
I'm surprised that there are other crazies like me out there (24/7 birds).
Really appreciate your insights. :-)

-- 
unheralded genius: "A clean desk is the sign of a dull mind. "
-------------------------------------------------------------
Phil Jourdan --- p...@ptahhotep.com
   http://www.ptahhotep.com
   http://www.chiccantine.com/andypantry.php


--- End Message ---
--- Begin Message ---
I have a program sending/receiving data to/from my CLI script using:

0 stdin
1 stdout
2 stderr
3 ??????

0, 1, and 2 are trivial.

How do I access 3?

I tried /dev/fd/3 and failed to open it...

Got no error messages about why it failed to open, it just failed.

Am I missing something?

-- 
Some people ask for gifts here.
I just want you to buy an Indie CD for yourself:
http://cdbaby.com/search/from/lynch



--- End Message ---
--- Begin Message ---
First time caller; long time listener..

 

I have been looking at various PHP MVC frameworks; Limb3, Symphony, Mojavi,
Navigator, WACT, etc. 

I'm looking for any input anyone might have regarding which framework seems
to be the most promising?

 

Thanks,

Marc


--- End Message ---
--- Begin Message ---
-----------------------------------------------------------------------
Use FreeOpenSourceSoftwares, Stop piracy, Let the developers live. Get
a Free CD of Ubuntu mailed to your door without any cost. Visit :
www.ubuntu.com
----------------------------------------------------------------------


On Mon, Mar 9, 2009 at 9:54 AM, HallMarc Websites <m...@hallmarcwebsites.com
> wrote:

> First time caller; long time listener..
>
>
>
> I have been looking at various PHP MVC frameworks; Limb3, Symphony, Mojavi,
> Navigator, WACT, etc.
>
> I'm looking for any input anyone might have regarding which framework seems
> to be the most promising?


YII(Yes It Is) is claiming to be next best framework.


>
>
>
>
> Thanks,
>
> Marc
>
>

--- End Message ---
--- Begin Message ---
On Sun, Mar 08, 2009 at 11:54:01PM -0400, HallMarc Websites wrote:

> First time caller; long time listener..
> 
> 
> 
> I have been looking at various PHP MVC frameworks; Limb3, Symphony, Mojavi,
> Navigator, WACT, etc.
> 
> I'm looking for any input anyone might have regarding which framework seems
> to be the most promising?
> 

Use CodeIgniter. It's about the lightest weight and relatively easy to
understand.

Paul

-- 
Paul M. Foster

--- End Message ---
--- Begin Message ---
HallMarc Websites wrote:
> First time caller; long time listener..
> 
>  
> 
> I have been looking at various PHP MVC frameworks; Limb3, Symphony, Mojavi,
> Navigator, WACT, etc. 
> 
> I'm looking for any input anyone might have regarding which framework seems
> to be the most promising?
> 
>  
> 
> Thanks,
> 
> Marc
> 
> 

I'm currently using Zend PHP Framework + Doctrine ORM.  Symfony has a
little better integration with Doctrine.  I chose the Zend PHP Framework
because of the rapid release schedule and large feature set.

You might want to check the archives as this discussion has come up before.

-- 
Micah

--- End Message ---
--- Begin Message ---
Chetan Rane wrote:
> HI 
> 
> I also was looking for various frameworks and came across a very nice
> framework, which is feature rich as well as very fast
> 
> You can see more details at http://www.yiiframework.com/
> 
> Chetan Dattaram Rane | Software Engineer | Persistent Systems
> chetan_r...@persistent.co.in  | Cell: +91 94033 66714 | Tel: +91 (0832) 30
> 79014
> Innovation in software product design, development and delivery-
> www.persistentsys.com
> 
> 
> 
> -----Original Message-----
> From: Micah Gersten [mailto:news.php....@micahscomputing.com] 
> Sent: Monday, March 09, 2009 9:52 AM
> To: php-gene...@lists.php.net
> Subject: [PHP] Re: PHP Frameworks
> 
> HallMarc Websites wrote:
>> First time caller; long time listener..
>>
>>  
>>
>> I have been looking at various PHP MVC frameworks; Limb3, Symphony,
> Mojavi,
>> Navigator, WACT, etc. 
>>
>> I'm looking for any input anyone might have regarding which framework
> seems
>> to be the most promising?
>>
>>  
>>
>> Thanks,
>>
>> Marc
>>
>>
> 
> I'm currently using Zend PHP Framework + Doctrine ORM.  Symfony has a
> little better integration with Doctrine.  I chose the Zend PHP Framework
> because of the rapid release schedule and large feature set.
> 
> You might want to check the archives as this discussion has come up before.
> 

Please keep on list by hitting reply-all.  Someone else already
mentioned yii framework.
-- 
Micah

--- End Message ---
--- Begin Message ---
is there a php Fop (XSLFO)?
if not are there any that are not java based and can be run o a website that 
has php support only?


tom_a_sparks

Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html


      Stay connected to the people that matter most with a smarter inbox. Take 
a look http://au.docs.yahoo.com/mail/smarterinbox

--- End Message ---
--- Begin Message ---
PJ wrote:
Jochem Maas wrote:
PJ schreef:
Seems it should be simple, but how does one extract values from an array
and assign them to a variable?
foreach ($categoriesIN as $category) {
1. if $categoriesIN comes from a POST, use $_POST['categoriesIN'] instead.
It does, but results are the same. Maybe it has something to do with
SESSIONS.
better yet use the filter extension (part of the core) to retrieve the
input.

2. validate & santize your input (the reason to use the filter extension)
I imagine it is duplicate work but since I am learning and want to
concentrate on 1 thing at a time, I am leaving the sanitizing part as
the lest task before validating the whole script (page).
3. WHY do you want to extract the values and assign them to vars?
shows you I don't know what i'm doing...
Her is what finally worked and that's where I learned that the values
are saved as arrays. =-O
$autoId = mysql_insert_id($result);// this was recovered earlier in page
foreach($categoriesIN as $category){
print "$category<br />";
$insert_category = "INSERT INTO book_categories (book_id, categories_id)
VALUES ($autoid, $category)";
mysql_query($insert_category,$db);
}

Here's the problem...
the values saved interesect the book_variables with variables and books.
I believe the variabless are saved as arrays (1 entry per fiels -e.g.
there are 4 categories for 1 book) I have to recover the names of the
categories (field category in categories table) and display them in an
HTML table with hrefs to the correesponding pages. What I am trying to
figure out is how to select and echo the categories for each book.

here is part of the select statement concerning the categories:
(the rest of the select works, but since there are several categories,
my table shows several entries for 1 book that has several categories)
snip...
LEFT JOIN book_publisher as abc ON b.id = abc.bookID
LEFT JOIN publishers AS c ON abc.publishers_id = c.id
LEFT JOIN book_categories AS d ON b.id = d.book_id
LEFT JOIN categories AS e ON d.categories_id = e.id
ORDER BY title ASC ";

I suppose that
LEFT JOIN categories AS e ON d.categories_id = e.id
needs some qualifier that will output the categories as
cat1, cat2, cat3, cat4... for count($rows)... or something like that; in
other words, as 1 row instead of 1 row per category.
Hope I'm not too confusing.
since you don't know the number of entries there is no point in creating
a stack of variables of which you have no idea what they are even called
or how many there are. instead loop the array or use an index into the
array to find the data your looking for. you don't have to create a
specific variable to use a piece of data the given element of an array
is just as valid.

4. there is the extract() function ... use at your own risk.
5. do something like this:

foreach ($array as $key => $val)
$$key = $val;

.. again use at your own risk.

echo "$category<br>";
}
or
if(!empty($_POST['categoriesIN'])){
foreach( $_POST['categoriesIN'] as $key => $value) {
echo "value = $value<br>";
}
both show the values entered in the form but do not create a variable.
Since the number of entries will vary, I cannot assign a number of
variable names before knowing how many entries there may be.





After I sent that last email a few hours ago, I started thinking that you were 
using a JOIN in your SQL.

Since you were complaining that you were getting duplicate entries for each book that had more then one category assigned to it.

The problem is with the JOIN.

What you need to do it execute 1 SQL call to the books table and 1 call to the 
book_categories table.

All the following code it typed directly into my email client.  It is 
completely untested.

Hopefully it works for you.

<?php

# Do your db setup...
$dblink = mysql_connect(.....);

# call to your books table (without a join to the books_categories table)
$SQL = "SELECT id, title, author, etc... FROM books ORDER BY author, title LIMIT 
10";

$books = array();

if ( ( $results = mysql_query($SQL, $dblink) ) !== false ) {

        # loop through results, place in array.
        while ( $row = mysql_fetch_assoc($results, $dblink) ) {
                $books[] = $row;
        }

        # we must have one book, otherwise this will return false and we would 
not need to call for other information
        if ( $books ) {

                $book_categories = array();

                # call to your books_categories table
                # ( do an ORDER BY in your SQL so the categories always show up 
in the same order)
                $SQL = "SELECT book_id, cat_id FROM books_categories ORDER BY 
category";

                # loop through results, place in array
                if ( ( $results = mysql_query($SQL, $dblink) ) !== false ) {
                        while ( $row = mysql_fetch_assoc($results) ) {
                                $book_categories[$row['book_id']][] = 
$row['cat_id'];
                        }
                }

                # we must have one book2cat association, otherwise this will 
return false
                if ( $book_categories ) {

                        $categories = array();

                        # call to your categories table
                        # ( do an ORDER BY in your SQL so the categories always 
show up in the same order)
                        $SQL = "SELECT id, category_name, etc... FROM 
categories";

                        # loop through results, place in array
                        if ( ( $results = mysql_query($SQL, $dblink) ) !== 
false ) {
                                while ( $row = mysql_fetch_assoc($results) ) {
                                        $categories[$row['id']] = $row;
                                }
                        }

                }

        }

}

# Ok, now that all of that is out of the way, lets display the results...

if ( $books ) {
        # Open results table
        echo '<table>';
        # Loop through data
        foreach ( $books AS $book ) {

                # Display book information
                echo '<tr><td>Title: ', $book['title'], '<br />Author: ', $book['author'], 
'</td><td>';

                # Check to see if the book has associated categories
                if ( isset($book_categories[$book['book_id']]) ) {

                        # loop through associated categories
                        foreach ( $book_categories[$book['book_id']] AS $cat_id 
) {

                                if ( isset( $categories[$cat_id] ) ) {
                                        echo $categories[$cat_id]['category_name'], 
"<br />\n";
                                } else {
                                        echo "Unrecognized category ID: '", $cat_id, 
"'<br />\n";
                                }

                        }
                } else {
                        echo 'No associated categories';
                }

                echo '</td></tr>';          
        
        }

        echo '</table>';
} else {
        # If no books are found, tell them...
        echo 'No books found';
}

?>


Wow, now that took one foot work. I combed over the past couple threads that you started to collect all the information for the above code.

obviously you will need to change the SQL statements a little, but the code should pretty much work once you update the SQL for your actual column names and the related bits to that display code.

Hope this points when you should and (in this case) should not use a JOIN in 
your SQL.

--

Jim Lucas

--- End Message ---
--- Begin Message ---
Jochem Maas schreef:
> Joanne Lane schreef:
>> I am trying to create a class that recursively iterates over an array an
>> creates XML tree to reflect a multidimensional array.
>> I am not really a PHP coder, but am trying my hand.
> 
> I've seen 'real coders' write stuff thats leagues worse.
> 
>> This is what I have so far.
>> http://pastie.org/private/w75vyq9ub09p0uawteyieq
>>
>> I have tried a few methods, but I keep failing.
>> Currently, all elements are appended to the root node.
> 

.. yes my pleasure, glad I could help, not a problem. phffft.

--- End Message ---
--- Begin Message ---
2009/3/8 Clancy <clanc...@cybec.com.au>

> On Sun, 8 Mar 2009 12:33:15 +0000, stut...@gmail.com (Stuart) wrote:
>
> >2009/3/8 Clancy <clanc...@cybec.com.au>
> >
> >> On Sun, 8 Mar 2009 09:01:18 +0000, stut...@gmail.com (Stuart) wrote:
> >>
> >> >2009/3/8 Clancy <clanc...@cybec.com.au>
> >> >
> >> >> I want to index the files on a website recursively. The program will
> run
> >> in
> >> >> the site root
> >> >> directory, which GETCWD reports as D:/Websites/Website_1.  I can open
> >> any
> >> >> file in the root
> >> >> directory simply using its file name; Joe.dat, for example, and I can
> >> >> opendir for any
> >> >> subdirectory; eg
> >> >>
> >> >>        opendir(Subdirectory_1);
> >> >>
> >> >> but opendir () does not seem to work, and the only way I can find to
> >> open
> >> >> the root
> >> >> directory is to give its full path; eg
> >> >>
> >> >>        opendir (D:/Websites/Website_1);
> >> >>
> >> >> I have got the program working by using the full path to open the
> root
> >> >> directory, and then
> >> >> using relative paths to open the subdirectories and individual files,
> >> but
> >> >> this seems
> >> >> rather a kludge, and I am wondering if there is a way to open the
> root
> >> >> directory without
> >> >> specifying an absolute path?
> >> >
> >> >
> >> >The current working directory cannot be trusted to be "right". The best
> >> >option is to use dirname(__FILE__) and add '/..' as many times as
> needed
> >> to
> >> >get from the current file to the document root you're after.
> >>
> >> It has always worked for me.  But then I never change directory. But
> both
> >> give a hardware
> >> dependent answer;
> >>
> >> echo '<p>Current directory is '.__FILE__.', CWD is '.getcwd().'</p>';
> gives
> >>
> >> Current directory is D:\Websites\Corybas\Cydaldev\Dev\Testbed_2.php, CWD
> is
> >> D:\Websites\Corybas.
> >
> >
> >Not sure what you mean by a hardware-dependent answer. The current working
> >directory for any given script is determined by the web server and so it
> >cannot be assumed to be the location of the current script.
> >dirname(__FILE__) will give you the directory the current script is in.
>
> I agree that 'hardware dependent' was not the right word, but what I meant
> was that if I
> am running the local version of my program getcwd() will return
> 'D:\Websites\Corybas',
> where as if I am running the remote version it will return
> 'home/Corybasftp/www'.


This should be irrelevant in your code, something you can achieve using
dirname(__FILE__).

My webpage is always launched by loading index.php from the root directory,
> and I don't
> think I ever change the working directory, so although the code actually
> being executed at
> any given time is usually in a subdirectory, getcwd() will reliably return
> the (system
> dependent) long definition of the root directory.


Either I'm not explaining this well enough or you're just not getting it.
Ignore the current working directory - it's not reliable. If every page is
created by a single script then the solution is to define a constant at the
top of that script that gives you the directory that script is in....

define('ROOT_DIR', dirname(__FILE__));

Then when you need to refer to another file in your directory structure you
do so relative to the location of index.php. So if you have a directory
named arse in the same folder as index.php you'd refer to that like so...

ROOT_DIR.'/arse'

If that's not clear maybe this will help:
http://dev.stut.net/php/dirname.php

>Based on that the following should work in your particular situation, but

>rather than just using this I encourage you to understand why it works...
> >opendir(dirname(__FILE__).'/../Cydaldev');
>
> Looking into this I did find that 'opendir('Albums/../');', where 'Albums'
> can be any
> subdirectory I know to be present, will reliably open the root directory.


You clearly haven't looked into this at all since you're still not using
dirname(__FILE__), and if this email complete with the example script above
doesn't get the point across I give up. Have you even looked up dirname in
the manual? http://php.net/dirname

As a result of what seems to me to be an oversight in the design of
> opendir(), in that
> opendir(Fred) and opendir(Fred/Nurg) will open the appropriate sub
> directories but
> opendir() will not open the root directory, I can readily index all the
> subdirectories by
> giving their relative paths, but I have to resort to a kludge to index the
> root directory,
> and another kludge to eliminate this kludge from the index.


As in the example script I've posted above you can refer to the current
working directory with a single period (.), but this is still relying on the
current working directory being what you expect it to be.


> If, as it seems, I have to accept this it is fairly immaterial which kludge
> I actually
> use.


There is no need to use a "kludge". I encourage you to read my replies
carefully if you're still not getting it.

-Stuart

-- 
http://stut.net/

--- End Message ---
--- Begin Message ---
Hi, 

  I have a question regarding using line breaks in PHP. I have the code 
something like:

       echo "1" . "\t " . $x . "\t" . $y . "\r\n";

When I run the code, it looks like a whole blob of text, but when I use "View 
Source", the line breaks are formatted then correctly. 
Anyone can please tell me if this is what this is supposed to be? 
If so, how can I get the user to see the line break as they are, do I have to 
use <br>?

Thanks for your help.

Alice







_________________________________________________________________
Use Messenger to talk to your IM friends, even those on Yahoo!
http://ideas.live.com/programpage.aspx?versionId=7adb59de-a857-45ba-81cc-685ee3e858fe

--- End Message ---
--- Begin Message ---
2009/3/9 Alice Wei <aj...@alumni.iu.edu>

>  I have a question regarding using line breaks in PHP. I have the code
> something like:
>
>       echo "1" . "\t " . $x . "\t" . $y . "\r\n";
>
> When I run the code, it looks like a whole blob of text, but when I use
> "View Source", the line breaks are formatted then correctly.
> Anyone can please tell me if this is what this is supposed to be?
> If so, how can I get the user to see the line break as they are, do I have
> to use <br>?


This has nothing to do with PHP. HTML does not show carriage returns or
repeated white-space. If you want carriage returns or tabs you need to use
appropriate tags and/or CSS.

-Stuart

-- 
http://stut.net/

--- End Message ---
--- Begin Message ---
Stuart schreef:
> 2009/3/9 Alice Wei <aj...@alumni.iu.edu>
> 
>>  I have a question regarding using line breaks in PHP. I have the code
>> something like:
>>
>>       echo "1" . "\t " . $x . "\t" . $y . "\r\n";
>>
>> When I run the code, it looks like a whole blob of text, but when I use
>> "View Source", the line breaks are formatted then correctly.
>> Anyone can please tell me if this is what this is supposed to be?
>> If so, how can I get the user to see the line break as they are, do I have
>> to use <br>?
> 

you can also wrap the output in question in a <pre> tag:

<pre>
1       X       Y
2       X       Y
</pre>

alternatively use the nl2br() function ... but that won't help
with displaying the tabs.

note that there is a difference between the output of your script
(which you can view using 'View Source' when the output is sent to
the browser) and the representation of that same source
(by which I mean how the the source is rendered [in the browser, in this
case).

HTML rendering ignores tabs, carriage returns and multiple consecutive spaces
found in the source (with the exception of the <pre> tag, possibly the <code> 
tag,
additionally the CSS attribute 'whitespace', IIRC, can be used to force 
rendering
all whitespace chars.

--- End Message ---
--- Begin Message ---


> Date: Mon, 9 Mar 2009 13:28:19 +0100
> From: joc...@iamjochem.com
> To: stut...@gmail.com
> CC: aj...@alumni.iu.edu; php-gene...@lists.php.net
> Subject: Re: [PHP] Line Break Problem
> 
> Stuart schreef:
> > 2009/3/9 Alice Wei <aj...@alumni.iu.edu>
> > 
> >>  I have a question regarding using line breaks in PHP. I have the code
> >> something like:
> >>
> >>       echo "1" . "\t " . $x . "\t" . $y . "\r\n";
> >>
> >> When I run the code, it looks like a whole blob of text, but when I use
> >> "View Source", the line breaks are formatted then correctly.
> >> Anyone can please tell me if this is what this is supposed to be?
> >> If so, how can I get the user to see the line break as they are, do I have
> >> to use <br>?
> > 
> 
> you can also wrap the output in question in a <pre> tag:
> 
> <pre>
> 1     X       Y
> 2     X       Y
> </pre>
> 
> alternatively use the nl2br() function ... but that won't help
> with displaying the tabs.
> 
> note that there is a difference between the output of your script
> (which you can view using 'View Source' when the output is sent to
> the browser) and the representation of that same source
> (by which I mean how the the source is rendered [in the browser, in this
> case).
> 
> HTML rendering ignores tabs, carriage returns and multiple consecutive spaces
> found in the source (with the exception of the <pre> tag, possibly the <code> 
> tag,
> additionally the CSS attribute 'whitespace', IIRC, can be used to force 
> rendering
> all whitespace chars.

Hi, all:



  Thanks to all who replied. It looks like that a simple <pre> tag works great. 
  Looks like I didn't realize that \r\n does not reflect itself to the screen.

  

Alice


_________________________________________________________________
All-in-one security and maintenance for your PC.  Get a free 90-day trial!
http://www.windowsonecare.com/purchase/trial.aspx?sc_cid=wl_wlmail

--- End Message ---

Reply via email to