creare tabel cu date din mysql

Discutii despre script-uri si coduri PHP-MySQL, precum si lucru cu XML in PHP.
bita41
Mesaje:4

creare tabel cu date din mysql

Salut,poate este cineva sa ma poata ajuta la crearea unui tabel cu date extrase din baza de date..

ceva de genul

Cod: Selectaţi tot

$data = array( 0 => array( 'nume' => 'pedala', 'nume' => 'gigi' ),
  1 => array( 'nume' => 'a', 'nume' => 'b' ) );
  echo '<table>';
 foreach ( $data as $rand ) { echo '<tr>'; foreach ( $rand as $value ) { echo '<td>' . $value . '</td>'; } echo '</tr>'; }
 echo '</table>';
sursa paginii va arata asa:
<table>
<tr>
<td>pedala</td> <td>gigi</td>
</tr>
<tr>
<td>a</td> <td>b</td>
</tr>
</table>

idea este ca mie datele imi vin intr-un array de genul

Cod: Selectaţi tot

while ($row = mysqli_fetch_assoc($result)) {
 $randuri[] = $row;
}
l-am creat asa ca sa fie cu douo dimensiuni ca in exemplul de mai sus...sper ca m,-am facut inteles..
sau poate stie cineva o modalitate de a crea un tabel cu douo coloane generat cu datele ce vin din baza de date..

MarPlo Mesaje:4343
Array-ul nu poate contine 2 elemente diferite, cu aceeasi cheie, dupa cum apare in postarea de sus

Cod: Selectaţi tot

array( 'nume' => 'a', 'nume' => 'b' )
Ar trebui specificat exact ce coloane scoti din mysql si ce array sa formeze, numele si valoarea lor.

bita41 Mesaje:4
Salut si multumesc pentru raspunsul rapid..
am sa incerc sa dau mai multe detalii

In baza de date am tabelul asa

+----+--------+------+
| id | parinte | name |
+----+--------+------+
| 1 | 0 | val1 |
| 2 | 0 | val2 |
| 3 | 0 | val3|
| 4 | 1 | val1_1 |
| 5 | 1 | val2_1|
| 6 | 1 | val3_1 |
| 7 | 2 | val1_2|
| 8 | 2 | val2_2 |
| 9 | 2 | val3_2 |
| 10 | 3 | val1_3 |
| 11 | 3 | val2_3 |
| 12 | 3 | val3_3 |
| 13 | 0 | val4 |
+----+--------+------+
Din cate se vede val1 spre ex. este o categorie iar val3_1 este o subcategorie a lui val1

interogarea este asa

Cod: Selectaţi tot

$sql = "SELECT c.name, c.parinte, c.id FROM categorii c ORDER BY id ASC";
$result = mysqli_query($conn, $sql) or trigger_error("Query: $sql\n<br />MySQL Error: " . mysqli_error($conn));
Ca sa se vada array-ul rezultat am sa dau un var_dump()

Cod: Selectaţi tot

while ($row = mysqli_fetch_assoc($result)) {
          $categorii[] = $row;
          var_dump($categorii);
         }
rezultatul arata ceva de genul

Cod: Selectaţi tot

array(1) { [0]=> array(3) { ["name"]=> string(4) "val1" ["parinte"]=> string(1) "0" ["id"]=> string(1) "1" } }

array(2) { [0]=> array(3) { ["name"]=> string(4) "val1" ["parinte"]=> string(1) "0" ["id"]=> string(1) "1" }
           [1]=> array(3) { ["name"]=> string(4) "val2" ["parinte"]=> string(1) "0" ["id"]=> string(1) "2" } }
		   
array(3) { [0]=> array(3) { ["name"]=> string(4) "val1" ["parinte"]=> string(1) "0" ["id"]=> string(1) "1" }
           [1]=> array(3) { ["name"]=> string(4) "val2" ["parinte"]=> string(1) "0" ["id"]=> string(1) "2" }
		   [2]=> array(3) { ["name"]=> string(4) "val3" ["parinte"]=> string(1) "0" ["id"]=> string(1) "3" } }		   
