Jump to content

[SOLVED] Form w/ checkboxes - inserting and viewing help.


Recommended Posts

I am building a form that allows the user to select "days" from a checkbox list. 

 

Form (newclient.inc.php):

 

<td><h3>Training Schedule:</h3></td>
      <td><input type="checkbox" name="day[]" value="m">Monday
          <input type="checkbox" name="day[]" value="t">Tuesday
          <input type="checkbox" name="day[]" value="w">Wednesday
          <input type="checkbox" name="day[]" value="r">Thursday
          <input type="checkbox" name="day[]" value="f">Friday
          <input type="checkbox" name="day[]" value="s">Saturday
      </td>

 

Process (addclient.inc.php):

 

$day = serialize($_POST['day']);

 

If a user selects Monday, Wednesday, & Friday it is storing in the mysql db as: a:3:{i:0;s:1:"m";i:1;s:1:"w";i:2;s:1:"f";}

 

QUESTION:

1) I am not sure if that is the proper way to store the data.

2) I want to be able to extract that information so that I can view the details of a user's training schedule.  For example, "MWF".

 

 

 

Thanks!

 

Okay Thanks!  I changed my "viewc.inc.php" file to:

 

<?php
  include ("include/dbconnect.php");
  include ("include/format.inc.php");

$id = $_GET['id'];

$query = "SELECT id, firstname, lastname, address, city, state, zip, day, time FROM client WHERE id = $id";
$result = mysql_query($query) or die('Could not find client');
$row = mysql_fetch_array($result, MYSQL_ASSOC) or die('No records retrieved');
        $id = $row['id'];
        $firstname = $row['firstname'];
        $lastname = $row['lastname'];
        $address = $row['address'];
        $city = $row['city'];
        $state = $row['state'];
        $zip = $row['zip'];
        $day = unserialize($row['day']);
        $time = $row['time'];

echo "<h2>$firstname $lastname <a href=\"index.php?content=editc&id=$id\" target=\"_blank\"><img border=0 src=icons/pencil.png width=16 height=16 title='".'Edit'."' alt='".'Edit'."'/></a> <a href=\"print.php?id=$id\" target=\"_blank\"><img border=0 src=icons/printer.png width=16 height=16 title='".'Printer Friendly'."' alt='".'Printer Friendly'."'/></a></h2>";
echo "<h4>$address</h4>";
echo "<h4>$city, $state $zip</h4>";
echo "<h4>Schedule:$day @ $time";
?>

 

On the output I get the word "Array".  What is the next step to viewing the data? 

well what you would have to do is do a foreach to output it, as you cant simply echo an array (unless you wanted to use the print_r function, but that would echo the structure of the array, and not simply the values that are in it. some something like

 

$string = "";
foreach($day as $day){
$string .= $day;
}
echo "<h4>Schedule:$string @ $time";

 

that would put all the entries in the array into a the variable string. so if the day array had the values M, W, and Th, the string variable would become "MWTh".

 

Hope that helps!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.