I'm trying to create an e-commerce site which sells PC components using PHP and MySQL.
I'm using a database which has a table called 'components' which stores PC components with fields like 'name', 'type' etc. I have a field called 'image' as I was told it is better to store images in the file system rather than as a BLOB. The field contains the name of the image for that component (e.g. for Intel i7 2700k processor the image filed has inteli72700k/jpg). The image will be stored in '../images/products/'.
Now i'm trying to make my product list (i.e. a user clicked on 'Processors' and I am now trying to show the list of products we have, similar to overclockers.co.uk). I'm creating a table where the table heading is the name of the product and there is one row per product.
<table id="products">
<?php
//Connects to the DB, localhost is yourpc, root is the username and there is no password
$con = mysql_connect('localhost','root','');
//This is the database we want to access in phpMyAdmin
mysql_select_db('pctweeks');
$query = "SELECT * FROM component WHERE type = 'CPU'";
$query_run = mysql_query($query) or die(mysql_error($con));
if (mysql_num_rows($query_run) >= 1){
while($row = mysql_fetch_assoc($query_run)) {
echo "<th>{$row['name']}</th>" .
"<tr><td>{$row['image']}</td></tr>";
}
} else
{
echo 'No Products';
}
?>
</table>
The problem is the {$row['image']} part. I know I have to use an image tag but I don't know how to insert the directory of the pictures before the $row['image'].
Cant you use the code like here:
echo "<th>{$row['name']}</th>" .
"<tr><td> <img src='../images/products/{$row['image']}'/> </td></tr>";
You could try this:
define( 'IMG_PATH', 'PATH_OF_YOUR_IMAGE_DIRECTORY_HERE' );
while($row = mysql_fetch_assoc($query_run)) {
echo "<th>{$row['name']}</th>" .
"<tr><td><img src='".IMG_PATH."'{$row['image']}</td></tr>";
}
Replace "PATH_OF_YOUR_IMAGE_DIRECTORY_HERE" with your actual image folder path.
Hope this helps..
Related
I am trying to query the image file names from the database using the Id inputted by the user and want to display the images with the resulting file names form the select statement. However the echo function is showing only the broken image icons instead of the image itself. How can I fix this? Thanks!
//$searchValue is the Id inputted by the user in the html page
$result = mysqli_query($con,"SELECT FileName FROM images WHERE Id='$searchValue'");
//$folder is the path where the images are currently stored
$folder = "C:/xampp/htdocs/images/";
while($row = mysqli_fetch_array($result)){
$file = $row["FileName"];
echo '<img src="'.$folder.$file.'"><br />';
}
I am new to php and js and I am working on a simply on one webpage but i get stuck. What I am trying to do is next:
I am logging on the web
I am using the user id to retrieve data associated with that id from table in db. I am retrieving just the "name" column and using it as a hyperlink.
Here is my code:
$query = "SELECT * FROM cases WHERE id='".$_SESSION['id']."'";
$result=mysql_query($query) or die("Query Failed : ".mysql_error());
while($rows=mysql_fetch_array($result))
{
echo '<tr>
<td><ul><li><a href="casedetails.php">'.$rows['Name'].'</li></ul></td>
</tr>';
}
It is retrieving data that is in the row named "name" in the table, and I want when i click on the hyperlink to save the name of the hyperlink (for ex. Murder(link to cases.php) ). because i want to use it in a query on the page of the hyperlink. I will appreciate if someone can help me!
If you want to click the link and it takes you a different page while remembering what ID (or name) was clicked, you would write your code like this:
$query = "SELECT * FROM cases WHERE id='".$_SESSION['id']."'";
$result=mysql_query($query) or die("Query Failed : ".mysql_error());
while($rows=mysql_fetch_array($result))
{
$name - $rows['Name'];
echo '<tr>
<td><ul><li><a href="casedetails.php?name='.$name.'">'.$name.'</li></ul></td>
</tr>';
}
So, whenever you go to casedetails.php, the variable $name will be in a GET that you can retrieve and use for queries. For example, on casedetails.php you would have $newName = $_GET['name']; and that would retrieve the name.
I have the image URL saved in my mySQL database.($imageid).
How can I display images using their respective id? I want to display different images attached to different blog posts,as we see in regular blogs.
Thank you.
Unsure what stage you are, bit you need to query your 'table' for a single result, perhaps the ID? If the column was Imageid then you could display it like below in an array of results.
$con=mysqli_connect("localhost","user","pass","my_db");
$result = mysqli_query($con,"SELECT * FROM table");
while($row = mysqli_fetch_array($result))
{
echo '<img src="' . $row['Imageid'] . '">';
}
mysqli_close($con);
SOLVED!
so ive created an image upload page which saves the image to a folder and sends the file name to the DB, ive checked to see if they are actually being added to the DB and folder which they are, but when i call the data to another page to display the images i get broken images.
below is my code to call the images, its some code ive scraped together from various tutorials as they gave me the same problem as im having now.
UPDATE:
ive managed to get the images showing but now im faced with being shown the same image for each row of data called, the id and img_name and right for each row but the image is always the same as the first listed.
UPDATED CODE:
<?php
//connect to database
include ('connect.php');
//save the name of image in table
$query = mysql_query("select * from tbl_img") or die(mysql_error());
//retrieve all image from database and store them in a variable
while ($row = mysql_fetch_assoc($query))
{
$img_name = $row['img'];
}
?>
<?php
include ('connect.php');
$img_id = mysql_query("SELECT * FROM tbl_img");
while ($row = mysql_fetch_assoc($img_id))
{
$id = $row['img_id'];
echo "
$id<br>
$img_name<br>
<img src='http://localhost/testarea/include/site_images/$img_name' />
";
echo "<br><br><br></p>
";
}
?>
If you start the path of image with / you mean an absolute path where / is the DocumentRoot folder (or the directory of virtualhost)
With src ="includes/xxx/image.png" you mean that includes is in the same folder with the php script. If it is not you can use relative path like
src="../includes/xxx/image.png" for example
<?php
include ('connect.php');
$img_id = mysql_query("SELECT * FROM tbl_img");
while ($row = mysql_fetch_assoc($img_id))
{
$img_name = $row['img'];
$id = $row['img_id'];
echo "
$id<br>
$img_name<br>
<img src='http://localhost/testarea/include/site_images/$img_name' />
";
echo "<br><br><br></p>
";
}
I am trying to build a simple catalog displaying items from a mysql database.
So far I can get the items to display in a list format including the images stored as a path in the items table in a field called "path".
I would like the ability to be able to click on the item image and it takes you to a dedicated page showing you the details of that product. The link would correspond to which ever item you click on based on the data in the item table.
So far I have the following code;
<?php
session_start();
require "connect.php";
$date = date("d-M-Y");
$query = "select * from item order by date && time asc limit 0,3";
$query2 = "select * from users where userid = ".$_SESSION['userid'];
$result = #mysql_query($query, $connection)
or die ("Unable to perform query<br>$query");
?>
<?php
while($row= mysql_fetch_array($result))
{
?>
<?php echo $row['item'] ?>
<?php echo $row['description'] ?>
//Code for Image Link
<a href='<a href='<?php echo $row['path']?>'><img src="<?php echo $row['path']?>
<?php
}
?>
The above code allows you to click on the image and it takes me to http://127.0.0.1/steal/%3Ca%20href=
However I don't know what I would need to enter in order for it to take me to a page that shows the product?
Please Help
Many Thanks
I don't know your database schema, so I'm taking some liberties here about your columns for the product. But, try this:
<img src="<?php echo $row['path']?"/>
I also am not sure what your product page URL is either, so change product_page.php to whatever template you are using to display your products.
You need to create another page called for example showProductsOrder.php that accepts the orderID and then does output the content.
showProductsOrder.php?orderID=1
<?php
SELECT * FROM order WHERE orderID = $_GET['orderID']
while(fetch()) {
//> Show product here
}
?>
Also please don't use mysql_query. Use php.net/pdo
Addendum
Show all products
As yes123 said you would need to create another page, but i if you have a problem creating the link here is my suggested solution:
Replace your link with:
<?php echo ("<a href='".$row['pagePath']."'><img src='".$row['imagePath']."' /></a>";?>
$row['pagePath'] is the page that it will load when the image is clicked on, and
$row['imagePath'] is where the image is stored on your pc / server.