................		   
array(81) { [0]=> array(3) { ["name"]=> string(4) "val1" ["parinte"]=> string(1) "0" ["id"]=> string(1) "1" } 
                        [1]=> array(3) { ["name"]=> string(4) "val2" ["parinte"]=> string(1) "0" ["id"]=> string(1) "2" } 
			[2]=> array(3) { ["name"]=> string(4) "val3" ["parinte"]=> string(1) "0" ["id"]=> string(1) "3" } 
			[3]=> array(3) { ["name"]=> string(4) "val4" ["parinte"]=> string(1) "0" ["id"]=> string(1) "4" } 
			[4]=> array(3) { ["name"]=> string(4) "val5" ["parinte"]=> string(1) "0" ["id"]=> string(1) "5" } 
			[5]=> array(3) { ["name"]=> string(4) "val6" ["parinte"]=> string(1) "0" ["id"]=> string(1) "6" }
			
	                [6]=> array(3) { ["name"]=> string(6) "val1_1" ["parinte"]=> string(1) "1" ["id"]=> string(2) "21" } 
			[7]=> array(3) { ["name"]=> string(6) "val2_1" ["parinte"]=> string(1) "1" ["id"]=> string(2) "22" } 
			[8]=> array(3) { ["name"]=> string(6) "val3_1" ["parinte"]=> string(1) "1" ["id"]=> string(2) "23" } 
			
			[9]=> array(3) { ["name"]=> string(6) "val1_2" ["parinte"]=> string(1) "2" ["id"]=> string(2) "30" } 
			[10]=> array(3) { ["name"]=> string(6) "val2_2" ["parinte"]=> string(1) "2" ["id"]=> string(2) "31" }
			
			[11]=> array(3) { ["name"]=> string(6) "val1_3" ["parinte"]=> string(1) "3" ["id"]=> string(2) "40" } 
			[12]=> array(3) { ["name"]=> string(6) "val2_3" ["parinte"]=> string(1) "3" ["id"]=> string(2) "41" } 
			[13]=> array(3) { ["name"]=> string(6) "val3_3" ["parinte"]=> string(1) "3" ["id"]=> string(2) "42" } 
			[14]=> array(3) { ["name"]=> string(6) "val4_4" ["parinte"]=> string(1) "3" ["id"]=> string(2) "43" }
			....
Daca as face var_dump($row);

Cod: Selectaţi tot

array(3) { ["name"]=> string(4) "val1" ["parinte"]=> string(1) "0" ["id"]=> string(1) "1" }
array(3) { ["name"]=> string(4) "val2" ["parinte"]=> string(1) "0" ["id"]=> string(1) "2" }
array(3) { ["name"]=> string(4) "val3" ["parinte"]=> string(1) "0" ["id"]=> string(1) "3" }
array(3) { ["name"]=> string(4) "val4" ["parinte"]=> string(1) "0" ["id"]=> string(1) "4" }
array(3) { ["name"]=> string(4) "val5" ["parinte"]=> string(1) "0" ["id"]=> string(1) "5" }
array(3) { ["name"]=> string(4) "val6" ["parinte"]=> string(1) "0" ["id"]=> string(1) "6" }

array(3) { ["name"]=> string(6) "val1_1" ["parinte"]=> string(1) "1" ["id"]=> string(2) "21" } 
array(3) { ["name"]=> string(6) "val2_1" ["parinte"]=> string(1) "1" ["id"]=> string(2) "22" } 
array(3) { ["name"]=> string(6) "val3_1" ["parinte"]=> string(1) "1" ["id"]=> string(2) "23" } 

array(3) { ["name"]=> string(6) "val1_2" ["parinte"]=> string(1) "2" ["id"]=> string(2) "30" } 
array(3) { ["name"]=> string(6) "val2_2" ["parinte"]=> string(1) "2" ["id"]=> string(2) "31" }
 
array(3) { ["name"]=> string(6) "val1_3" ["parinte"]=> string(1) "3" ["id"]=> string(2) "40" } 
array(3) { ["name"]=> string(6) "val2_3" ["parinte"]=> string(1) "3" ["id"]=> string(2) "41" } 
array(3) { ["name"]=> string(6) "val3_3" ["parinte"]=> string(1) "3" ["id"]=> string(2) "42" } 
array(3) { ["name"]=> string(6) "val4_3" ["parinte"]=> string(1) "3" ["id"]=> string(2) "43" }
ok din datele astea vroiam sa am un tabel su douo coloane ceva de genul
<table>
<tr>
<td>val1
val1_1
val2_1
val3_1</td> <td>val2
val1_2
val2_2</td>
</tr>
<tr>
<td>val3
val1_3
val2_3
val3_3</td> <td>val4
val1_4
val2_4
val3_4</td>
</tr>
</table>
Sper ca m-am facut inteles..
Problema este ca mie imi construieste tabelul dar in unele locuri am cate 3 coloane in altele doar una poate nu este buna metoda mea si poate stia cineva alta

Multumesc

MarPlo Mesaje:4343
Am inteles, dar daca ai numar diferit de coloane, nu stiu vreo solutie, adica de unde sa cunosti cand sunt 3 coloane si cand doar una.

Subiecte similare