How do I reference an image path to my PHP File? - php

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>

Related

display type BLOB as a link

I want to display records in the website and the $row['data'] of the table report its type (BLOB) in a database and I want to display it as a link but it doesn't appear anymore.
What Can I do ? Whats the problem here?
<?php
include 'connect.php';
extract($_SESSION);
session_start();
?>
<html>
<body >
<table>
<?php
$sql="SELECT data,report.StudID,studFName,StudLName
FROM report,student
WHERE report.SuperID={$_SESSION['supervisor']} AND
report.StudID=student.StudID ";
$result= mysqli_query($con,$sql) or die ("could not found;
".mysqli_error($con));
while ($row=mysqli_fetch_array($result) )
{
?>
<tr>
<td><?php echo '<a href="data:application/pdf;base64,'.base64_encode($row ['data']).' " height="20" width="20" />'?></td>
<td><?php echo $row['StudLName'] ?></td>
<td><?php echo $row['studFName'] ?></td>
<td><?php echo $row['StudID'] ?></td>
<?php
}
?>
</tr>
</table>
Your link won't appear because you need to enclose it with </a> and specify text in it.
<?php
echo '<a href="data:application/pdf;base64,'.base64_encode($row ['data']).' " />Pdf Link</a>'
?>
I don't think you need to specify height and width for link.

Codeigniter Display Image With Open Library API

I am retrieving data from my MySQL database, and displaying a table including;
item_id
item_image (empty)
item_title
item_isbn (contains hyphens)
The code I have so far works, in the sense that I am able to retrieve the data and display it in a simple table.
My code so far;
model
class Items_model extends CI_Model {
public function itemList() {
$query = $this->db->query("SELECT * FROM item LIMIT 10");
return $query->result_array();
}
}
view
<table>
<tr>
<td><strong>ID</strong></td>
<td><strong>Image</strong></td>
<td><strong>Title</strong></td>
<td><strong>ISBN</strong></td>
</tr>
<?php foreach ($items as $item): ?>
<tr>
<td><?php echo $item['item_id']; ?></td>
<td><?php echo $item['item_image']; ?></td>
<td><?php echo $item['item_title']; ?></td>
<td><?php echo $item['item_isbn']; ?></td>
</tr>
<?php endforeach; ?>
</table>
controller
class Items extends CI_Controller {
public function index() {
$data['items'] = $this->items_model->itemList();
$this->load->view('item_view', $data);
}
}
I want to use the book's ISBN, retrieve the book cover using Open Library Covers API and populate the image field in the html table.
Apparently this is possible by using the following URL pattern;
http://covers.openlibrary.org/b/isbn/0385472579-S.jpg
I have tried the following code in my view but it doesn't work;
<td><img src="<?php echo ('http://covers.openlibrary.org/b/isbn/'. stripslashes($item['item_isbn']) .'-S.jpg');?>"/></td>
Ideally I would like to display a standard 'No Image Found' jpg, if I'm unable to retrieve an image using Open Library API also, but I'm sure I can work this part out on my own (hopefully!).
Any advice would be appreciated, very new to Codeigniter.
Assuming you have valid ISBN's stored in your database, please change your view to the following:
<table>
<tr>
<td><strong>ID</strong></td>
<td><strong>Image</strong></td>
<td><strong>Title</strong></td>
<td><strong>ISBN</strong></td>
</tr>
<?php foreach ($items as $item): ?>
<tr>
<td><?php echo $item['item_id']; ?></td>
<td><img src="http://covers.openlibrary.org/b/isbn/<?php echo $item['item_isbn']; ?>-S.jpg" /></td>
<td><?php echo $item['item_title']; ?></td>
<td><?php echo $item['item_isbn']; ?></td>
</tr>
<?php endforeach; ?>
</table>
If an image is not found, a 1px by 1px transparent pixel is displayed.
So the code syntax to display the image is:
<img src="http://covers.openlibrary.org/b/isbn/<?php echo $item['item_isbn']; ?>-S.jpg" />
Resulting screenshot from my test
Update (If No Image Found)
As per the documentation:
By default it returns a blank image if the cover cannot be found. If
you append ?default=false to the end of the URL, then it returns a 404
instead.
To add your own fallback image, you could append this parameter to the image URL, and then check if the file doesn't exist.
There are multiple ways to check if a file exists or not. Using curl would probably be the most graceful but here is a...
Quick & Dirty Method
This uses a ternary operator in conjunction with the getimagesize function to handle the fallback. You will need to ensure that GD (image processing library) support is enabled in your version of PHP for this.
<img src="<?php echo (!getimagesize('http://covers.openlibrary.org/b/isbn/' . $item['item_isbn'] . '-S.jpg?default=false')) ? '/path/to/your-image.jpg' : 'http://covers.openlibrary.org/b/isbn/' . $item['item_isbn'] . '-S.jpg'; ?>" />
Think of the ternary operator syntax like an inline if/else statement.

Format cell file .xls made by php

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>

What am I missing or doing wrong, trying to get image out of database using BLOB

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.'"/>

Retrieving images from the path data saved at SQL database

Part of my code to retrieve the stores images and the content from the SQL database (only the path is saved at the database) is as follows. I get the content displayed except the images.
My database record says the path as; C:/xampp/htdocs/bro/productLoader/uploaded_files/1377935517-IMG_0150.JPG
The image source I entered does not seems to be helping me. How do I adjust my approach to make sure the pictures are extracted from the database?
code as follows;
<?php
$j=0;
while ($rows = mysql_fetch_assoc($query))
{
?>
<tr style="height:100px; font-family: Georgia, 'Times New Roman', Times, serif;">
<td style=" width:100px;">
<img src='<?php $rows['pictures'];?>'><br><br>
</td>
<td style="text-align:justify;"><?php echo $rows['description'];?><br><br></td>
<td style="text-align:right;"><?php echo $rows['brand'];?><br><br></td>
<td style="text-align:right;"><?php echo $rows['model'];?><br><br></td>
<td style="text-align:right;"><?php echo $rows['unitprice'];?><br><br></td>
<td style="text-align:right;"><?php echo $rows['availability'];?> units available<br><br></td>
<td><input type='submit' id='buynow[]' class='buynow' name='but' value='Buy'><br><br></td>
</tr>
<?php
$j++;
}
echo "</table>";
}?>
You have to insert only relative path excluding your document root. That is if your document root is set till htdocs folder only (which is default in apache for localhost) then you have to insert image path from this document root in your case
/bro/productLoader/uploaded_files/1377935517-IMG_0150.JPG
And yes you need to echo that variable too.
Try html link first
<img src='bro/productLoader/uploaded_files/1377935517-IMG_0150.JPG' />
Ok
Try this fixed:<img src='<?php echo substr_replace($rows['pictures'], '',0, 16);?>'/>

Categories