Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is somewhat at the edge of practicality, but I'm approaching this problem more as an exercise in "doing it right."</p> <p>I would do a simple select of each table and insert in to an array:</p> <pre>$query1 = "SELECT * FROM cities"; $query2 = "SELECT * FROM prices"; $results1 = mysql_query( $query1 ); $results2 = mysql_query( $query2 ); while( $rows = mysql_fetch_array( $results1 ) ) $cities[] = $row['name']; while( $rows = mysql_fetch_array( $results2 ) ) $prices[] = array( $row['city1_id'], $row['city2_id'], $row['name'] ); </pre> <p>I would then use this to dynamically create two javascript lists:</p> <pre>$java_array1 = "var Cities = new Array;\n"; foreach( $cities as $key=>$city ) $java_array1 .= "Cities[$key] = \"$city\";\n"; $java_array2 = "var Prices = new Array;\n"; foreach( $cities as $key=>$price ) $java_array2 .= "Prices[$key] = new Array( \"{$price[0]}\", \"{$price[1]}\", \"{$price[2]}\", );\n";</pre> <p>I would next output a table with carefully crafted ids for each cell. I would use code very similar to that in the first answer, but I would give each cell a unique id of <code>"cell-&lt;row&gt;-&lt;col&gt;"</code>.</p> <p>The last thing I would do would be whip up some <code>onload</code> javascript which would populate the table using the appropriate pairings. My javascript is quite rusty, but it would look something like this (NOTE: pseudocode follows):</p> <pre>n = 1; for( var i in cities ) { // set city names on the diagonal document.getElementById( 'cell-'&i&'-'&i ).innerHTML = names[i]; n = n + 1; } for( var j in prices ) { // use the city IDs in the prices array (see earlier code initializing // prices array) to place the prices document.getElementById( 'cell-'&prices[1]&'-'&prices[2] ).innerHTML = prices[0]; }</pre> <p>I almost certainly messed up some of that javascript; use at your own risk.</p> <p>This is a lot more complex than the simpler answer given above, but this is the only way I can think of doing it so that it makes "sense." Basically, with this method, you place everything where it <em>should</em> be, and you don't have to worry about odd tables and weird selects.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload