I have a MySQL database where I have a table with several columns and records.
I have made a .php script can made a file .xls with all records can i have in my table.
Everything works fine but the problem occurs when i find an id like this '36E49'.Microsoft Excel format the cell that contains the value '36E49' in scientific and the value become '3,6E+50'.
Example of my code:
<?php
header("Content-type: application/vnd.ms-excel; name='excel'");
header("Content-Disposition: filename=$filename");
?>
<table width="1702" border="1" style="border-collapse: collapse;">
<tr>
<td style="background-color: #F0F0F0; text-align: center;" width="160"><b><u>ID</u></b></td>
<td style="background-color: #F0F0F0; text-align: center;" width="170"><b><u>Description</u></b></td>
</tr>
<?php
$query = mysql_query ("SELECT * FROM ..............");
while($result = mysql_fetch_array($query))
{
$id1 = $result['id1'];
$id2 = $result['id2'];
echo "<tr>";
echo "<td>$id1</td>";
echo "<td>$id2</td>";
echo "</tr>";
}
?>
</table>
Thanks!
I 've solved the problem
For solve my problem i had use the fuction ="value"
Example:
<?php
echo "<td>=\"$id1\"</td>";
?>
Or if you are not in php:
<td><?php echo "=\"$id\""; ?></td>
I 've solved the problem
For solve my problem i had use the fuction ="value"
Example:
<?php
echo "<td>=\"$id1\"</td>";
?>
Or if you are not in php:
<td><?php echo "=\"$id\""; ?></td>
Related
I want to create a PHP page to display the results of a query in a MySQL database under the format of a table. By spending quite some time on different forums, I ended with something that is somehow satisfying me but that is strongly affecting the design and the layout of my webpage. Due to the fact that I wrote the code by a test-fail strategy, it is far from being straightforward and I am sure it is possible to shorten and simplify it and, therefore, make it more compatible with the format of my webpage. Could anybody have a look at it and give some suggestions of general interest about how to solve this kind of issues?
<div id="main">
<?php
require_once('../mysqli_connect.php');
$response = $db->query("SELECT * FROM metabolite");
echo '<table align="center" cellspacing="2" cellpadding="5" border = "1">
<tr><td align="center"><b>Metabolites</b></td>
<td align="center"><b>KEGG Id</b></td>
<td align="center"><b>Synonyms</b></td></tr>';
while ($data = $response->fetch())
{
?>
<tr><td align="left">
<?php echo $data['Metabolite_name']; ?></td>
<td align="left">
KEGG: <?php echo $data['Synonyms']; ?></td>
<td align="left">
<?php echo $data['Synonyms']; ?></td>
</tr>
<?php
}
$response->closeCursor();
?>
</div>
I thank you in advance for all your effort and your help.
Tom.
There's no way we can improve the design and layout of your webpage with the code you've given us. What I can do is write 'better' readable code.
<?php
function tableCell($content)
{
echo '<td align="left">'.$content.'</td>';
}
// database access
require_once('../mysqli_connect.php');
// get all records from the metabolite table
$response = $db->query("SELECT * FROM metabolite");
// start main division
echo '<div id="main">';
// start the table
echo '<table align="center" cellspacing="2" cellpadding="5" border = "1">';
// walk through all the metabolite records
while ($data = $response->fetch())
{
// start a row
echo '<tr>';
// create the cells
tableCell($data['Metabolite_name']);
tableCell('KEGG: '.$data['Synonyms']);
tableCell($data['Synonyms']);
// finish a row
echo '</tr>';
}
// close the table
echo '</table>';
// close main division
echo '</div>';
// close query
$response->closeCursor();
But this is not worth much, the output should remain the same.
if ($response->num_rows > 0) {
while($data = $response->fetch_assoc()) {
echo "<tr><td>" . $data["Metabolite_name"]. "</td></tr>" . ;
}
}
else {
echo "0 results";
}
CSS is effecting in UI but not in Excel. Anyone can help me on this please.
Below code will export table into excel but not able to provide space before tags.
<?php
echo $excel_data = '<table border="1">
<thead>
<th align="left">S.No.</th>
<th align="left">Name</th>
<th align="left">DOJ</th>
</thead>
<tbody>
<tr>
<td align="left">1</td>
<td align="left">Sreekanth Kuriyala</td>
<td align="left">04-06-2015</td>
</tr>
<tr>
<td align="left">2</td>
<td style="padding-left:20px;">SK</td>
<td align="left">26-07-2015</td>
</tr>
</tbody>
</table>';
$excel_file = 'Reports.xls';
file_put_contents ($excel_file, $excel_data);
?>
</br>
</br>
<a href="<?php echo $excel_file; ?>" download>Export to Excel</a>
you have to create a CSV file just create new php file with a function that expects a result of a db select.
then just echo cols separated by ; and carrier return for next row.
this is a function i made:
<?php
function printcsv($result){
//result of database select
header('Content-Encoding: UTF-8');
header('Content-type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename=example.csv');
echo "\xEF\xBB\xBF";
$nl = "\n";
//this will be the carrier return var
if($rs = $result->fetch_array(MYSQLI_NUM)){
while($rs != ''){
echo $rs[0].";".$rs[1].";".$rs[2].";".$rs[3].";".$rs[4].";".$rs[5].";".$nl;
$rs = $result->fetch_array(MYSQLI_NUM);
}
}
}
?>
change my mysqli_fetch_array by your result desired fetch and try it =)
oh, and you cannot write tables on it, only echo data separated by semicolon. each data for each column, semicolon to put the next data in next column, and carrier return to write in next row.
cheers!
I am a student and I have been given a project to do, we made a database in MySql called Video_Game_Shop. Then we had to connect it to a website using PHP and HTML. And we have to show on one page all the games in our database listed. So we did that by calling a procedure which ordered the games by price. This is where I have the problem. We now need to show images for every one of these games on the page. What I have done so far I have downloaded the pictures, for every game we have listed, and I have no idea on how to do that task. I heard of some ways of inputting the path for the images or to use BLOB file type, but I don't know how to do any of that. I will add the PHP file so you can more easily understand my situation. I would be very grateful if you could help me. :)
<!DOCTYPE html>
<html>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"/>
<style>
h1 {font-family:Serif; font-size:22px; text-align:center}
p {font-family:Serif; font-size:16px}
.container {
width: 100%;
clear: both;
opacity: 1
}
input[type=number] {
border:2px #ffo;
opacity:0.2}
input[type=text] {
border:2px #ff0;
opacity:0.2}
input[type=date] {
border:2px #ff0;
opacity:0.2}
</style>
<h1>videogames</h1>
<head>
<title>videogames</title>
<link rel="stylesheet" href="css/table.css" type="text/css" />
</head>
<body background="http://i.imgur.com/yGkEuwZ.jpg">
<?php
require_once 'user.php';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname",
$username, $password);
// execute the stored procedure
$sql = 'CALL List_game_by_price()';
$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $pe) {
die("Error occurred:" . $pe->getMessage());
}
?>
<div class="container">
<table border="1" align="center" width="90%">
<tr>
<th>GameID</th>
<th>Name/th>
<th>Developer</th>
<th>Publisher</th>
<th>ESRB</th>
<th>Relase Date</th>
<th>Platform</th>
<th>Genre</th>
<th>Language</th>
<th>Price</th>
</tr>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><?php echo $r['GameID'] ?></td>
<td><?php echo $r['Name'] ?> </td>
<td><?php echo $r['Developer'] ?></td>
<td><?php echo $r['Publisher']?></td>
<td><?php echo $r['ESRB'] ?></td>
<td><?php echo date_format ($r['Relase Date']) ?></td>
<td><?php echo $r['Platform'] ?></td>
<td><?php echo $r['Genre'] ?></td>
<td><?php echo $r ['Language'] ?></td>
<td><?php echo $r['Price'] ?></td>
</td>
</tr>
<?php endwhile; ?>
</table>
</div>
</body>
</html>
A quick solution without changing your database at all would be to upload images to the FTP and rename them for their name to match the GameID.
Example: Change your Mass-Effect2_600x400.jpg into 1.jpg.
In your fetch (where you show the data from the base) just add another something like this
<td><?php echo '<img src="path/to/'.$r['GameID'].'.jpg" />' ?></td>
You could create an assets table with cols [ID Primary Key], [Url]. Create a foreign key on your products table AssetID. This way you only have one record per image URL, and multiple products can use that image.
Upload the files to FTP and insert records into the assets table.
If you really wanted to go above and beyond your task, you could create an image upload for the products from an Admin panel. Which when uploaded inserts the path into the assets table and assigns the AssetID to the product.
Upload the images to an ftp, input your image names in a column and then call that in your query. Might be a bit of a workaround but it works well!
<td><img src="path/to/<?= $r['GameID'] ?>.jpg" /></td>
I am trying to get images out of the database using Blob, I know its not secure but it is for demo purposes. Below is the code I have at the moment.
<?php
error_reporting(0);
require './db/connect.php';
include './includes/header.php';
?>
<?php
if($result = $connection->query("SELECT * FROM Production")){
if($count = $result->num_rows){
while($row = $result->fetch_object()){
?>
<table class="productioninfo" style="width: auto; height: auto; border: 5px black solid;">
<tr>
<th>Image:</th>
<td><img src="/phpmyadmin/production/<?php echo $row->ProductionId;?>.jpeg" </td>
<th>Production Name:</th>
<td><?php echo $row->ProductionName; ?></td>
<th>Production Type:</th>
<td><?php echo $row->ProductionType; ?></td></br>
</tr>
<?php
}
$result->free();
}
}
echo $result;
include './includes/footer.php';
Also I want to know how to do it the other way, by having the file path in the database and then displaying it on the web page.
Many Thanks for your help!
An arror in this line:
<td><img src="/phpmyadmin/production/<?php echo $row->ProductionId;?>.jpeg"</td>.
Try to close correctly the img tag :
<td><img src="/phpmyadmin/production/<?php echo $row->ProductionId;?>.jpeg" /></td>
Then be sure that the image was correctly encoded and decoded respectively when storing and retrieving.
While saving an image to database, You have to encode it and then save to db. & while showing it in HTML you have to decode it.
eccoding :
$img_encoded = base64_encode('your image');
decode and show in HTML
<img src="data:image/jpg;base64,'.$img_encoded.'"/>
Overview
I have some data stored in MySql database related to Products. I am trying to retrieve this data and display it on a page using HTML table.
The PHP and MySql has gone well and all the data is retrieved but it is displayed in a very messy manner.
Here is what I have as a layout:
What I am aiming to achieve is to further divide the results table add more columns rows to make the data more readable
Something like this;
The code: PHP, MySQL & HTML:
<?php
session_start();
include('connect_mysql.php');
$product_name = 'product_name';
$product_qua = 'product_qua';
$product_price = 'product_price';
$product_image = 'product_image';
$product_des = 'product_des';
$sql = mysql_query("SELECT * FROM products");
echo "<table id='display'>";
while($rows = mysql_fetch_array($sql))
{
echo"<br>";
echo"<tr><td>";
echo"$rows[$product_name]<br></td>";
echo"<td><img src=$rows[$product_image] height='200px' width='200px'><br></td>";
echo"<td>Avalible: $rows[$product_qua]<br></td>";
echo"<td>Price: $rows[$product_price]<br></td>";
echo"<td>Description: $rows[$product_des]<br></td>";
echo"</tr>";
}
echo "</table>";
?>
CSS responsible for this part:
#display{
float:left;
border: 5px solid black;
margin-left:100px;
}
just add some padding or a border to the table cells:
table#display td{
border: 1px solid black;
padding:0 8px;
}
Edit: What you could do as well:
<table id='display'>
<?php while($rows = mysql_fetch_array($sql)): ?>
<!-- <br> <- why a break? it's not in the correct spot anyway -->
<tr><td>
<?php echo $rows[$product_name]; ?><br>
</td>
<td> - </td>
<td><img src="<?php echo $rows[$product_image]; ?>" height='200px' width='200px'><br></td>
<td> - </td>
<td>Avalible: <?php echo $rows[$product_qua]; ?><br></td>
<td> - </td>
<td>Price: <?php echo $rows[$product_price]; ?><br></td>
<td> - </td>
<td>Description: <?php echo $rows[$product_des]; ?><br></td>
</tr>
<?php endwhile; ?>
</table>
Tip: I prefer to use the while/endwhile; approach rather than using brackets when displaying data to the user.
First of all don't echo so much HTML using PHP, instead do it like this
<?php
session_start();
include('connect_mysql.php');
$product_name = 'product_name';
$product_qua = 'product_qua';
$product_price = 'product_price';
$product_image = 'product_image';
$product_des = 'product_des';
$sql = mysql_query("SELECT * FROM products"); ?>
<table id='display'>
<?php
while($rows = mysql_fetch_array($sql)) {
?>
<tr><td><?php echo $rows[$product_name]; ?></td>
<!-- And so on-->
<?php
}
?>
</table>
Secondly by seeing your inmage, it seems like you need a border for your table so use
table, td {
border: 1px solid #000000;
}
add the following css to apply border on your table cells:
#display td{ border: 2px solid red; }
and optionally add to your #display { :
border-collapse: collapse;