PDA

View Full Version : Store multiple value in single column



devil_vin
09-20-2007, 04:45 AM
Hi..guys! I have a column which need to store multiple time value,like 11:00am,2:00pm,5:30pm.How can that single column fix it?And how to control time format in between 12-hours and 24-hours format?Thanks....

Twey
09-20-2007, 10:28 AM
You have several options: you can store it as a list of comma-separated values, like you have there; you can store it as a string storing a serialised array from whatever language you're using to interact with the database (e.g. PHP's serialize() or Python's pickle module) or you can create two new tables and have a many-to-many relationship between the rows in that table and a special "times" table.

devil_vin
09-20-2007, 04:01 PM
Thanks for helping.I am using first option and create an extra column,screeningTime with varchar type.How can I display each of its value accompany by a radio button in PHP?Thanks...

Twey
09-20-2007, 05:42 PM
<form action="">
<?php foreach(explode(',', $row['screeningTime']) as $time) { ?>
<label>
<input type="radio" value="<?php echo $time; ?>">
<?php echo $time; ?>
</label>
<?php } ?>
</form>

devil_vin
09-20-2007, 06:14 PM
Thanks for reply.Let said now I have seven rows of record need to be fetched out,how can it be achieved?

I try $result = mysql_query("SELECT name,category,screeningTime FROM $tbl_name WHERE id < 8") but seems like look weird.How can I manipulate each id's name,category and screeningTime be fetched and displayed properly?



<?
if (isset($_SESSION['gmemberid'])) {
$tbl_name = "movie";

$result = mysql_query("SELECT name,category,screeningTime FROM $tbl_name
WHERE id ='1'")
or die("Cannot execute query.");

$numrow = mysql_num_rows($result);

if ($numrow != 0) {
while($rows = mysql_fetch_array($result)){
echo '<strong><br>' . $rows[0] .' (' . $rows[1] .') </strong></font>';
foreach(explode(',',$rows[2])as $time) {?>
<br><br><label>
<input type="radio" value="<?php echo $time; ?>">
<?php echo $time; ?>
</label>
</td></tr>
<?php } ?>
<?
}

}
}
?>

Twey
09-20-2007, 07:37 PM
<?php
$tbl_name = 'movie';
$rs = mysql_query(sprintf('select name, category, screeningTime from %s limit 7', $tbl_name))
or die(sprintf('Cannot execute query: %s', mysql_error()));

while($row = mysql_fetch_array($rs))
foreach(explode(',', $row['screeningTime']) as $time) {
?>
<label>
<input type="radio" value="<?php echo $time; ?>">
<?php echo $time; ?>
</label>
<?php } ?>Using <br> there is most probably an abuse of the element. Use CSS instead. The <font> tag is deprecated.

devil_vin
09-22-2007, 06:47 AM
Thanks for helping,by now I would like to submit the form after clicking one of the radio button.How can the selected screeningTime and its movie name being captured by $_POST?Really appreciate for your initiative.



<?
if (isset($_SESSION['gmemberid'])) {
$tbl_name = "movie";

$result = mysql_query(sprintf('SELECT name,category,screeningTime FROM %s
LIMIT 7', $tbl_name)) or die('Cannot execute query.');

//$numrow = mysql_num_rows($result);


while ($rows = mysql_fetch_assoc($result)) {
echo '<table width="100%" border="0"><tr><td height="68">
<table width="100%" height="47" border="0">
---------------------------------------------------------------------------------------------------------<br>';

echo '<strong>' . $rows['name'] . ' (' . $rows['category'] . ')
<br></strong>';
foreach (explode(',', $rows['screeningTime']) as $time) { ?>
<label>
<input type="radio" value="<?php echo $time; ?>">
<?php echo $time; ?>&nbsp;&nbsp;&nbsp;
</label>
<?php } ?>
<?
}


}
?>