Everything comes up fine but in the cbItems column, I still get the array keyword:
s:5:"Array";
I get the number 5 no matter how many or if none are selected. Obviously, I'm not using this "serialize" feature correctly?
chazzy
07-25-2006, 12:10 PM
is the function implode what you're looking for? maybe explode?
either way, you shouldn't store an array like that in a database. use a separate table to store each entry as it's own line and have it reference a foreign key to another table, it's harder to search this way.
rbragg
07-25-2006, 02:19 PM
No, it does not implode what I'm looking for, nor does it look serialized. I'm guessing that it should not be imploding the word "Array" and that this suggests a syntax problem.
I won't be allowing search capabilities for this table using certain criteria. I'll only be displaying the contents as a whole so I'm not sure that using a foreign key is necessary? Is there a way to use INSERT command with the foreach() construct?
OR
Could I just use a different column within my database for each item in cbItems?
serialize will make it a string representation of the object, so that when you unserialize it is available again.
you should almost never have multiple values stored in a single column, it's just bad design. sure you don't have a requirement now but you might one day.
rbragg
07-25-2006, 02:57 PM
With your sample code in mind - would I have a different name for each of my checkboxes and therefore have a separate column in my db for each of them? Is implode just used to bring all of these together for more simple echoing and handling? If I'm following, I could use this sample code to later retrieve my checkbox information from my db?
Maybe I'm missing the big picture.
chazzy
07-25-2006, 05:12 PM
With your sample code in mind - would I have a different name for each of my checkboxes and therefore have a separate column in my db for each of them? Is implode just used to bring all of these together for more simple echoing and handling? If I'm following, I could use this sample code to later retrieve my checkbox information from my db?
Maybe I'm missing the big picture.
Think about it this way.
if 0,2,3,4 are checked, the array looks like $array[0]=$zeroval; $array[1]=$twoval; etc and if 1,4,6,7 are set, they have the same positions but different vals. you should be using implode to store it as a string since that's what you want to do based on your post. Serializing it and storing it in a database might not be what you want.
If you implode then store in a database, you would explode the database entry to read it. if you serial and store in a database, you would unserialize and read the entry. They do basically the same thing, one is a string representation of an array, the other is a serialized form of an object.
rbragg
07-26-2006, 09:21 AM
I think I understand what you are explaining though I still have an error somewhere. On my confirmation page I have this:
---------------------
<?php
if(!isset($_POST['cbItems']))
{
//array is empty
$_POST['cbItems'] = array();
echo "None selected.";
}
else
{
//must do this for checkboxes (passing multiple values)
foreach ($_POST['cbItems'] as $cbItems)
{
echo " - $cbItems";
}
}
?>
<!-- this passes the variable to the next page to be inserted in db -->
<input type='hidden' name='cbItems' value='<?php echo $_POST['$cbItems'];?>'>
---------------------
On the page where I connect and insert into the db I have this:
However, my echo says I'm sending every item, whether or not these items are checked. Does this mean I must use explode() even before I plan on echoing/printing cbItems? I'm fidgeting with the syntax as you read this post.
chazzy
07-26-2006, 02:56 PM
your array has nothing to do with the items checked or not, as per the start of your script
mmhmm, definitely hard-coded. I've been trying a few things that give me some php errors. Since I want to only deal with the submitted choices this means that I'm not wasting my time on another foreach() construct ... or am I?
I'm assuming that either the fields aren't named "items[]" or an array of an array will give problems w/ implode.
rbragg
07-27-2006, 03:52 PM
Thanks for not giving up on me.
Interesting. I get this:
---------------------
Thank you for submitting a presentation request. Array ( [0] => ) INSERT INTO healthed_workshop (date, ...
---------------------
... on down to my where my array is. It is empty.
In my form, my fields are named "cbItems[]". But, on my confirmation page it is changed when I set up my array.
<?php
if(!isset($_POST['cbItems']))
{
//array is empty
$_POST['cbItems'] = array();
echo "None selected.";
}
else
{
//must do this for checkboxes (passing multiple values)
foreach ($_POST['cbItems'] as $items)
{
echo " - $items";
}
}
?>
<input type='hidden' name='cbItems' value='<?php echo $_POST['$items'];?>'>
That's why on the last page I'm dealing with $items.
chazzy
07-27-2006, 04:11 PM
you don't seem to understand - the $_POST array is a list of fields sent based on their HTML field names. cbItems is not the same as items, they must have the same name.
rbragg
07-27-2006, 04:27 PM
Now I'm passing $cbItems to my last page instead of $items.
name='cbItems' //being my new field name so I needed to also pass 'cbItems' in $_POST[ ];
I should get in the habit of naming my variables differently than my field names to prevent that confusion.
Now, on my last page I have this:
$items= array($_POST['cbItems']); //with my new habit of naming variable differently than the field name
$cbItems_array = implode(",", $items);
And I'm inserting, $cbItems_array. I am getting the word "Array".
chazzy
07-28-2006, 09:58 AM
just so i can understand and you can understand then...
On page 1, you have the checkboxes.
And on the page that processes page1 you have
$cbItems = $_POST['cbItems'];//this is an array already
$cbItemsString = implode(",",$cbItems);//now they're in string form
?>
<input type='hidden' name='cbItems' value='<?php echo $cbItemsString;?>'>
Make sense? So now you're sending a field type hidden back to the processor, that will contain the string version of cbItems ALREADY when you reference $_POST['cbItems'] so you shouldn't need to implode the field.
What I'm confused about was when you started this topic, I thought you were talking about a single page processor. Now it seems like you're having a multistep processor - is that it? Please explain what you're trying to do. don't be vague.
rbragg
07-28-2006, 10:08 AM
I apologize b/c this problem is an extension of a previous problem that I posted in another thread. In that thread I explained that I have 3 pages. I should have made that more clear in this new thread.
You didn't implode it though! That's why you're getting "Array". If you did <?php
echo array();
?> You would just get Array also. you need to implode it to echo it like i mentioned before. You can't pass it like that over HTTP, the parsing as an array is on the PHP side, but not on the HTML/HTTP sides so it doesn't know what to do w/ it.
suggestion is to convert it to string on the confirmation page, then when they post to the final page it's already a string. Use my example from above.
rbragg
07-28-2006, 12:50 PM
And I should explode() on the final page before I insert them, correct?
chazzy
07-28-2006, 12:59 PM
And I should explode() on the final page before I insert them, correct?
Why? They're already in string form and you just want to insert the string, right?
rbragg
07-28-2006, 01:31 PM
I didn't think I needed explode() but my value was empty. HOWEVER, I was trying to insert VALUE '".$_POST['cbItemsString']."' When I changed that to '".$_POST['cbItems']."' it worked beautifully!!
Thanks for sticking with me! :D
chazzy
07-28-2006, 04:09 PM
why would it be cbItemsString...you don't have a form field named that do you? :-D
webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved.