Re: assign array values to variables

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, Mar 7, 2009 at 3:20 PM, PJ <af.gourmet@xxxxxxxxxxxx> 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.


so this really sounds more like a select (sql) problem than a php one, also
specific to your applications' schema, which we dont have at hand.., but the
point is, once youve got you select dialed in, getting the data to a page w/
php should be a cinch.

i see youre using the mysql api; in that case, youd likely use
mysql_query("some select stmt.."), follwed by a call to one of the
mysql_fetch_* functions, the documentation will tell you what they return.
from there all you have to do is loop over the result and toss the values in
w/ some html, echo and viola.

-nathan

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux