Jump to content

Array in database


lauralee

Recommended Posts

I am having trouble showing the value of the $timeslot array in the database. It just shows up as Array. How can I actually see the value?

<?php{    $firstname = $_POST['firstname'];    $lastname = $_POST['lastname'];    $numberinparty = $_POST['numberinparty'];    $timeslot = $_POST['timeslot'];    $entree1 = $_POST['entree1'];    $entree2 = $_POST['entree2'];    $dinnerdate = $_POST['dinnerdate'];     $n        = count($timeslot);   $i        = 0;    }        if ($entree1 + $entree2 == $numberinparty){echo $firstname . " " . $lastname . '<p>Thank you for your reservation for a party of ' . $numberinparty . ' on ' . $dinnerdate .  '.</p>';                                                  echo "The reservation time you selected is \r\n";   while ($i < $n)   {      echo "{$timeslot[$i]} \r\n";      $i++;   }     echo '<p> You have chosen <br />'; echo $entree1 . ' - Entree #1 <br />'; echo $entree2 . ' - Entree #2</p>';echo '<p>If this is correct, please select the "Make Reservation" button below.</p>';} else {   echo '<h3>The number of entrees ordered must equal the number in your party</h3>'; exit ('<p>Please <a href="reservations.php">return </a>to the reservation page to correct your reservation</p>'); }?>

Link to comment
Share on other sites

I take it this is an array of strings? If so, you can implode() it, using as the separator a character which doesn't occur in the actual strings. Then when you get it back out of the database you can explode() it around that same character.If anything other than strings (and numbers) happens to be in your array, you'll need to use a Binary Large OBject which is out of my league, but I know MySQL can handle it.

Link to comment
Share on other sites

you also can use the function serialize($arr), which will convert it into a string, and store that in the database, then when you read it again use unserialize($str) to get it back as array(tho if its a one-dimenional array with just strings and numbers implode() and explode() would be better, since it takes less space)

Link to comment
Share on other sites

How exactly do I use the implode() explode() function. Is it before it is sent to the database? or somehow in the field of the database, or just when I print from the database

Link to comment
Share on other sites

Where can I find info on the implode, explode function online. I'm really under the gun here, and need to fix this problem asap! Thanks!

Link to comment
Share on other sites

I guess I need to explain that the array is from a form in which the $timeslot array is used in a form where a user can select a time for a dinner reservation. The array lists have - name="timeslot[]" and value of "5:30 pm" or "6:00 pm" etc. All with the same name. When the user selects one of the checkboxes, it shows up as Array in the database, instead of the actual time.

Link to comment
Share on other sites

<label>6:00 pm <input type="checkbox" name="timeslot[]"  value="6:00 pm" /></label><br /><label>6:15 pm <input type="checkbox"  name="timeslot[]"  value="6:15 pm" /></label><br /><label>6:30 pm <input type="checkbox" name="timeslot[]"  value="6:30 pm"/></label><br />

Link to comment
Share on other sites

No, they would only be allowed to choose one time slot. However, I haven't learned how to do that yet either. I'll check out the serialize information for the array problem, then figure out how to allow only one check. Thanks!

Link to comment
Share on other sites

if there should only one be checked, use radiobuttons with the same name<input type="radio" name="timeslot" value="6:00" /><input type="radio" name="timeslot" value="6:15" /><input type="radio" name="timeslot" value="6:30" />then users will only be able to select one, and $_POST['timeslot'] will be a string and not an array

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...