Jump to content

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.

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.