Jezza22 Posted May 22, 2009 Share Posted May 22, 2009 I have a function that creates an Array and after each row enters a blank row, that displays in grid. The grid displays a row followed by a blank row, which is great!. As follows, [["SL","121","9"],[], ["SL","121","9"],[], ["SL","121","10"],[] ["SL","121","11"],[], ["SL","121","16"]]; My problem is the third element is a document number and I want to somehow group that together when the more than one row contains the same document number, by not outputting a blank row. as Follows, [["SL","121","9"],[], ["SL","121","9"], ["SL","121","10"],[] ["SL","121","11"],[], ["SL","121","16"]]; The only way I think I can achieve it is by adding a count as fourth element, [["SL","121","9","0"],[], ["SL","121","9","1"], ["SL","121","10","0"],[] ["SL","121","11","0"],[], ["SL","121","16","0"],[],]; But I can't work out how to get the above, here's the code. function cells($dataset){ $rows = array(); while ($record = @mysql_fetch_row($dataset)) { $cols = array(); foreach ($record as $value) { $cols[] = '"'.addslashes($value).'"'; } echo $cols[3]; if ($cols[3] == '"0"') { $rows[] = "\t[".implode(",", $cols)."]"; } else { $rows[] = "\t[".implode(",", $cols)."],[]"; } } echo "[\n".implode(",\n",$rows)."\n];\n"; } Help, Please... Quote Link to comment Share on other sites More sharing options...
MadTechie Posted May 22, 2009 Share Posted May 22, 2009 I'm Not 100% sure this is what your asking for but <?php function cells($dataset){ $rows = array(); $last = ""; while ($record = @mysql_fetch_row($dataset)) { $cols = array(); foreach ($record as $value) { $cols[] = '"'.addslashes($value).'"'; } //echo $cols[3]; if ($cols[3] == '"0"') { $rows[] = "\t[".implode(",", $cols)."]"; } else { if($last == $cols[3]) { //repeated from last $rows[] = "\t[".implode(",", $cols)."]"; }else{ $rows[] = "\t[".implode(",", $cols)."],[]"; } } $last = $cols[3]; } echo "[\n".implode(",\n",$rows)."\n];\n"; } ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 22, 2009 Share Posted May 22, 2009 In my opinion you are doing this backwards. Your array should just contain your data and the process of outputting the array to the page should take care of the rest. You should not be adding empty array elements to create a blank row, Example: <?php //Create array of JUST the data $data = array ( array ("SL","121","9"), array ("SL","121","9"), array ("SL","121","10"), array ("SL","121","11"), array ("SL","121","16") ); function displayArrayData($arrayValues) { //Start the table echo "<table border=\"1\">\n"; echo " <tr>\n"; echo " <th>Column 1</th>\n"; echo " <th>Column 2</th>\n"; echo " <th>Column 3</th>\n"; echo " </tr>\n"; $lastRowID = ''; //Iterrate through each record foreach ($arrayValues as $dataRow) { //Show blank row if this record is different ID than last if($dataRow[2]!=$lastRowID && $lastRowID != '') { echo "<tr><td colspan=\"3\"> </td></tr>\n"; } //Set last ID value $lastRowID = $dataRow[2]; //Display current record echo " <tr>\n"; echo " <td>{$dataRow[0]}</td>\n"; echo " <td>{$dataRow[1]}</td>\n"; echo " <td>{$dataRow[2]}</td>\n"; echo " </tr>\n"; } //Close the table echo "</table>\n"; } displayArrayData($data); ?> The data array only contains the data and the logic parses the data and outputs it as needed. The output would look like this: SL 121 9 SL 121 9 SL 121 10 SL 121 11 SL 121 16 Quote Link to comment Share on other sites More sharing options...
Jezza22 Posted May 23, 2009 Author Share Posted May 23, 2009 Thanks, to both equally good replies. I have tried MadTechie answer but unfortunately it results in, SL 121 9 SL 121 9 SL 121 10 SL 121 11 SL 121 16 Mjdamato, reply works.... But it works outputting a grid in PHP, HTML. I need the Array to be structured in this fashion because the Array feeds my grid which is written in Javascript, Activewidgets. So... I am now trying variations of MadTechie's code and adapting Mjdamato code to try and find the answer. Thanks to both of your help... Jez!! Once I have cracked it, I shall updated the thread, However, Mjdamato, please feel free to update your example, but using the Array to process the output.. Thanks once again !!!! Quote Link to comment Share on other sites More sharing options...
MadTechie Posted May 23, 2009 Share Posted May 23, 2009 try if($last != $cols[3]) instead its late my brain it shutting down! Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 23, 2009 Share Posted May 23, 2009 My "example" was just that - an example. The logic still holds true no matter what method you are using to generate the output. I still contend that the most appropriate method of solving this issue is with modifying the code that generates the output not in manipulating the data. If you were to provide the code used to generate the output then we could take a look at modifying that. But, if the code is a completely self-contained 3rd party function it may not be worthwhile to do so and modifying the data may be the most "efficient" solution - while not the optimal solution. Quote Link to comment Share on other sites More sharing options...
Jezza22 Posted May 24, 2009 Author Share Posted May 24, 2009 Mjdamato, You are indeed correct and I have cracked it, using your method. I am sorry If I sounded pedantic on my reply, I am fairly new, and any help received is much appreciated. I have been fighting this particular problem for a couple of days now and both your and MadTechie's advice has helped solve it... So thanks once again.. to both of you! Here's the finall code, //Retrieves the data from the database and creates an Array while ($array = mysql_fetch_array($result, MYSQL_NUM)) { $data[] = $array; } //Displays the Array data into a 2D javascript array function displayArrayData($arrayValues) { //Start of the Array echo "["; $lastRowID = '"'; //Iterrate through each record foreach ($arrayValues as $dataRow) { //Show blank row if this record is different ID than last if($dataRow[2]!=$lastRowID && $lastRowID != '') { echo '[],'; } //Set last ID value $lastRowID = $dataRow[2]; //Display current record echo ' ['; echo ' "'.$dataRow[0].'",'; echo ' "'.$dataRow[1].'",'; echo ' "'.$dataRow[2].'",'; echo ' ],'; } //End of the Array echo '[],];'; } displayArrayData($data) Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 24, 2009 Share Posted May 24, 2009 Please mark the post as solved. 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.