2015-03-29 2 views
0

У меня есть эта таблица «город» в моей базе данных:Как создать динамическую матрицу в php?

 
|id |id_city_a |id_city_b|distance| 
|1 |1   | 1 | 0  | 
|2 |1   | 2 | 8  | 
|3 |1   | 3 | 6  | 
|4 |2   | 1 | 8  | 
|5 |2   | 2 | 0  | 
|6 |2   | 3 | 9  | 
|7 |3   | 1 | 6  | 
|8 |3   | 2 | 9  | 
|9 |3   | 3 | 0  | 

Я хочу, чтобы конечный результат, чтобы быть в матрице, такие как:

| | 1 | 2 | 3 | | 1 | 0 | 8 | 6 | | 2 | 8 | 0 | 9 | | 3 | 6 | 9 | 0 |

This is my code :

function random() { include('config/koneksi.php'); $result = mysql_query("select * from temp_hasil"); $n =mysql_num_rows(mysql_query("SELECT * FROM temp_hasil")); for ($i = 1; $i <= $n; $i++) { for ($j = 1; $j <= $n; $j++) { $rows = mysql_fetch_array($result); $this->table[$i][$j] = $i == $j ? INF : $rows['id']; } } } function __toString() { $str = '<table class="table table-bordered" id="tableInput"> <tbody>'; $str .= '<tr><td></td>'; foreach ($this->table as $rowName => $row) { $str .= "<td>$rowName</td>"; } $str .= '</tr>'; foreach ($this->table as $rowName => $row) { $str .= "<tr><td>$rowName</td>"; foreach ($row as $columnName => $value) { $str .= "<td>"; $str .= '<input class="form-control" type="text" value="' . $value . '" name="table[' . $rowName . '][' . $columnName . ']" requied' . ($columnName == $rowName ? ' disabled' : '') . '>'; $str .= "</td>"; } $str .= '</tr>'; } $str .= '</tbody></table>'; return $str; } } $str .= '</tr>'; foreach ($this->table as $rowName => $row) { $str .= "<tr><td>$rowName</td>"; foreach ($row as $columnName => $value) { $str .= "<td>"; $str .= '<input class="form-control" type="text" value="' . $value . '" name="table[' . $rowName . '][' . $columnName . ']" requied' . ($columnName == $rowName ? ' disabled' : '') . '>'; $str .= "</td>"; } $str .= '</tr>'; } $str .= '</tbody></table>'; return $str; }

`

Как закодировать его в PHP? пожалуйста помогите.

ответ

0

Сделать индекс city_a полностью отличным от индекса city_b, поэтому его легко проверить.

// generate a two-dimensional matrix in here 
$distMatrix = array(); 

foreach($tableRows as $cityDist) { 
    $from = $cityDist['id_city_a']; 
    $to = $cityDist['id_city_b']; 
    $dist = $cityDist['distance']; 

    $distMatrix[$from][$to] = $dist; 
} 

Показать как HTML таблицы ...

echo '<table border="1">'; 
echo '<tr>'; 
echo '<td>', '#', '</td>'; 
foreach(array_keys(current($distMatrix)) as $city_b) { // city_b headings 
    echo '<td>', $city_b ,'</td>'; 
} 
echo '</tr>'; 

foreach(array_keys($distMatrix) as $city_a) { // need the city_a as row index 
    echo '<tr>'; 
    echo '<td>', $city_a, '</td>'; // city_a ad 
    foreach(array_keys($distMatrix[$city_a]) as $city_b) { // need the city_b as column index 
     echo '<td>', $distMatrix[$city_a][$city_b], '</td>'; // distance from the matrix; 
    } 
    echo '</tr>'; 
} 
echo '</table>'; 

Тестовые данные - данные, используемые из @ashkufaraz

// changed the city ids so we can easily see city_a and city_b 
$tableRows[0]=array("id"=>1, "id_city_a"=>1, "id_city_b"=>11, "distance"=>0); 
$tableRows[1]=array("id"=>2, "id_city_a"=>1, "id_city_b"=>12, "distance"=>8); 
$tableRows[2]=array("id"=>3, "id_city_a"=>1, "id_city_b"=>13, "distance"=>6); 
$tableRows[3]=array("id"=>4, "id_city_a"=>2, "id_city_b"=>11, "distance"=>8); 
$tableRows[4]=array("id"=>5, "id_city_a"=>2, "id_city_b"=>12, "distance"=>0); 
$tableRows[5]=array("id"=>6, "id_city_a"=>2, "id_city_b"=>13, "distance"=>9); 
$tableRows[6]=array("id"=>7, "id_city_a"=>3, "id_city_b"=>11, "distance"=>6); 
$tableRows[7]=array("id"=>8, "id_city_a"=>3, "id_city_b"=>12, "distance"=>9); 
$tableRows[8]=array("id"=>9, "id_city_a"=>3, "id_city_b"=>13, "distance"=>0); 

Выходные:

# 11 12 13 
1 0 8 6 
2 8 0 9 
3 6 9 0 
+0

как подключить этот массив к базе данных? – user4716022

+0

код благодарности. – user4716022

1

попробовать, как этот

Online Demo

$tableRows[0]=array("id"=>1,"id_city_a"=>1,"id_city_b"=>1,"distance"=>0); 
$tableRows[1]=array("id"=>2,"id_city_a"=>1,"id_city_b"=>2,"distance"=>8); 
$tableRows[2]=array("id"=>3,"id_city_a"=>1,"id_city_b"=>3,"distance"=>6); 
$tableRows[3]=array("id"=>4,"id_city_a"=>2,"id_city_b"=>1,"distance"=>8); 
$tableRows[4]=array("id"=>5,"id_city_a"=>2,"id_city_b"=>2,"distance"=>0); 
$tableRows[5]=array("id"=>6,"id_city_a"=>2,"id_city_b"=>3,"distance"=>9); 
$tableRows[6]=array("id"=>7,"id_city_a"=>3,"id_city_b"=>1,"distance"=>6); 
$tableRows[7]=array("id"=>8,"id_city_a"=>3,"id_city_b"=>2,"distance"=>9); 
$tableRows[8]=array("id"=>9,"id_city_a"=>3,"id_city_b"=>3,"distance"=>0); 

$counter=0; 
$result=array(); 
foreach($tableRows as $tableRow) 
{ 
    $result[$tableRow["id_city_a"]][$tableRow["id_city_b"]]=array("id_city_a"=>$tableRow["id_city_a"],"id_city_b"=>$tableRow["id_city_b"],"distance"=>$tableRow["distance"]); 
} 

теперь $result[cityA][cityB]=distance

ПОЛУЧИТЬ unique_id_city_a для столбца

$unique_id_city_a = array_unique(array_map(function ($i) { return $i['id_city_a']; }, $tableRows)); 
echo "\t"; 
foreach($unique_id_city_a as $R) 
{ 
    echo $R."\t"; 
} 
echo "\n"; 

получить unique_id_city_b для строк

$unique_id_city_b = array_unique(array_map(function ($i) { return $i['id_city_b']; }, $tableRows)); 


foreach($result as $R1) 
{  
    echo $unique_id_city_b[$counter++]."\t"; 
    foreach($R1 as $R2) 
    { 
     echo $R2["distance"]."\t"; 
    } 
    echo "\n"; 
} 
+0

код Succes THANK в. – user4716022

+0

Ваш прием;) – ashkufaraz

+0

mr. ashkufaraz, .. у меня новый вопрос. это ссылка на мой вопрос. http://stackoverflow.com/questions/29382428/how-to-query-from-two-tables/29383248#29383248. помогите мне – user4716022

Смежные вопросы