I'm writing an html code to retrieve the details of the item table in my database.In it, I have some columns and one such column name is Image.It stores images of the Items and it's type is blob.I have written the following code to retrieve and display the data in this table in my php file.
$sql = "SELECT * FROM item where item.Category = 'Tops' limit 3";
$result = $conn->query($sql);
if($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
$id = $row["Item_ID"];
echo " <div class='ite'>
<div class='card'>
<img src=".$row["Image"]." alt=".$row["Item_Name"]." class='imge'>
<h3>".$row["Item_Name"]."</h3>
<p class='price'>Rs. ".$row["Price"]."</p>
<p><button type='submit' onclick='view_data($id)'>View Item</button></p>
</div>
</div>";
}
}
The code works perfectly for the other columns except the image column(<img src=".$row["Image"]." alt=".$row["Item_Name"]." class='imge'>)
The output is as follows.
It gives garbage values like this.How can I fix this error and display the image stored in the database?
<img src='data:image/jpeg;base64," . base64_encode($row['Image']) . "' class='imge'>
This works!Thanks for the support!
Related
I have a page with list of persons.
Now I want to filter them with a drop down select.
Here's my code:
<main>
<div class="wrapper">
<div class="team-list">
<h3><?php echo $teamTitle; ?></h3>
<div class="filter">
<label for="filter"><?php echo $specialtiesTitle; ?></label>
<form action="" method="post">
<div class="form-item">
<select name="specialties">
<?php
$query = "SELECT * FROM specialties";
$result = mysqli_query($connection, $query);
echo '<option selected="selected" value="All">'. $filterTitle . '</option>';
while($row = mysqli_fetch_assoc($result)) {
$id = $row['id'];
$title = $row['title_'.$lang];
echo '<option value="' . $id . '">' . $title . '</option>';
}
?>
</select>
</div>
<div class="form-item">
<input class="form-submit" type="submit" name="submit" value="<?php echo $filterSubmit; ?>">
</div>
</form>
</div>
<div class="content">
<?php
$query = "SELECT * FROM team";
$result = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($result)) {
$id = $row['id'];
$image = $row['image'];
$title = $row['title_'.$lang];
echo '<div class="row">';
echo '<div class="image"><img src="/uploads/' . $image . '"></div>';
echo '<a class="title" href="/team-view?id=' . $id . '">' . $title . '</a>';
echo '<a class="more" href="/team-view?id=' . $id . '">' . $teamMore . '</a>';
echo '</div>';
}
?>
</div>
</div>
</div>
</main>
As you can see from the code the first part has the div filter that receives select from the database.
This line:
echo '<option selected="selected" value="All">'. $filterTitle . '</option>';
Is a additional option with value "All" and the other options are getting from the "specialties" table.
The second part is the content that pulls from the "team" table.
I'm relating the categories from "Specialties" table with the "Team" table.
In the Admin area of my Custom CMS, everything is working and when I change the "Specialties" category, it saves successfully in the database.
Right now the page displays all the fields, but I don't have functionality of filtering.
How can I filter the content?
I know that I have to use:
if(isset($_POST['submit'])) {
//the code inside
}
but what query should I use for such filtering?
EDIT:
By filtering I mean, when I have the list content and from top I have a select drop down and search button. So if I select let's say the first value from the options and click on submit, it should display only the content that has the first category value inside.
In the table "Team" I have many fields and these are the connection:
id
specialties_bg
specialties_en
This is the table "Specialties"
id
title_bg
title_en
Title_bg and title_en has all the options values and id is the connection.
The website is multilingual and that's why I have two fields for different languages.
Check the below code. If the form is submitted add a condition to the query.
$query = "SELECT * FROM team";
// declare the variable
// check if the form is submitted and have a valid search value
if(isset($_POST['submit']) && (trim($_POST['specialties'])!='') && (trim($_POST['specialties'])!='ALL')) {
//add the condition
$query.= " WHERE specialties_en='".trim($_POST['specialties'])."'";
}
You can modify condition based on the languages with OR condition.
Always check the user input for sql injections.
Fetching Specific Data From Database :
In order to fetch all data from a table you can do that simply by using SELECT Query in MySQL as :
SELECT * FROM table;
But as now you want to filter the whole data and just get something specific from your database so for that you must should have something unique in order to differentiate the expected data from the whole rest of the data.
TWO WAYS TO DO THAT :
1) Using An Extra Field (Column) In Your Database Table :
You can do that by adding an extra field (column) to your table structure like something which will contain values based on the records in order to put them in a collective based group.
For Instance :
I have a table with alot of records of players now I add a column namely as sport so now I have something unique in order to filter the players.So I can use following SQL SELECT Query to get all those players as:
SELECT * FROM players WHERE sport="sport_name";
2) Filtering Data Through PHP :
If you don't want to get the filtered data from the Database,So you can do the same process in PHP also as:
For Instance :
You already fetched the whole data from the players table and now you want to filter it so you can filter it like this way as :
<?php
//Assuming that $results contains the whole fetched data from table
for ($i=0;$i<=count($results);$i++) {
if ($results[$i]['sport'] == "cricket") {
// Can target any sport located in your database
//You can pretty much do anything you want..!
echo $result[$i]['player_name'];
}
}
?>
I am making an eccommerce website. my page is filled with images of items for sale and details of the item under the image. I also have a button under the image so that when clicked the user can view more information on this item. But I don't know how to do this. Below is the code I have that displays the information and images from database.
<?php
// Default query for this page:
$q = "SELECT * FROM item";
// Create the table head:
echo '
';
// Display all the items, linked to URLs:
$r = mysqli_query ($dbc, $q);
while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC)) {
$directory = '/uploads';
$file= $row['image_name'];
// Display each record:
echo "
<div class='col-lg-3'>
<image src=$directory/$file title='$row[image_name]' width='213' height='200'></br></br>
<p align='left'> Brand : {$row['brand']}</br> Type: {$row['category']} </br> Price : £{$row['price']} </p>
<a class='btn btn-primary' href='showitem.php' role='button'>More Details »</a>
</br></br></br>
</div>
";
}
echo '</tbody>';
echo '</table>';
mysqli_close($dbc);
?>
What about adding the identifier into the link of your button like this:
<a class='btn btn-primary' href='showitem.php?item={$row['id']}' role='button'>More Details »</a>
And then you can generate detailed product description with showitem.php, using $_GET['item']
First of all you need unique id in table then Pass your product id in url like showitems.php?pid=1 and make a query with where condition pid get from url.
SELECT * FROM item WHERE id = "$_GET['pid']"
I have a question which is very similar to this one: Display Album with Photos (PHP), however I need a bit of help in applying the code to my situation.
I have two tables, which are related by the column AlbumID:
Album: AlbumID, name, date(optional)
Image: ImageID, name, imageURL, AlbumID
My PHP code so far:
function select_galleryimage($sql,$a_id, $img_id) {
include 'connect.php';
$result = $conn->query($sql);
if ($result->num_rows > 0);
while ($row = $result->fetch_assoc()) {
echo "<div id='".$row['AlbumID']."'>\n
<a class='$a_id' href=".$row["imageURL"]." target='_blank'>
<img src=".$row["imageURL"]." alt='".$row['name']."'>
</a>\n
</div>\n";
}
}
where $sql = SELECT * FROM album JOIN image ON album.AlbumID=image.AlbumID'
$a_id = 'lightbox' and $img_id = ''
The issue that I am having is how to create a loop which creates the divs into which the loop with the images is inserted, so as the end result is something like this:
<div id="album1" style="display:none">
<h1>Gallery</h1>
<a class="lightboxX35" href="media/Photos/_MG_7732.jpg"><img src="media/Photos/_MG_7732.jpg"></a>
<a class="lightboxX35" href="media/Photos/_MG_7508.jpg"><img src="media/Photos/_MG_7508.jpg"></a>
</div>
Gallery
Thank you all in advance for any help provided.
Somehing like this?
if ($result->num_rows > 0) {
$current_album = null;
while ($row = $result->fetch_assoc()){
if ($current_album <> $row['AlbumID']) {
if (!empty($current_album))
echo "</div>\n";
echo "<div id='".$row['AlbumID']."'>\n";
echo "<h1>Gallery</h1>\n";
$current_album := $row['AlbumID'];
}
echo "<a class='$a_id' href='".$row["imageURL"]."' target='_blank'><img src=".$row["imageURL"]." alt='".$row['name']."'></a>\n";
}
echo "</div>\n";
}
I haven't tried it on a server so may not be the correct synthax...
With more than a single album you have to sort the SQL by album using ORDER BY then echo the DIV fo every new album. To achieve this assign a variable $current_album = null; outside the while loop then check for changes inside.
This is my first post on stackoverflow after following posts for a very long time.
My problem is, I have a function that is supposed to retrieve data from a table (models) and display a name, location and an image to one of the pages. Everything is working but the images don't get displayed. Please look at my code and tell me what I should change to get these images to display. I'm using twitter bootstrap for styling.
My directory is: localhost/mainsite/admin/uploads
I've checked everything related to this post and tried it out but they didn't solve my problem. So please help.
function view_models() {
global $dbc;
$q = "SELECT * FROM models";
$r = mysqli_query($dbc, $q);
while ($row = # mysqli_fetch_array($r)) {
$id = $row['model_id'];
$mn = $row['model_name'];
$ln = $row['location'];
$img = $row['image1'];
echo "
<div class='col-xs-6 col-lg-3'>
<h3>$mn</h3>
<a href='details.php?id=$id'>
<img src='admin/uploads/$img' class='img-thumbnail' />
<p>$ln</p>
</div>";
}
}
Try this.,
I have changed the echo statement because you did a error, that was misplaced '&"
echo "<div class='col-xs-6 col-lg-3'>
<h3>".$mn."</h3>
<a href='details.php?id=".$id."'>
<img src='admin/uploads/".$img."' class='img-thumbnail' />
<p>".$ln."</p>
</a>
</div>";
I have this PHP code :
$query = "SELECT * FROM news WHERE news_active = 1 AND news_type = 1 ORDER BY id DESC";
$q2 = "SELECT * FROM st_photos WHERE id = 4 LIMIT 1";
$r2 = mysql_query($q2);
$row22 = mysql_fetch_array($r2);
$news_set = mysql_query($query);
$news_set2 = mysql_query($query);
if (mysql_num_rows($news_set) != 0) {
$r = mysql_fetch_array($news_set);
echo "<div id=\"d_coll\">
<div id=\"top_text\">$row22[img]</div>
<div id=\"d_image\"><img id=\"larg_p2\" src=\"photos/$r[news_image]\" width=\"320\" height=\"250\" border=\"0\"></div>
<div style=\"width:300px\"><div id=\"n_text2\">$r[news_part_en]</div>
</div>
</div>";
}
if (mysql_num_rows($news_set2) != 0) {
while ($news = mysql_fetch_array($news_set2)) {
echo "<div id=\"n_col\">
<div id=\"n_tittle\">$news[news_tittle_en] <img src=\"images/bu3.png\" border=\"0\" align=\"middle\"></div>
<div id=\"im\"><img onMouseOver=\"MM_swapImage('larg_p2','','photos/$news[news_image]','imgs[$news[id]]','','photos/$news[news_image]',1);up2('$news[news_part_en]')\" onMouseOut=\"MM_swapImgRestore()\" name=\"imgs[$news[id]]\" id=\"imgs[$news[id]]\" src=\"photos/$news[news_image]\" width=\"50\" height=\"50\"></div>
<div dir=\"ltr\" id=\"n_div\">$news[news_part_en] <div class=\"mo\">MORE</div></div>
</div>";
}
echo "<div align=\"right\" class=\"arr\"><img src=\"images/prev.png\"> <img src=\"images/next.png\"></div>";
}
There are 2 images at the end of the code (prev & next), I want to use them to do pagination but I don't want to view any numbers, only these 2 images.
How I can do that?
I think we can do that by using JQuery library, but I don't know how to use it.
Thanks in advance.
you can use one of many plugins, for example here .
you must just remoove thе numbers. you can, i believe;)
or you can write the script by yourself.
i'll give you only an idea. let's assume you have 30 rows( from DB).put them into <div> tags, and increase the id of div. the display proparty of first <div> you must set to '', and all others to none. and then, onclick on your buttons, you just change display proparty of div elements...