Scooby08 Posted March 18, 2011 Share Posted March 18, 2011 Hello all.. This one's sort of difficult to explain, but yet it should be a pretty simple solution.. What I'm trying to do is loop through an array that has either single event id's or a group of event id's, and run a condition on the change of id's while looping.. Sort of grouping each set if unique id's within a new event section.. Here's sort of a test code I put together: <?php $result = array( array('event_id' => 70), array('event_id' => 70), array('event_id' => 70), array('event_id' => 95), array('event_id' => 96), array('event_id' => 97), array('event_id' => 98), array('event_id' => 99), array('event_id' => 145), array('event_id' => 145), array('event_id' => 145), array('event_id' => 145), array('event_id' => 166), array('event_id' => 166), array('event_id' => 177), array('event_id' => 200), array('event_id' => 200), array('event_id' => 200) ); $previous_id = ''; foreach($result as $row){ if ($row['event_id'] != $previous_id) { echo '<div style="background:#ccc;">New Event</div>'; } if ($previous_id > 0 && $row['event_id'] != $previous_id) { echo '<table border="1"><tr><td>'.$row['event_id'].'</td></tr></table>'; $previous_id = ''; } else { echo $row['event_id'].'<br />'; $previous_id = $row['event_id']; } } ?> It's about half way working, but there are issues when the ids change and they are either single event ids in a row, or multiple same event ids in a row.. I can't think how to set the $previous_id variable properly on each loop.. Well, figured I'd throw it out there and see what happens.. Thanks.. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 18, 2011 Share Posted March 18, 2011 You still didn't provide an explanation of what you are trying to achieve. But, I'll provide some code on what I suspect you are wanting. $previous_id = false; $output = ''; foreach($result as $row) { if ($row['event_id'] !== $previous_id) { $output .= "<tr><td style=\"background:#ccc;\">New Event</td></tr>\n"; $previous_id = $row['event_id']; } $output .= "<tr><td>{$row['event_id']}</td></tr>\n"; } Quote Link to comment Share on other sites More sharing options...
Scooby08 Posted March 18, 2011 Author Share Posted March 18, 2011 Hey thanks for responding mjdamato.. Yeah I didn't quite know how to explain what I was doing.. Maybe the attached screenshot will help with getting my objective across.. The desired_output.jpg has a couple notes of where the code is going wrong and what it should be doing.. [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 18, 2011 Share Posted March 18, 2011 OK, from your screenshot my understanding is that if there is only one entry for a specific ID you want that item to have a boder. However, if there are multiple entries for the same ID then there will be no border. My advice is to NOT create individual tables for the individual IDs. It only makes the HTML more complex and the PHP code for creating it harder to create/maintain. Instead use a single table for the entire content and use style properties to modify the entries of single IDs. You could also make this process much, much easier by reconfiguring your array so it is not multidimensional. But, a more important question is how is this array generated? If this is generated from a DB query - then you are going about this all wrong. Is there other data for each record that you are wanting to display or is the ID really the only thing you want displayed? Quote Link to comment Share on other sites More sharing options...
Scooby08 Posted March 18, 2011 Author Share Posted March 18, 2011 Yeah the table will be rebuilt once I get the loop working.. It was strictly for testing purposes.. And it is an array coming from a database with thousands of records at a time and the unique id is the event id for each group.. So I sorta have to figure it out the way that it is.. I'm really not too sure how I could restructure the array better so it could loop like I want.. Do you have an example? Thank you very much for helping out.. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 18, 2011 Share Posted March 18, 2011 Sure, first do not run a database query then dump the data into an array just so you can process it later. It is createing a two step process that should only be one. Just process the database results once. There is no need to put them into an array. However, the example code below does use a temp array. That is because - accordingly to your specifications - you need to style the output differently based upon whether there is one record or multiple for and event ID. The easiest way to do that is not to generate any output for a particular event ID until you have gathered all the data for that ID. What you are asking is fairly simple, here is some example code: <?php //Function to create HTML output for all records of an event function createEventHTML($eventArray) { $htmlOutput = "<tr><th style=\"background:#ccc;\">New Event</th></tr>\n"; //Determine style based on whether there is one or multiple records $style = (count($eventArray)>1) ? '' : 'border:1px solid black;'; foreach($eventArray as $event) { $htmlOutput .= "<tr><td style=\"{$style}\">{$event['event_id']}</td></tr>\n": } return $htmlOutput; } //Run query to get the data $query = "SELECT * FROM table_name ORDER BY event_id"; $result = mysql_query($query) or die(mysql_error()); //Create variables for the processing loop $eventTableOutput = ''; //String to hold html output $current_id = false; //Var to check for id change $eventAry = array(); //Temp array to event records //Process the results into html output while($row = mysql_fetch_assoc($result)) { //Check if the record has different id from last if($row['event_id']!==$current_id) { //Create html output for previous event ID records if(count($eventAry)>0)) { $eventTableOutput .= createEventHTML($eventAry); } //Set current ID and set temp array $current_id = $row['event_id']; $eventAry = array(); } //Add record to temp array $eventAry[] = $row; } //Add the last event records to the output $eventTableOutput .= createEventHTML($eventAry); ?> <table> <?php echo $eventTableOutput; ?> </table> Quote Link to comment Share on other sites More sharing options...
Scooby08 Posted March 18, 2011 Author Share Posted March 18, 2011 Hello again mjdamato.. Had to step away for a bit.. But I just got to testing it out and guess what? It's Money! Seems to do exactly what I needed... I'll work this in and see if any other issues arise, but I'm thinking it's all good.. I can't thank you enough.. Thanks a million! Quote Link to comment 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.