Jump to content

retrieved images from database


tauchai83

Recommended Posts

can some1 guide me on how to link a jpg images to the available item? what should I confifure in MYSQL db? or any special coding needed? my code look like this:

[code]<?php
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
    die('Not connected : ' . mysql_error());
}

$db_selected = mysql_select_db('tcmdb', $link);
if (!$db_selected) {
    die ('Can\'t use the Database: ' . mysql_error());
}

$sql = 'SELECT Item_id,Item_pic,Item_desc, Unit_price FROM Item ORDER BY Item_id';
$result = mysql_query($sql);
$output[] = '<table border="3"><tr><td>';
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))  {
$output[] = '<tr><td>'.$row['Item_pic'].'&nbsp;<td>'.$row['Item_desc'].'&nbsp;<td>Price:'."".'RM'.$row['Unit_price'].'&nbsp;<td><a href="cart.php?action=add&id='.$row['Item_id'].'">Add to cart</a></td></tr>';
}
$output[] = '</td></tr></table>';


echo join('',$output);
?>[/code]


eveything is fine except the images appear in words...how to solve this prob? please help..thanksss
Link to comment
https://forums.phpfreaks.com/topic/32046-retrieved-images-from-database/
Share on other sites

first why are you putting your output into an array. just echo it out or put into a single variable. but it is up to you :)

[code]<?php
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
    die('Not connected : ' . mysql_error());
}

$db_selected = mysql_select_db('tcmdb', $link);
if (!$db_selected) {
    die ('Can\'t use the Database: ' . mysql_error());
}

$sql = 'SELECT Item_id,Item_pic,Item_desc, Unit_price FROM Item ORDER BY Item_id';
$result = mysql_query($sql);
$output = '<table border="3"><tr><td>';
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))  {
  $output .= '<tr><td><img src="path/to/image/'.$row['Item_pic'].'"><td>'.$row['Item_desc'].'&nbsp;<td>Price:'."".'RM'.$row['Unit_price'].'&nbsp;<td><a href="cart.php?action=add&id='.$row['Item_id'].'">Add to cart[/url]</td></tr>';
}
$output .= '</td></tr></table>';


echo $output;
?>[/code]

Ray
the coding only work for one picture...i put the output in array because it is easier to display all in a table. I doing a shopping website which have several items in a table to view. How about I have many pictures to put? is it cannot use array?
answer:

There are total 6 pictures in the table

These pictures should be displayed in the same column. In the webpage view, There are four column in the table. First column to display picture, followed by item description,price and final column is the cloumn reserved for "add to cart". --> I'm talking about the display of the pages to the user and not databases.

I just store the name of the pictures in the table. For instance, BirdNest.jpg...the path to the folder as like this: tcm/images/BirdNest.jpg  tcm is the folder put in WWW folder.

the table structure as following:


Field Type Collation Attributes                    Null Default Extra Action
  Item_id int(11)  No  auto_increment             
  Item_barcode varchar(14) utf8_general_ci  No               
  Item_desc varchar(100) utf8_general_ci    No               
  [color=red]Item_pic varchar(64) utf8_general_ci        No                [/color]
  Supplier_id int(11)                                  No               
  Category_id int(11)                                No               
  Minimum_qty int(2)                                No               
  Qty_on-hand varchar(3) utf8_general_ci  No               
  Supplier_price decimal(15,2)  No               
  Unit_price decimal(15,2)  No               
  Expire_Date varchar(20) utf8_general_ci  No               
  Product_ordered int(3)  No


For Item_pic column, i trying by inserting value BirdNest.jpg which do not work stil..plz help..thanks!
For DB, yes...it's only one picture per row.

For display in webpages, also one picture per row in the <table> tag.

Craygo, all u talking here is about DB rite? ur table could be the <table> tag or database table. haha...confused with ur question somehow coz i'm newbies in PHP.:)

the 6 pictures name is in DB Item_pic column already.
Ray,

should I use the foreach keyword to solve this prob? How to solve it in the efficient way....i going 2 retrieve all pictures but in one row per row. One row one picture. I use array to display all in once but problems with the pictures column...Hope you can modify my code...i had tried so many ways and do not work still...
Ok. Ray...i used ur method by retriving one row of images and just echo it using if statement....i think this is the best way....:)...thanks lot..the problems therefore is solved with the suggestion from Craygo...Thanks..:)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.