Jump to content

Associative array - 3 values?


tHud

Recommended Posts

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]"; }

 

Link to comment
Share on other sites

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. :) 

Link to comment
Share on other sites

:-[  :-[ 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 :)

Link to comment
Share on other sites

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}."

Link to comment
Share on other sites

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>";

	}

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.