ShadowThrone
19-01-2016, 22:40
Premessa: non sono un programmatore, ma per necessità devo esportare delle tabelle MySql in Excel. Per fare ciò ho creato due file ed il tutto funziona, peccato che il file excel si porti dietro anche il form html.
http://s16.postimg.org/62etw9lgl/problem.png
INDEX.PHP
<?php
$con = mysql_connect("IP","user","pwd");
$db = mysql_select_db("DB",$con);
$get = mysql_query("SHOW TABLES FROM DB");
$option = '';
while($row = mysql_fetch_array($get))
{
$option .= '<option value = "'.$row[0].'">'.$row[0].'</option>';
}
?>
<html>
<body>
<form action="#" method="post">
<h3>Select table</h3>
<select name="table">
<?php echo $option; ?>
</select>
<input type="submit" name="submit" value="Get Table" />
</form>
<?php
if(isset($_POST['submit']))
{
$selected_val = $_POST['table'];
echo "You have selected :" .$selected_val;
$myFile="/tmp/".test;
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $selected_val);
fclose($fh);
include 'SelectTable.php';
}
?>
</body>
</html>
SELECTABLE.PHP
<?php
header("Content-type: application/vnd-ms-excel");
header("Content-Disposition: attachment; filename=TEST.xls");
$tablesel=file_get_contents("/tmp/".test.txt);
$result = mysql_query('SELECT Time, Temp1, Temp2, Temp3, Temp4 FROM '.$tablesel) or die('cannot show columns from '.$tablesel);
if(mysql_num_rows($result))
{
echo '<table cellpadding="0" cellspacing="0" class="db-table">';
echo '<tr><th>Time</th><th>Temp1</th><th>Temp2</th><th>Temp3</th><th>Temp4</tr>';
while($row2 = mysql_fetch_row($result))
{
echo '<tr>';
foreach($row2 as $key=>$value)
{
echo '<td>',$value,'</td>';
}
echo '</tr>';
}
echo '</table><br />';
}
?>
http://s16.postimg.org/62etw9lgl/problem.png
INDEX.PHP
<?php
$con = mysql_connect("IP","user","pwd");
$db = mysql_select_db("DB",$con);
$get = mysql_query("SHOW TABLES FROM DB");
$option = '';
while($row = mysql_fetch_array($get))
{
$option .= '<option value = "'.$row[0].'">'.$row[0].'</option>';
}
?>
<html>
<body>
<form action="#" method="post">
<h3>Select table</h3>
<select name="table">
<?php echo $option; ?>
</select>
<input type="submit" name="submit" value="Get Table" />
</form>
<?php
if(isset($_POST['submit']))
{
$selected_val = $_POST['table'];
echo "You have selected :" .$selected_val;
$myFile="/tmp/".test;
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $selected_val);
fclose($fh);
include 'SelectTable.php';
}
?>
</body>
</html>
SELECTABLE.PHP
<?php
header("Content-type: application/vnd-ms-excel");
header("Content-Disposition: attachment; filename=TEST.xls");
$tablesel=file_get_contents("/tmp/".test.txt);
$result = mysql_query('SELECT Time, Temp1, Temp2, Temp3, Temp4 FROM '.$tablesel) or die('cannot show columns from '.$tablesel);
if(mysql_num_rows($result))
{
echo '<table cellpadding="0" cellspacing="0" class="db-table">';
echo '<tr><th>Time</th><th>Temp1</th><th>Temp2</th><th>Temp3</th><th>Temp4</tr>';
while($row2 = mysql_fetch_row($result))
{
echo '<tr>';
foreach($row2 as $key=>$value)
{
echo '<td>',$value,'</td>';
}
echo '</tr>';
}
echo '</table><br />';
}
?>