Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add Sterilize

Status
Not open for further replies.

tshey

Technical User
Dec 28, 2005
78
AU
Hi I have checkboxes in an array that i want to put into a table(customers) column (category) in a database(members).
I have used the insert wizard in dreamweaver, changed the name ="category[] value="reception", where do I insert sterilize and is there anything more I need to add to the script! Help please!
 
First off, do you mean serialize?

And second what exactly are you trying to accomplish here? Each database entry should be it's own seperate entity.

NATE


mainframe.gif


Got a question? Search G O O G L E first.
 
hi yes sorry i do mean serialize, I am trying to enter the users selection of checkboxes into a catagory column in a table. then display it on another page. Please explain how I can do this! PLEASE
 
Just set all the checkboxes like this
<input type="checkbox" name="checks[]" value="whatever">
and continue for how every many you have/need

Then, on the php processing just do:
$var = serialize($_POST["checks"]);

Now, store $var in the database.


When you call the serialized array store the data unserialized like so:
$checks = unserialize($var['column_name']);

Then if you want to show all the checks on the page do this:
echo '<input type="checkbox" name="checks[]" value="whatever"'; if (in_array('whatever', $checks) { echo 'checked'; } echo '> and continue in that same pattern for each checkbox.

Hope this helps!

NATE


mainframe.gif


Got a question? Search G O O G L E first.
 
When you call the serialized array store the data unserialized like so:
$checks = unserialize($var['column_name']);

What does this mean? I am having trouble unserializing.
So what you are saying is you serialize the array to put into the database, and then you unserialize to display it.
 
tshey said:
So what you are saying is you serialize the array to put into the database, and then you unserialize to display it.

Yes this is what SPYDERIX is saying. For a matter of simplicity when you call:
Code:
$var = serialize($_POST["checks"]);
PHP returns a string representation of the array contained in $_POST["checks"]. This string is now stored in $var, and you can insert that into your db column.

Then when you retrieve the string from the db and call:
Code:
$checks = unserialize($var['column_name']);
PHP takes the serialized string and converts it back to an array. Then you can use that array to determine what to display on the following page. Like in SPYDERIX's example:
Code:
echo '<input type="checkbox" name="checks[]" value="whatever"'; 
if ([url=http://us3.php.net/manual/en/function.in-array.php]in_array[/url]('whatever', $checks) { 
  echo 'checked'; 
} 
echo '>';

Itshim
 
Yup, can't really explain it much better. The choices from the checkboxes are stored in an array while processing but in order to save that array you simply serialise and write to a file or database or xml or whatever. Then if you want to use those choices again you need to unserialize the array effectively turning the string back into an array that you can loop through etc when php processes it.

Hope this helps!

NATE


mainframe.gif


Got a question? Search G O O G L E first.
 
the post-er's confusion might be being caused by the reuse of the $var variable as a row identifier. perhaps a sample code block showing the unserialisation might help clarify.

Code:
//db server connect
//db select
$table = "" ;//insert table name here
$col = "" ;//insert name of column used for storing array

$results = mysql_query("Select $col as arr from $table");
while ($row = mysql_fetch_assoc($results)):
$checks = unserialize($row['arr']);
  echo "<br/><pre>";
  print_r ($checks);
  echo "</pre>";
endwhile;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top