Jump to content

display one thing first loop pass and something else second pass


futrose

Recommended Posts

I'm trying to get a simple table to display and am not sure how to make it happen.  Table will have just 2 columns.  A TIME and an EVENT.  What I want to have happen is for the first row to show the TIME and next to that, the EVENT.  If a TIME has more than one event going I want the next row to show an empty first cell and then the second event below the first.

 

If I set up the loop the only way I know how (so far) it would also output the TIME value again and again as many times as there were events for that same time.  For example what I don't want is:

 

9:15  Sunday School

9:15 Nursery Available

 

What I do want is:

 

9:15 Sunday School

        Nursery Available

 

Thanks.

 

well, I didn't quit get to coding that part yet because I knew it would be a problem.  I guess I can go ahead and do it the wrong way so you all have something to work off of. 

 

I guess so I start in the right direction can you tell me should the TIMES and the EVENTS be in separate tables? or does in not matter?  There will also be a DAY column (I left out of my initial post because it will be set up the same way in relation to the other information so if I can do what I asked I should be able to figure the rest out)  I'm thinking I need 3 tables, TIMES, EVENTS, and DAYS.  Let me know and I'll get started.

Thanks.

The logic to decide whether or not to display time:

Initialize an empty variable to hold the value of the time.

In your loop, compare the value in the variable to the value in the record to be displayed.

  - If the same, do nothing

  - If different, echo it and store the new value in the variable.

 

It's just about that simple.

so something like this?  I don't think I'm even close so go easy on me. :-\

 


$result = mysqli_query($link, 'SELECT * From s_times');
if (!$result)
{
$error = 'Error getting service times: ' . mysqli_error($link);
include $_SERVER['DOCUMENT_ROOT'] . './includes/error.php';
exit();
}

echo '<center>';
echo '<table cellspacing="0" cellpadding="3" border="1">';
echo '<tr>';
echo '<th>Times</th><th>Event</th></tr>';

while ($row = mysqli_fetch_array($result))
{
$stimes[] = array('times' => $row['times'], 'events' => $row['events']);
}
  $time = $row['times'];

foreach ($stimes as $s):
  {
     if (($s['times']) != $time)
          {
  		echo '<tr><td>' . ($s['times']) . '</td>';
                echo '<td>' . ($s['events']) . '</td></tr>';
          }
     else
          {
	echo '<td> </td>';
                echo '<td>' . ($s['events']) . '</td></tr>';
          }
   
  }
endforeach;

echo '</table>';

Kind of close, but you're really overthinking it . . .

 

$result = mysqli_query($link, 'SELECT * From s_times');
if (!$result) {
$error = 'Error getting service times: ' . mysqli_error($link);
include $_SERVER['DOCUMENT_ROOT'] . './includes/error.php';
exit();
}
echo '<center>';
echo '<table cellspacing="0" cellpadding="3" border="1">';
echo '<tr>';
echo '<th>Times</th><th>Event</th></tr>';
$time = '';
while ($row = mysqli_fetch_array($result) ) {
echo "<tr>\n<td>";
echo $time != $row['times'] ? $row['times'] : ' ';
echo "</td>\n";
echo "<td>{$row['events']}</td>\n</tr>\n";
$time = $row['times'];
}

I have a tendency to over think most of this which is why I find it so complicated (that and I'm just learning so my vocabulary is extremely limited) 

 

Can you tell me what the ? in this line means?

 

echo $time != $row['times'] ? $row['times'] : ' ';

 

Do I not need all the foreach stuff to run the loop???...

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.