futrose Posted February 13, 2011 Share Posted February 13, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/227496-display-one-thing-first-loop-pass-and-something-else-second-pass/ Share on other sites More sharing options...
jcbones Posted February 13, 2011 Share Posted February 13, 2011 Show us your code, we'll show you how to fix that problem. Quote Link to comment https://forums.phpfreaks.com/topic/227496-display-one-thing-first-loop-pass-and-something-else-second-pass/#findComment-1173452 Share on other sites More sharing options...
futrose Posted February 13, 2011 Author Share Posted February 13, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/227496-display-one-thing-first-loop-pass-and-something-else-second-pass/#findComment-1173459 Share on other sites More sharing options...
Pikachu2000 Posted February 13, 2011 Share Posted February 13, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/227496-display-one-thing-first-loop-pass-and-something-else-second-pass/#findComment-1173464 Share on other sites More sharing options...
futrose Posted February 13, 2011 Author Share Posted February 13, 2011 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/227496-display-one-thing-first-loop-pass-and-something-else-second-pass/#findComment-1173470 Share on other sites More sharing options...
Pikachu2000 Posted February 13, 2011 Share Posted February 13, 2011 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']; } Quote Link to comment https://forums.phpfreaks.com/topic/227496-display-one-thing-first-loop-pass-and-something-else-second-pass/#findComment-1173473 Share on other sites More sharing options...
futrose Posted February 13, 2011 Author Share Posted February 13, 2011 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???... Quote Link to comment https://forums.phpfreaks.com/topic/227496-display-one-thing-first-loop-pass-and-something-else-second-pass/#findComment-1173474 Share on other sites More sharing options...
futrose Posted February 13, 2011 Author Share Posted February 13, 2011 Never mind, I found what the ? and the : do. nice little short hand. Thanks for your help. I'm off to get this set up and see how it works out. Quote Link to comment https://forums.phpfreaks.com/topic/227496-display-one-thing-first-loop-pass-and-something-else-second-pass/#findComment-1173476 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.