Forum Moderators: coopster

Message Too Old, No Replies

function help+unserialize

I have created a function (my first php function)....

         

cucumberdesign

1:46 pm on May 31, 2006 (gmt 0)

10+ Year Member



function serializer() { 
$serialarray = array("radiostations", "hobbiesinterests"); //get serialized entries from MySQL db
$x = count($serialarray);
for ($i = 0; $i < $x; $i++) {
$genvar = unserialize($serialarray[$i]);
foreach($genvar[$i] as $key[$i] => $value[$i]);
//Print value of each array in their respective spot, by calling respective $variable
$variableArray[$i] .= $value.', ';
}
}

basically what I have is a survey form that submits data to a database. Checkboxes have been serialized for easier validation.
I am busy coding a backend for the survey, where I can retrieve the data in the database and display in a user friendly way for the client.
I am able to recover all of the other data excepting the checkbox data.
if i code each serialize loop individually, it works 100% but the 2 values mentioned in the above script are 2 of 20.
I don't really want to have to type 4 lines of code for each one, so hence the decision to create the function.
Now I have tested this function, and it is not returning any errors, so I would say it is complete to an extent, but how would I call an individual variable from within the function to print outside the function...

Much like below

<?php 
// Database Connection
include ("../includes/dbconnect.php");
// View link details
if($_GET['action']=="view"){
$id = $_GET['id'];
$getsurveysdetail = mysql_query("select * from survey WHERE id=$id");
while($row = mysql_fetch_array($getsurveysdetail))
{
extract($row);
$radio = unserialize($radiostations);
foreach($radio as $key => $value) {
$radioArray .= $value.', ';
}
print ("<div id=\"results\">
<p class=\"result_header\">Demographics</p>
<strong>Name:</strong> $name<br />
<strong>Date Submitted:</strong> $date<br />
<strong>Time Submitted:</strong> $time<br />
<strong>Email Address:</strong> $email<br />
<strong>Contact Number:</strong> $contactnumber<br />
<strong>Age:</strong> $age<br />
<strong>Marital Status:</strong> $maritalstatus<br />
<strong>Gender:</strong> $gender<br />
<strong>Lives:</strong> $live<br />
<strong>Works:</strong> $work<br />
<strong>Radio Stations:</strong> " . $radioArray . "<br />

Hope you understand this post...

[edited by: coopster at 2:15 pm (utc) on May 31, 2006]
[edit reason] inserted code, removed urls [/edit]

coopster

12:18 pm on Jun 3, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, cucumberdesign.

Are you saying that the "serializer" function is not complete yet? You are wondering how to use it to dynamically build a <select> element?

siMKin

3:37 pm on Jun 4, 2006 (gmt 0)

10+ Year Member



If you don't mind the discussion, i'm interested why you are storing serialized arrays in your database. It is my experience that it always turns out to be a bad idea.
From what i understand the situation is more or less that in 1 record / row you store, say, 10 radiostations that someone indicated should be stored using a checkbox. Right?
But what, if at some point you want to create an overview of all the users that have 10 or more radiostations stored? This would be impossible with a serialized data. Well, not impossible, but it means you have to extract all the data from the database, unserialize it and manually analyse it in PHP. Like that i could think of a dozen other situations in which you will have the same problem.
Like i said, it almost always turns out to be a bad idea.

Regarding your question, i'm afraid i don't fully understand your problem.

cucumberdesign

8:11 am on Jun 5, 2006 (gmt 0)

10+ Year Member



Coopster

Thanks for the welcome...
No, basically I wanted to create a function that would simplify the unserializing of data.

The reason being, is that I have 7/8 checkboxes that I have serialized. That makes 14 or so extra lines of code. I was thinking there must be away that I can create a function that will unserialize all the checkboxes into an array, and then i can just print each array using a single print statement per checkbox.

Does that make sense...

coopster

2:42 pm on Jun 5, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The easiest way to do your unserialize() would be to serialize() it that way in the first place. So, if the data is in a $_POST superglobal you could just push each $_POST index into your own index before you serialize and store it. For example,
$myData['radiostations'] = $_POST['radiostations']; 
$myData['hobbiesinterests'] = $_POST['hobbiesinterests'];
serialize($myData);

However, siMKin has a very good point here, especially with stored survey data. You are going to have difficulty analyzing any of it if it is stored in a serialized format.