Re: assign array values to variables

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

 



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.
>>
>
>


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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[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