tHud Posted December 1, 2011 Share Posted December 1, 2011 Hello I have been taking a look at associative arrays. I 'sort' of get the key/value idea, but I'm not quite sure if they appropriate for use with 3 items (as opposed to the 2 key/value items.) In the code below, I can incorporate the 'company' and 'table' values - but I'm not sure how to add the 'id' value (from the query). I would very much appreciate some guidance here. Thank you. $a = array(); // 'A' table array $b = array(); // 'B' table array $c = array(); // 'C' table array $query = "SELECT id, company, `table` FROM EXHIBITORS WHERE year = $year ORDER BY company ASC"; $result = mysql_query($query) or die("There was a problem with the SQL query: " . mysql_error()); if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_array($result)) { $testCol = substr($row['table'], 0, 1); if ($testCol == 'A') { $a[$row['company']] = $row['table']; } else if ($testCol == 'B') { $b[$row['company']] = $row['table']; } else if ($testCol == 'C') { $c[$row['company']] = $row['table']; } } echo "<table>"; foreach($a as $key => $value){ echo "<tr><td>company: $key</td><td>Table: $value </td></tr>"; } echo "</table>"; echo "<table>"; foreach($b as $key => $value){ echo "<tr><td>company: $key</td><td'>Table: $value </td></tr>"; } echo "</table>"; echo "<table>"; foreach($c as $key => $value){ echo "<tr>"<td>company: $key</td><td>Table: $value </td></tr>"; } echo "</table>"; } else { echo "No Exhibitor data for the year $_POST[selectByYear]"; } Quote Link to comment https://forums.phpfreaks.com/topic/252259-associative-array-3-values/ Share on other sites More sharing options...
requinix Posted December 2, 2011 Share Posted December 2, 2011 Go for multiple dimensions. But before I go on, why are you using only the first letter of the table? Are you trying to group the results by the table? Because there is a better way to do that. Quote Link to comment https://forums.phpfreaks.com/topic/252259-associative-array-3-values/#findComment-1293299 Share on other sites More sharing options...
tHud Posted December 2, 2011 Author Share Posted December 2, 2011 Hello The exhibitor is assigned an exhibition table. It starts with either A, B or C. (A12, C24 etc.) I'm only using the first letter to decide which array the 'e-table' belongs in. Then I want to add it to its appropriate array for outputting on the screen later. Hope that clears it up. I'm open to all advice. Quote Link to comment https://forums.phpfreaks.com/topic/252259-associative-array-3-values/#findComment-1293395 Share on other sites More sharing options...
requinix Posted December 2, 2011 Share Posted December 2, 2011 Then I want to add it to its appropriate array for outputting on the screen later. Which you're doing by grouping the exhibitors by their table letter? Is that the only reason you're grabbing the letters or is there something else? Quote Link to comment https://forums.phpfreaks.com/topic/252259-associative-array-3-values/#findComment-1293601 Share on other sites More sharing options...
tHud Posted December 2, 2011 Author Share Posted December 2, 2011 err yeah... (You can tell I'm not much good at this can't you...) I've since decided to take your advice and go down the multi-deminsional array method. I got the results into the array by doing this... while($row = mysql_fetch_array($result)) { $results[] = $row; A typical element outputted via print_r looks like this.. [2] => Array ( [0] => 81 [id] => 81 [1] => pongo supplies [company] => pongo supplies [2] => C10 [table] => C10 [3] => 1 [status] => 1 [4] => 0000-00-00 [paid] => 0000-00-00 ) In the example above, the vendor has been assigned Table C10 What I'm not sure about, is.... How could I now create 3 (HTML) tables grouped by element2 'table' where the table groups are A, B or C ? Thanks so much for the help Quote Link to comment https://forums.phpfreaks.com/topic/252259-associative-array-3-values/#findComment-1293631 Share on other sites More sharing options...
requinix Posted December 2, 2011 Share Posted December 2, 2011 Since you are just doing this to group them, I'll tell you the better way that I alluded to earlier. You don't need arrays. Order your query by the table name first and then whatever else you want (ie, the company name). Then loop through the results one-by-one: if the previous table('s letter) doesn't match the current table('s letter) then start a new section for it. // ... $year = (int)$_POST["selectByYear"]; // ... $query = "SELECT `id`, `company`, `table` FROM EXHIBITORS WHERE `year` = $year ORDER BY `table` ASC, `company` ASC"; $result = mysql_query($query) or die("Database error"); // or die("There was a problem with the SQL query: " . mysql_error()); // only show the message when debugging a bad query if (mysql_num_rows($result) > 0) { $table = null; while ($row = mysql_fetch_array($result)) { // check if we changed tables if ($table != $row["table"][0]) { // end the previous table, if any if ($table) { echo ""; } $table = $row["table"][0]; // start the next table echo "{$table}"; echo "</pre> <table>"; } // print the table row echo "Company: {$row["company"]}Table: {$row["table"]}"; } // end the final table echo "</table>";<br>} else {<br>echo "No Exhibitor data for the year {$year}." Quote Link to comment https://forums.phpfreaks.com/topic/252259-associative-array-3-values/#findComment-1293637 Share on other sites More sharing options...
tHud Posted December 2, 2011 Author Share Posted December 2, 2011 Wow! That certainly does what I want. Thank you However there is a small glitch.... Notice: Uninitialized string offset: 0 in /../info.php on line 137 line #137 equates to the first line of this code chunk... if ($table != $row["table"][0]) { // end the previous table, if any if ($table) { echo "</table>"; } $table = $row["table"][0]; // start the next table echo "<h1>{$table}</h1>"; echo "<table>"; } Quote Link to comment https://forums.phpfreaks.com/topic/252259-associative-array-3-values/#findComment-1293667 Share on other sites More sharing options...
requinix Posted December 2, 2011 Share Posted December 2, 2011 Then the table name is empty... Quote Link to comment https://forums.phpfreaks.com/topic/252259-associative-array-3-values/#findComment-1293677 Share on other sites More sharing options...
tHud Posted December 2, 2011 Author Share Posted December 2, 2011 Aaaaaahhhhhh... requinix, I appreciate your patience, the world is a better place with you in it! Thank you Quote Link to comment https://forums.phpfreaks.com/topic/252259-associative-array-3-values/#findComment-1293680 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.