<?php

function afficheInfos($key, $value)
{
	if ($key != "id" ) {
		echo "<p class='{utf8_encode($key)}'><b>".utf8_encode($key)."</b></p>";
		echo "<p>".utf8_encode($value)."</p>";
	}
}

$id = $_GET['id'];

$db = mysql_connect('localhost', 'root', 'm148r43a9Y@%Z')  or die ('Erreur de connexion '.mysql_error());

mysql_select_db('smartpassTest2',$db) or die ('Erreur de selection '.mysql_error());

$sql = "SHOW TABLES FROM smartpassTest2";
$tables = mysql_query($sql);

echo'
	 <!DOCTYPE html>
        <head>
          <title>Fiche point</title>
          <link rel="stylesheet" href="style.css">
          <meta charset="UTF-8">
        </head>';

echo "<a href='points.php'>Accueil</a>";
echo "<a href='confSuppression.php?id={$id}'>Supprimer</a>";
echo "<a href='mdfForm.php?id={$id}'>Modifier</a>";

echo "<h1>Modification du point</h1>";

echo "<form action='update.php?id={$id}' method='post'>";

while ($row = mysql_fetch_row($tables)) {
	if($row[0]!="Parcours" and $row[0]!="photos" and $row[0]!="ips"){
		
		$sql = "SELECT * FROM {$row[0]} WHERE id = {$id}";
		$data = mysql_fetch_array(mysql_query($sql));		
		
		foreach ($data as $key => $value) {
			if (!is_null($value) and !is_integer($key)) {
				if($key != "id"){

					$tableName = strval($row[0]);
					$columnName = strval($key);

					$champs = mysql_fetch_array(mysql_query("SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '{$tableName}' and COLUMN_NAME = '{$columnName}' GROUP BY DATA_TYPE"));

					echo "<p class='nomChamp'>{$key}</p>";
					
					foreach ($champs as $nom => $size) {					

						$dataChamps = explode(")",$champs[0]);
						$dataChamps = explode ( "(" , $dataChamps[0]);

						if ($dataChamps[0] == "int" and $dataChamps[1] == 1){
							if ($value == 1){
								echo "<input type='radio' name='{$tableName}_{$columnName}' value = '1' checked>Oui
								<input type='radio' name='{$tableName}_{$columnName}' value = '0'>Non";
							}
							else{
								echo "<input type='radio' name='{$tableName}_{$columnName}' value = '1'>Oui
								<input type='radio' name='{$tableName}_{$columnName}' value = '0' checked>Non";
							}
						}
						else{
							echo "<input type='text' name='{$tableName}_{$columnName}' value = '".htmlspecialchars(($value),ENT_QUOTES)."'>";
						}
						echo "</br>";
						break;
					}
				}
			}
		}
	}		
}

echo "<input type = 'submit' value ='Modifier' id='submit'>";

echo "</form>";

?>
