Diplay All Records, But Distinct? - php

I'm trying to display all the records in tbInventory, but there are multiple duplicates on the records, specifically under the colItem column. Now what I'm trying to show are all the records in tbInventory but I want it in a way that it will only show an item once if it has a duplicate.
Now my current code displays this:
As you can see, its printing all the items with the colItem values of 'Access Point'. I only want the code to display the item 'Access Point' only once even if it has duplicates.
Now I can't simply use the DISTINCT function as I'm also using the code block for the printing of the data. Here's my current code:
$con=mysqli_connect("localhost","root","","dbDssInventory");
if (mysqli_connect_errno($con)){
echo "Failed to connect to MySQL " . mysqli_connect_error();
}
$result=mysqli_query($con,"SELECT * from tbInventory ORDER BY colItem");
if($result != NULL){
while($row=mysqli_fetch_array($result))
{
echo "<table border=0 style=\"table-layout:fixed;\">";
echo "<tr>";
echo "<td align=left width=15 style=\"word-wrap:break-word;\">";
echo "<div class=\"hover_img\">";
echo " <a href=\"#\">";
echo " <img src='MAGNIFYINGGLASS.png' alt='Point' width='70%'><em>
<object data=" .$row['colImage']. ".PNG width='850%' type=\"image/png\">
<img src=\"default.png\" width='850%'>
</object>";
echo "</td>";
echo "<td align=left width=160 style=\"word-wrap:break-word;\" style=\"word-wrap:break-word;\">";
echo $row['colItem'];
echo "</td>";
$itemname= $row['colItem'];
Is there a way to execute this?

Instead of
$result=mysqli_query($con,"SELECT * from tbInventory ORDER BY colItem");
Try this:
$result=mysqli_query($con,"SELECT * from tbInventory GROUP BY colItem ORDER BY colItem");
You will get the item 'Access Point' only once even if it has duplicates. Hope it helps!

Related

Populate a drop down box from sql query in a table and strip symbols

At present I have this code
$id = (isset($_GET['id']) ? $_GET['id'] : '');
$result = mysqli_query($con,"SELECT * FROM garage WHERE PlayerUID = '$id'
ORDER BY DateStored");
$row_cnt = $result->num_rows;
printf("Player has %d Vehicles stored.\n <br>", $row_cnt);
echo "<br>";
echo "<table border='1'>
<tr>
<th>Player Name</th>
<th>Vehicle</th>
<th>Date Stored</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><a href='https://www.battlemetrics.com/rcon/players?filter%5Bsearch%5D=%22".$row['PlayerUID']."%22&filter%5Bservers%5D=2191805%2C2315320%2C2384537&sort=score&showServers=true' target='_blank'>" . $row['Name'] . "
</td>";
echo "<td><a href='https://www.google.co.uk/search?q=Dayz+Epoch+".$row['DisplayName']."' target='_blank'>" . $row['DisplayName'] . "
</td>";
echo "<td>" . $row['DateStored'] . "</td>";
echo "<td><a href='deletegarage.php?del=$row[id]' Onclick='return ConfirmDelete()'>Delete</a>";
echo "</tr>";
}
echo "</table>";
Which gives me this output :
Current output
What I would like to achieve is adding another table after Vehicle field which displays the information from the database in the "Inventory" field into a dropdown list, but the current information that is in a single row looks like this:
[[[],[]],[["CinderBlocks","cinder_door_kit","cinder_garage_kit","full_cinder_wall_kit","ItemComboLock","metal_floor_kit","ItemVault","PartVRotor","ItemPole","PartGeneric","MortarBucket","ItemSledgeHandle","ItemSledgeHead","ItemTankTrap","plot_pole_kit"],[61,3,5,25,4,11,4,1,22,1,15,2,2,21,1]],[[],[]]]
so I would like to try and display each item in that list without symbols and on different lines within that dropbox if it's at all possible.
I would like it to look something like this :
What i need the dropdown to look like
I hope this provides you with enough information as to what I need and would appreciate any support!

PHP image display from mysql from different tables [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am creating a used car website for a class. I have a mysql database that has two tables. One for the car details and one table for car photos. I am trying to display 1 single photo of the car next to some details (like you see on cars.com) but I am struggling. The car id is one one table and the "ext_1" in on another table.
<?php
$page_title = "List cars";
require_once ('includes/header.php');
require_once('includes/database.php');
//select statement
$sql = "SELECT * FROM inventory_tbl, inventory_photos_tbl";
//execute the query
$query = $conn->query($sql);
//Handle selection errors
if (!$query) {
$errno = $conn->errno;
$errmsg = $conn->error;
echo "Selection failed with: ($errno) $errmsg<br/>\n";
$conn->close();
//require_once ('includes/footer.php');
exit;
}
//display results in a table
?>
<h2>Inventory</h2>
<table border="1px solid" border="collapse" align ="center" height='300' width='750' >
<tr><center>
<th></th>
<th>Year</th>
<th>Make</th>
<th>Model</th>
<th>Mileage</th>
<th>Price</td>
<th>Details</th>
</center>
</tr>
<?php
//insert a row into the table for each row of data
while (($row = $query->fetch_assoc()) !==NULL){
echo "<tr>";
echo "<td><img src =" .$row['id']. $row['ext_1']. " width = 100 height = 100></td>";
echo "<td>", $row['year'], "</td>";
echo "<td>", $row['make'], "</td>";
echo "<td>", $row['model'], "</td>";
echo "<td>", number_format($row['mileage']), "</td>";
echo "<td>","$", number_format($row['price']), "</td>";
echo "<td><a href='cardetails.php?id=", $row['id'],"'>View Details</td>";
echo "</tr>";
}
?>
<?php
// clean up resultsets when we're done with them!
$query->close();
// close the connection.
$conn->close();
//include the footer
require_once ('includes/footer.php');
I'm moving my answer out of the comments, so future readers are able to benefit from the solution.
First, the query needs to qualify the join condition, so change the query to:
$sql = "SELECT * FROM inventory_tbl, inventory_photos_tbl WHERE inventory_tbl.id = inventory_photos_tbl.ext_1"
Second, the img tag should only refer to the photo source/column, so it should be something like:
echo "<td><img src =" .$row['myPhotoColumn']. " width = 100 height = 100></td>";
What you're wanting to do is a left join. (Join data from the right table into the left table if the data in the right table exists).
Your SQL statement would look like this.
$sql = "SELECT * FROM inventory_tbl
LEFT JOIN inventory_photos_tbl as images
ON images.ext_1 = car_id";
Images serves as an alias to your photos table.
You match up images.ext_1 column to the main table's car_id column which is what the ON statement signifies. Think of it as a where clause for the JOIN
Now you can reference any of the data in the inventory_photos_table you want in your PHP using your $row variable such as $row['image_column_name'].
You'll probably want to wrap the image data in an if statement to show a placeholder image in case there isn't an image associated with the vehicle yet.
As i don't know your table structure. I assumed the relational column name as 'id' in inventory_photos_tbl table.
<?php
$sql = "SELECT * FROM inventory_tbl";
$query = $conn->query($sql);
while (($row = $query->fetch_assoc()) !==NULL){
$carID=$row['id'];
// In this query, i'm assuming the relational column as 'id'. (If you are having different column name, please change it here itself)
$imgQuery = $conn->query("SELECT * FROM inventory_photos_tbl WHERE id=$carID LIMIT 0,1");
while(($rowImg = $imgQuery->fetch_assoc()))
{
$carIMG=$rowImg['ext_1'];
}
echo "<tr>";
echo "<td><img src ="$carID.$carIMG" width = 100 height = 100></td>";
echo "<td>", $row['year'], "</td>";
echo "<td>", $row['make'], "</td>";
echo "<td>", $row['model'], "</td>";
echo "<td>", number_format($row['mileage']), "</td>";
echo "<td>","$", number_format($row['price']), "</td>";
echo "<td><a href='cardetails.php?id=", $row['id'],"'>View Details</td>";
echo "</tr>";
}?>

How can I use hyperlinks in PHP to display data on the page it sends you to?

I have this piece of code below. It displays the image and name of all the entries in a table in my database. The name is set up to become a hyperlink.Is it possible to make it so when one specific name is clicked that data for only that specific name will be displayed on the page you are sent to?
So for example if I select the first entry that is displayed back "mealname1" and it takes me to the showrecipe.php page, can I make it so I can display all the data I have for "mealname1" and only "mealname1". I'm really lost, I have scoured the internet and my php books but can't find anything to that is relevant.
If there is no way of doing it is there an obvious solution that I am missing?... I am very much a novice to this... thanks for your help guys.
<?php
require("db.php");
$prodcatsql = "SELECT * FROM recipes";
$prodcatres = mysql_query($prodcatsql);
$numrows = mysql_num_rows($prodcatres);
if($numrows == 0)
{
echo "<h1>No Products</h1>";
echo "There are no recipes available right now.";
}
else
{
echo "<table id='recipetable'>";
while($prodrow = mysql_fetch_assoc($prodcatres))
{
echo "<tr>";
if(empty($prodrow['image'])){
echo "<td><img
src='./images/No_image.png' alt='"
. $prodrow['mealname'] . "'></td>";
}
else {
echo "<td><img src='./images/".$prodrow['image']
. "' alt='"
. $prodrow['mealname'] . "'></td>";
}
echo "<td>";
echo ''.$prodrow['mealname'].'';
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
Change the query to
SELECT * FROM recipes WHERE mealname='$mealname' LIMIT 1;
You can remove "LIMIT 1" if you want but this makes sure you will only get 1 or 0 row back. Don't forget to escape the string.

cannot delete row from database using php

I am unable to delete any record from database. I cannot find any error in this.
I have deleted records from another table by just changing a little bit but here it's not working. Below is the code to apply the delete query.The name of table is from where I want to delete records but its not happening here.
deleteSupplier.php
<?php
mysql_connect("localhost","root","");
mysql_select_db("db_kiln");
$id1 = $_GET['id1'];
$query0 = "DELETE FROM tbl_supplier WHERE sup_id='$id1'";
if(mysql_query($query0)){
echo "<script>window.open('supplier_connect.php','_self')</script>";
}
else{
echo "Not deleted";
}
?>
This is the file where I fetch data from database and have delete button against each record. When I click on the button it does not delete record and show the error message. I can't find any error I think there is a logical error in this code. Please help.
supplier_connect.php
<?php
mysql_connect("localhost","root","");
mysql_select_db("db_kiln");
$query = "Select * from tbl_supplier";
$run = mysql_query($query);
echo "<table border='1'>
<tr>
<th>Supplier Id</th>
<th>Name</th>
<th>Contact Number</th>
<th>Quotation </th>
<th>Remove</th>
</tr>";
while($row = mysql_fetch_assoc($run)){
echo "<tr>";
echo "<td>" . $row['sup_id'] . "</td>";
echo "<td>" . $row['sup_name'] . "</td>";
echo "<td>" . $row['sup_contact'] . "</td>";
echo "<td>" .$row['sup_quotation']. "</th>";
echo '<td>Delete</td>';
echo "</tr>";
}
echo "</table>";
?>
You probably have the ID of the supplier referencing to another table, and upon DELETE, no action is set. You need to set the option 'upon DELETE: Cascade or SET NULL.' I'd go for SET NULL, else your whole reference record will be deleted as well.
EDIT: check the relationship between your contacts and quotations, see where your supplierid is going to.

Can you store selected sql query values in the url and then retrive them with $_GET?

I'm trying to make it so that when a users clicks one of the rows,it will take them to a new page whose link is given as the value of the row they selected and then retrieve the value with $_GET["timesub"].
Anyone know how to do this?
mysql_select_db("RRRC", $con);
$result = mysql_query("SELECT * FROM mainreq WHERE roomnum=$loc");
echo "<table border='1'>
<tr>
<th> Submitted </th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td> . $row['timesub'] . </td>";
echo "</tr>";
}
echo "</table>";
Assuming that $row['timesub'] identifies a row in your data set (I doubt it), just fix your echo instruction as:
echo "<td>" . $row['timesub'] . "</td>";
Escaping the html quotes properly.
echo "<td><a href='roomdata.php?timestamp=".$row['timesub']."'>".$row['timesub']."</a></td>";
Close the outer " before the . concatenator, replace the inner " with '
A good practice is to use the row's primary key to reference your get query; but yes - this can be done.
All you have to do is store the get data into a sanitized variable, and perform the required SQL lookup / data display.
EX:
$roomnum=mysql_real_escape_string(preg_replace("/[^a-zA-Z0-9]+/", "", $_GET['roomnum']));
Now, given that "roomnum" is your primary key just look it up and display:
$result = mysql_query("SELECT * FROM mainreq WHERE roomnum='$roomnum'");
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td> . $row['timesub'] . </td>";
echo "</tr>";
}
echo "</table>";

Categories