Transpunere randuri din tabel mysql in coloane tabel html

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

Transpunere randuri din tabel mysql in coloane tabel html

Salutare,

Am urmatorul tabel (mai jos este un demo, tabelul se insira pe mai multe questions)

Cod: Selectaţi tot

+----+-----+-----+-----+-----+
| ID | Q_1 | Q_2 | Q_3 | Q_4 |
+----+-----+-----+-----+-----+
| 1  |  2  |  3  |  4  |  5  |
| 2  |  4  | 10  |  9  |  8  |
| 3  |  1  |  5  |  4  |  7  |
+----+-----+-----+-----+-----+
si incerc sa il transpun intr-un tabel html sub forma aceasta:

Cod: Selectaţi tot

+-----+---+----+---+
|  Q  | 1 | 2  | 3 |
+-----+---+----+---+
| Q_1 | 2 | 4  | 1 |
| Q_2 | 3 | 10 | 5 |
| Q_3 | 4 | 9  | 4 |
| Q_4 | 5 | 8  | 7 |
+-----+---+----+---+
am inceput codul meu asa:

Cod: Selectaţi tot

$sql = "SELECT *
           FROM table_1";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    $thead = null;
    $q_1 = [];
    $q_2 = [];
    $q_3 = [];
    $q_4 = [];
    $counter = 1 //folosesc acest counter pentru a imi crea <th>-urile, acesta fiind egal cu numarul de randuri ale tabelei sql
    while ($row = $result->fetch_assoc()) {
    	$thead = '<th>' . $counter . '</th>';
    	$counter++;
	$q_1 [] = $row['Q_1'];
	$q_2 [] = $row['Q_2'];
	$q_3 [] = $row['Q_3'];
	$q_4 [] = $row['Q_4'];
    }
    foreach ($q_1 as $key => $value) {
	$i_1 [] = '<td>' . $value . '</td>';
    }
}
echo '<table>';
echo '<thead>';
echo <tr><td>INTREBARI</td>;
echo '<td>' . $thead . '</td></tr>';
echo '</thead>';
echo '<tbody>';
echo '<tr><td>Question 1</td>';
echo $i_1. '</tr>';
echo '</tbody>';
echo '</table>';
Nu stiu cum pot scoate valorile din array-urile $i_1, $i_2... si sa le transpun intr-o variabila ca ulterior sa o aduc in scrierea tabelului html.

Multumesc!

MarPlo Mesaje: 4343
Salut
Vezi daca iti e de folos codul acesta:

Cod: Selectaţi tot

$q_mysql =[
  'q_1'=>[2, 4, 1],
  'q_2'=>[3, 10, 5],
  'q_3'=>[4, 9, 4],
  'q_4'=>[5, 8, 7]
];
$t_html ='<table border="1"><thead><tr><td>INTREBARI</td><td>1</td><td>2</td><td>3</td><td>4</td></tr><thead>';
foreach($q_mysql as $k=>$ar){
  $t_html .='<tr><td>'. $k .'</td><td>'. implode('</td><td>', $ar) .'</td></tr>';
}
$t_html .='</table>';

echo $t_html;

sterica Mesaje: 285
am incercat codul, insa imi returneaza o eroare: implode(): Invalid arguments passed in am reediat codul sub forma:

Cod: Selectaţi tot

$t_html .='<tr><td>'. $k .'</td><td>'. implode('</td><td>', array($ar)) .'</td></tr>';
si a functionat.

ideea ta vreau sa o folosesc in acest cod:

Cod: Selectaţi tot

foreach ($q_d1 as $k=>$v) {
  $t_body_d1 .= "<td><a href='#/' class='too-long' title='Optional Title' data-content='" .$v. "' data-placement='bottom'><span class='glyphicon glyphicon-chevron-down'></span></a>" .implode ('</td><td>', array($v)). "</td>";
}
insa aici foreach-ul imi scrie direct in pagina valorile din $v si nu inteleg de ce, pentru ca nu folosesc un echo.
daca elimin array-ul din array($v)) imi returneaza eroarea implode(): Invalid arguments passed in
Multumesc1

MarPlo Mesaje: 4343
In codul pe care l-am postat se foloseste un array 2-dimensional, iar foreach() se aplica la acel array. Ca sa functioneze in scriptul tau, trebuie rescris codul ca datele de la mysql sa fie adaugate intr-un array asemanator.
Pe scurt cam asa:

Cod: Selectaţi tot

if($result->num_rows >0){
  $q_mysql =['q_1'=>[], 'q_2'=>[], 'q_3'=>[], 'q_4'=>[]];

  while($row = $result->fetch_assoc()){
    $q_mysql['q_1'][] = $row['Q_1'];
    $q_mysql['q_2'][] = $row['Q_2'];
    $q_mysql['q_3'][] = $row['Q_3'];
    $q_mysql['q_4'][] = $row['Q_4'];
  }

  foreach($q_mysql as $k=>$ar){
    $t_body .='<tr><td>'. $k .'</td><td>'. implode('</td><td>', $ar) .'</td></tr>';
  }
}
- La mine a functionat codul fara: array($ar) fiindca deja $ar e sub-array-ul de la fiecare cheie $k.

sterica Mesaje: 285
Acum am inteles, multumesc mult de ajutor

Subiecte similare