Jump to content

Display related fields in form


Naqsh

Recommended Posts

Hello,

 

Here is the situation:

 

we have three tables-

 

1."Event" Table

2. "Event_statuses" Table

3."Status" Table

 

The "Event" table holds basic information about an event:

id, location, date, time.

 

The "Event_statuses" table holds

 

id, date, event_id, status_id

 

The "Status" table holds:

id, statusname

 

I am dynamically generating the list of events with a checkbox next to each and a date field. so an event can be checked and a date entered to show the date it was activated.

 

I have got the list of events to show and the ones that are selected for the event being viewed but am struggling to get the date to show.

 

any help with this would be very much appreciated....

 

 

Code being used is as follows:

 

 

Controller file:

$eventid = mysqli_real_escape_string($link, $_SESSION['eventid']);

// Get list of statuses assigned to this event

$sql = "SELECT statusid, date FROM Event_statuses WHERE event_id='$event_id'";
$result = mysqli_query($link, $sql);
if (!$result)
{
$error = 'Error fetching list of assigned statuses.';
include 'error.php';
exit();
}


while ($row = mysqli_fetch_array($result))
{
$selectedstatuses[] = $row['statusid'];
$date = $row['date'];
}



// Build the list of all statuses

$sql = "SELECT id, statusname FROM Status";
$result = mysqli_query($link, $sql);
if (!$result)
{
$error = 'Error fetching list of statuses.';
include 'error.php';
exit();
}

while ($row = mysqli_fetch_array($result))
{
$statuses[] = array(
'id' => $row['id'],
'statusname' => $row['statusname'],
'selected' => in_array($row['id'], $selectedstatuses),
'date' => $date);
}

include 'form.php';
exit();

 

The Form being populated

 

<fieldset>
<legend>Statuses</legend>
<?php for ($i = 0; $i < count($statuses); $i++): ?>
<div>

<label for="statuses<?php echo $i; ?>"><input type="checkbox" name="statuses[]" id="statuses<?php echo $i; ?>"
value="<?php htmlout($statuses[$i]['id']); ?>"
<?php if ($statuses[$i]['selected']) {echo ' checked="checked"';}?>>
<?php htmlout($statuses[$i]['id']); ?></label>:
<?php htmlout($statuses[$i]['statusname']); ?>
<label for="Date">Date</label> <input type="text" name="date" id="date" value="<?php htmlout($statuses[$i]['date']); ?>" >   

</div>
<?php endfor; ?>
</fieldset>

 

MOD EDIT: code tags added.

Link to comment
https://forums.phpfreaks.com/topic/238927-display-related-fields-in-form/
Share on other sites

You're setting the date variable here, and re-assigning it every iteration of the loop.

while ($row = mysqli_fetch_array($result))
{
$selectedstatuses[] = $row['statusid'];
$date = $row['date'];
}

 

Then using the static variable here

while ($row = mysqli_fetch_array($result))
{
$statuses[] = array(
'id' => $row['id'],
'statusname' => $row['statusname'],
'selected' => in_array($row['id'], $selectedstatuses),
'date' => $date);
}

 

Perhaps you should echo it out in one of these loops to make sure you're getting the right info from the database.

Archived

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

×
×
  • 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.