views:

64

answers:

3

Hi! I'm trying to load data from database and display it in table like here: http://img196.imageshack.us/img196/1857/cijene.jpg In database i have 2 tables: cities (id, name) prices (id, city1_id, city2_id, price)

For example, the printed table for 4 cities would look like this:

<table>
 <tr>
  <td>city1</td>
  <td> </td>
  <td> </td>
  <td> </td>
 </tr>

 <tr>
  <td> price_city1-city2</td>
  <td>city2</td>
  <td> </td>
  <td> </td>
 </tr>

 <tr>
  <td> price_city1-city3</td>
  <td> price_city2-city3</td>
  <td>city3</td>
  <td> </td>
 </tr>

 <tr>
  <td> price_city1-city4</td>
  <td> price_city2-city4</td>
  <td> price_city3-city4</td>
  <td>city4</td>
 </tr>
</table>

Does someone know what's the php statement to echo this kind of tables?

Thanks in advance, Ile

+1  A: 

I've used a very simple approach to converting the results of a query to an HTML table. I test that $query_result is true and fetch the results as an associative array...

$query_result = mysqli_query($db, $query);

if ($query_result) {

    while ($columns = mysqli_fetch_assoc($query_result)) {

        echo "<tr>\n";

        foreach ($columns as $name => $column) {
            echo "<td>";
            printf("%s",$columns[$name]);
            echo "</td>\n";
        }

        echo "</tr>\n";
    }
}

EDIT: Now I've been able to look at your table, I can see that I haven't really answered your question. The query is obviously very important. Does the question become 'how do I create a query which returns results which can be turned into a table by my simple-minded approach?'

I hope someone else has a few ideas.

pavium
Thank you for answer, but I think that you didn't understand my question, or what would be more truthful is that I didn't asked it proper :)Please check this image: http://img196.imageshack.us/img196/1857/cijene.jpg Price from Beograd to Novi Sad is 100. Price from Beograd to Nis is 200 etc.
ile
+1  A: 
eykanal
+1  A: 
// This single query gets all required data
// NOTE: this query assumes that your price data is entered
//   such that from_city always alphabetically precedes to_city!
$sql =
    "SELECT a.name AS from_city, b.name AS to_city, price ".
    "FROM prices ".
    "INNER JOIN cities AS a ON a.id=prices.city1_id ".
    "INNER JOIN cities AS b ON b.id=prices.city2_id";


// connect and do query
$conn = mysql_connect($host, $user, $pass);
mysql_select_db($db, $conn);
$q = mysql_query($sql, $conn);


// Stuff all data into an array for manipulation;
//   keep list of all cities mentioned
$price = array();
$cities = array();
while (($res = mysql_fetch_assoc($q)) !== false) {
    $from = $res['from_city'];
    $cities[ $from ] = true;

    $to = $res['to_city'];
    $cities[ $to ] = true;

    $price[$to][$from] = $res['price'];
}

// Invert to get alphabetical list of cities
$cities = array_keys($cities);
sort($cities);
$num = count($cities);


// some utility functions
function table($rows) {
    return '<table>'.join($rows).'</table>';
}
function row($cells) {
    return '<tr>'.join($cells).'</tr>';
}
function cell($text) {
    $text = (string) $text;
    if (strlen($text)==0)
        $text = '&nbsp;';
    return '<td>'.$text.'</td>';
}


// The data is now in the desired order;
// produce output as HTML table
$rows = array();
for ($to = 0; $to < $num; ++$to) {
    $t = $cities[$to];
    $cells = array();

    for ($from = 0; $from < $to; ++$from) {
        $f = $cities[$from];

        if isset($price[$t]) && isset($price[$t][$f])
            $text = $price[$t][$f];
        else
            $text = '';

        $cells[]= cell($text);
    }

    $cells[]= cell($t);    // add destination-label

    for ($from = $to+1; $from < $num; ++$from)   // pad to proper number of cells
        $cells[]= cell('');

    $rows[]= row($cells);
}
$html = table($rows);


// show results!
echo $html;
Hugh Bothwell
Hi Hugh,thanks a lot for answering, this was very helpful!Cheers,Ilija
ile