Hello
I want to merge data from two tables and then post it into html table.
I have two database tables, one is: model_periferie (is like type of periphery) and another is periferie (periphery). Now, I want to show periphery and type of periphery in html table.
Problem is select returns two rows , but just the first row is showed and there is not showed type of periphery. I think I have error in my two FOR cycles which shows the data in html table
Here is piece of my code:
<?php
/*
THIS PIECE OF CODE SHOULD RETUR PERIPHERY FOR ACTUAL SELECTED COMPUTER
*/
//Connection to database (function is based in another file)
$db_conn = db_connect();
if (!$db_conn)
{
return "Nepodarilo sa pripojiť k databáze - skúste to prosím neskôr.";
pata_html();
exit;
}
//Select from model_periferie (model_periferie its like type or model of periphery)
//returns no error and in this tabe are this data
$query_mod_per = "select * from model_periferie";
$result_mod_per = $db_conn->query($query_mod_per);
If (!$result_mod_per)
{
echo '<br />Chyba! Pri výbere modelu periférie nastala chyba<br />';
pata_html();
exit;
}
//Check if id_pc was POSTET from hidden field
If ($_POST['hid_id_pc'])
{
$id_pc = $_POST['hid_id_pc'];
$vc_pc = $_POST['hid_vc'];
//Select data (periphery)where id_pc is equal to posted (id_pc)
//This select returns two rows
$query_per = "select * from periferie where id_pc= '$id_pc' order by vyrobne_cislo";
$result_per = $db_conn->query($query_per);
$num_results = $result_per->num_rows;
If (!result_per)
{
echo 'Chyba!!! Nastala chyba pri vycitavani periferii podla pocitaca';
}
//$num_results = $result_per->num_rows;
IF ($result_per->num_rows > 0)
{
?>
<form action="pridat_per_form.php" method="post">
<table border=0 cellpadding=4>
<?php
echo '<tr><td></td><td>Model periférie</td><td>Výrobné číslo</td><td>Inventárne číslo</td><td>Upraviť</td></tr>';
//In next two for cycles(loops) I want to merge type of periphery to actual showed periphery and show it
for ($i=0; $i < $result_per->num_rows; $i++) //shows selected periphery row
{
$row=$result_per->fetch_assoc();
$id_periferie=$row['id_periferie'];
echo '<tr><td><strong>'.($i+1).'. </strong></td>';
//next for should show model_periphery row equal to showed periphery
for ($i=0; $i < $result_mod_per->num_rows; $i++)
{
$row_mod=$result_mod_per->fetch_assoc();
If ($row_mod['id_model_periferie'] == $row['id_model_periferie'])
echo '<td>'. $row['vyrobca'] . ' ' . $row['model'] .'</td>';
}
echo '<td>'.htmlspecialchars(stripslashes($row['vyrobne_cislo'])).'</td>';
echo '<td>'.stripslashes($row['inventarne_cislo']).'</td>';
echo "<td><input type='radio' name='rad_edit_pc[]' value='$id_periferie'></td>";
}
$result_mod_per->free();
$result_per->free();
}
else
{
echo 'K danému počítaču neboli priradené žiadne periférie.';
}
}
?>
</table>