How to add "book button to each car product"? - php

I want to add "book" button to each car product.But it only display only one button for first car only.
$sql = "SELECT * FROM car";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>Id</th>";
echo "<th>Name</th>";
echo "<th>Price(RM)</th>";
echo "<th>Colour</th>";
echo "<th>Mode</th>";
echo "<th>Image</th>";
echo "<th>Status</th>";
echo "<td><button onclick=\"book_car('" . $row['car_id'] .
"')\">Book</button></td>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['car_id'] . "</td>";
echo "<td>" . $row['car_name'] . "</td>";
echo "<td>" . $row['car_price'] . "</td>";
echo "<td>" . $row['car_colour'] . "</td>";
echo "<td>" . $row['car_mode'] . "</td>";
echo "<td><img src='" . $row['car_image'] . "' height='100'
width='100'></td>";
echo "<td>" . $row['car_status'] . "</td>";
echo "</tr>";
}
There is no error.But i just want "book" button display for each car products.

This is simply because your button is out of the while loop !
Also you did not close first tr tag .
Correct code :
$sql = "SELECT * FROM car";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>Id</th>";
echo "<th>Name</th>";
echo "<th>Price(RM)</th>";
echo "<th>Colour</th>";
echo "<th>Mode</th>";
echo "<th>Image</th>";
echo "<th>Status</th>";
echo "<th>action</th>";<!-- Added this line -->
echo "</tr>";<!-- Added this line -->
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['car_id'] . "</td>";
echo "<td>" . $row['car_name'] . "</td>";
echo "<td>" . $row['car_price'] . "</td>";
echo "<td>" . $row['car_colour'] . "</td>";
echo "<td>" . $row['car_mode'] . "</td>";
echo "<td><img src='" . $row['car_image'] . "' height='100'
width='100'></td>";
echo "<td>" . $row['car_status'] . "</td>";
echo "<td><button onclick=\"book_car('" . $row['car_id'] .
"')\">Book</button></td>";<!-- Replaced This line -->
echo "</tr>";
}
echo "</table>";
I hope this helps you :)

Related

Displaying the color equivalent of the hex code retrieved in a table cell

I need to retrieve the color hex code stored in a database and display it as seen on screen within a table cell. i'm trying to find a way to convert the hex code the db query returns for that specific table cell into the actual color that the hex denotes. Here's my code...
<?php
// Include config file
require_once "config.php";
$pdo = new PDO($dsn, $username, $password, $options);
// Attempt select query execution
$sql = "SELECT * FROM sales";
if($result = $pdo->query($sql)){
if($result->rowCount() > 0){
echo "<table class='table table-bordered table-striped'>";
echo "<thead>";
echo "<tr>";
echo "<th>S/N</th>";
echo "<th>Transaction Date</th>";
echo "<th>Customer Name</th>";
echo "<th>Address</th>";
echo "<th>Phone Number</th>";
echo "<th>Vehicle Model</th>";
echo "<th>Vehicle Chassis Number</th>";
echo "<th>Vehicle Registration Number</th>";
echo "<th>Vehicle Color</th>";
echo "<th>Amount Paid</th>";
echo "<th>Advance</th>";
echo "<th>Balance</th>";
echo "<th>Balance Due Date</th>";
echo "<th>Paid To</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = $result->fetch()){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['transactiondate'] . "</td>";
echo "<td>" . $row['customername'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['phonenumber'] . "</td>";
echo "<td>" . $row['vehiclemodel'] . "</td>";
echo "<td>" . $row['vehiclechassisnumber'] . "</td>";
echo "<td>" . $row['vehicleregistrationnumber'] . "</td>";
echo "<td>" . $row['vehiclecolor'] . "</td>";
echo "<td>" . $row['amountpaid'] . "</td>";
echo "<td>" . $row['advance'] . "</td>";
echo "<td>" . $row['balance'] . "</td>";
echo "<td>" . $row['balancedate'] . "</td>";
echo "<td>" . $row['paidto'] . "</td>";
echo "<td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
// Free result set
unset($result);
} else{
echo "<p class='lead'><em>No records were found.</em></p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
// Close connection
unset($pdo);
?>
It is a bit unclear about where you wish to use the colour but I'm assuming it is here. And also my assumption is the retrieved value for vehiclecolor column is a HEX value
echo "<td>" . $row['vehiclecolor'] . "</td>";
You can use tag property 'background-color' for this.
$temp = $row['vehiclecolor'];
echo "<td style='background-color:" . $temp ."'></td>";
Pretty self-explanatory I guess. String concatenation.

Echo Mysql into a table - presentation and DOM

Two questions in one:
First question, mainly about presentation:
I'm echo-ing the following code which should create a table. The table should have a single column, but it's being rendered with the elements above the image as a single line. can anyone see why?
<?php
$sql = "SELECT * FROM catdata WHERE featured='yes' LIMIT 2";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['manufacturer'] . "</td>";
echo "</tr";
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "</tr";
echo "<tr>";
echo "<td><img src=\"6.diesel.png\"></td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['size'] . "l</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['mileage'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['fitsmodel'] . "</td>";
echo "</tr>";
echo "<tr class=\"tablePriceBlock\">";
echo "<td>£" . $row['pricefitted'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>£" . $row['pricedel'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else {
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Unable to execute $sql. " . mysqli_error($link);
}
?>
Question 2:
Can I show the second result in a second column, or as a separate table? Also, is it possible to access the $result elements like say, $[manufacturer][1]?
Always look at the emitted source your code generates by either "View Source" or using a tool like curl. You'll find this mistake:
echo "</tr";
You're missing a >.
Many people who write HTML have browser plugins that can link through to an HTML validator to ensure they've got the correct syntax. You may want to find and install one of these.
To generate the second result is a separate table follow the following code.
echo "<table>";
$count = 1;
while($row = mysqli_fetch_array($result)){
if($count == 2){
echo "<table>";
echo "<tr>";
echo "<td>put your code here</td>";
echo "</tr>"
echo </table>
}else{
echo "<tr>";
echo "<td>" . $row['manufacturer'] . "</td>";
echo "</tr";
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "</tr";
echo "<tr>";
echo "<td><img src=\"6.diesel.png\"></td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['size'] . "l</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['mileage'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['fitsmodel'] . "</td>";
echo "</tr>";
echo "<tr class=\"tablePriceBlock\">";
echo "<td>£" . $row['pricefitted'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>£" . $row['pricedel'] . "</td>";
echo "</tr>";
}
$count ++;
}
echo "</table>";
As you are limiting your query to only two record so this method is not bad for that and hope this will help.

how to use 2 fetch_assoc in one while loop?

i need list something like this.
First table is display By using the this code.
<?php
if (isset($_REQUEST['asign'])) {
include 'includes/connection.php';
$sql12= $cid->query("SELECT name FROM user WHERE designation='Technical' AND status='1'");
$query = "SELECT * FROM allinone WHERE flag=3 ORDER BY id ASC";
$rs = $cid -> query($query);
$n = $rs -> num_rows;
echo "<br /><span style='color:red;'><center>$n records found</center></span>";
echo "<div id='record'>";
echo "<table border='1' width='80%' align='center' cellpadding='0' cellspacing='0' id='unsolTable'>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Ticket Number</th>";
echo "<th>KOID</th>";
echo "<th>PROBLEM</th>";
echo "<th>COMMENT</th>";
echo "<th>DATE</th>";
echo "<th>TIME</th>";
echo "<th>STATUS</th>";
echo "<th>SOLVED BY</th>";
echo "<th>Assign</th>";
echo "<th>Assigned to</th>";
echo "</tr>";
while ($row = $rs -> fetch_assoc() ) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['token'] . "</td>";
echo "<td>" . $row['koid'] . "</td>";
echo "<td>" . $row['problem'] . "</td>";
echo "<td>" . $row['comment'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['time'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['solvedBy'] . "</td>";
echo "<td><a href='assign.php?qType=technical&name=anshul&id=" . $row['id'] . "'>anshul</a>
<br />
<a href='assign.php?qType=technical&name=kiran&id=" . $row['id'] . "'>kiran</a>
<br />
<a href='assign.php?qType=technical&name=akhilesh&id=" . $row['id'] . "'>akhilesh</a></td>";
echo "<td>" . $row['assTo'] . "-" . $row['assTime'] . "-" . $row['assDate'] . "</td>";
echo "</tr>";
}
echo"</table>";
echo "</div>";
}
?>
On the another side is list on the assign i don't want to write it manually just get from the database how can i do this??
$sql= $cid->query("SELECT name FROM user WHERE designation='Technical' AND status='1'");
while ($row=$sql->fetch_assoc()) {
$name= $row['name'];
}
this is the another code i want to add in first code.
Nothing prevents you from executing another SQL query inside you while loop; just replace the echo...anshul...echo...kiran... part with another while loop containing the code you already provided.
You just have to make sure that you use different variables, otherwise your $row variable is overwritten by the second query. Hence, you can do something like this:
// ...
echo "<td>" . $row['solvedBy'] . "</td>";
$resultSetAssignedUsers = $cid->query("SELECT name FROM user WHERE designation = 'Technical' AND status = '1'");
while ($rowAssignedUsers = $resultSetAssignedUsers->fetch_assoc()) {
echo "<td><a href='assign.php?qType=technical&name=" . $rowAssignedUsers['name'] . "&id=" . $row['id'] . "'>" . $rowAssignedUsers['name'] . "</a>";
}
echo "<td>" . $row['assTo'] . "-" . $row['assTime'] . "-" . $row['assDate'] . "</td>";
// ...

Displaying 0/1 as no/yes from MYSQL with PHP

I have a mysql table with a row that is either going to be a 0 or a 1. However, I want to be able to display the table with PHP and have 0 show up as no, and 1 show up as yes. I am still a beginner with PHP and have been searching for a way to do it, but have had no luck. The row in question is the 'masterwork' row. The line of code I thought would do it is
$row['masterwork'] = ( intval( $row['masterwork']) == 1) ? "YES" : "NO";
Here is the code that displays the table:
<?php
$con=mysqli_connect("localhost","username","password","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM weapons");
$row['masterwork'] = ( intval( $row['masterwork']) == 1) ? "YES" : "NO";
while($row = mysqli_fetch_array($result))
{
echo "<center>";
echo "<table border='1' class='display'>";
echo "<tr>";
echo "<td>Weapon Name: </td>";
echo "<td>" . $row['weaponName'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Creator: </td>";
echo "<td>" . $row['creator'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Weapon Category: </td>";
echo "<td>" . $row['weaponCategory'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Weapon Sub-Category: </td>";
echo "<td>" . $row['weaponSubCategory'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Cost: </td>";
echo "<td>" . $row['costAmount'] . " " . $row['costType'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Damage(S): </td>";
echo "<td>" . $row['damageS'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Damage(M): </td>";
echo "<td>" . $row['damageM'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Critical: </td>";
echo "<td>" . $row['critical'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Range Increment: </td>";
echo "<td>" . $row['rangeIncrement'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Weight: </td>";
echo "<td>" . $row['weight'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Weapon Type: </td>";
echo "<td>" . $row['weaponType'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Masterwork: </td>";
echo "<td>" . $row['masterwork'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Special Abilities: </td>";
echo "<td>" . $row['specialAbilities'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Additional Info: </td>";
echo "<td>" . $row['additionalInfo'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "</center>";
mysqli_close($con);
?>
try this --
$result = mysqli_query($con,"SELECT * FROM weapons");
while($row = mysqli_fetch_array($result))
{
$row['masterwork'] = ( intval( $row['masterwork']) == 1) ? "YES" : "NO";
echo "<center>";
echo "<table border='1' class='display'>";
echo "<tr>";
echo "<td>Weapon Name: </td>";
echo "<td>" . $row['weaponName'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Creator: </td>";
echo "<td>" . $row['creator'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Weapon Category: </td>";
echo "<td>" . $row['weaponCategory'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Weapon Sub-Category: </td>";
echo "<td>" . $row['weaponSubCategory'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Cost: </td>";
echo "<td>" . $row['costAmount'] . " " . $row['costType'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Damage(S): </td>";
echo "<td>" . $row['damageS'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Damage(M): </td>";
echo "<td>" . $row['damageM'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Critical: </td>";
echo "<td>" . $row['critical'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Range Increment: </td>";
echo "<td>" . $row['rangeIncrement'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Weight: </td>";
echo "<td>" . $row['weight'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Weapon Type: </td>";
echo "<td>" . $row['weaponType'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Masterwork: </td>";
echo "<td>" . $row['masterwork'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Special Abilities: </td>";
echo "<td>" . $row['specialAbilities'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Additional Info: </td>";
echo "<td>" . $row['additionalInfo'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "</center>";
mysqli_close($con);
?>

How to add up totals using php?

been trying out my new skills in php to build an accounts style system, that logs incoming and out going payments similar to what you would do in excel. thought this would be a good starting point to test my stills.
now ive built a form which submits to a database, and also a page that allows you to view the payments logged via date.
stuck on getting it to display a profit total. This will be worked out from the incoming payments total minus the outgoing payments totals.
ive attached my code below, id really appreciate any help i can get on this.
<style>
</style>
<?php
include 'db-connect.php';
$result = mysqli_query($con,"SELECT * FROM payments");
echo "<table border='0' align='center' text-align='left'>
<tr>
<th>Title:</th>
<th>Date:</th>
<th>Incoming:</th>
<th>Outgoing:</th>
<th>Notes:</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['incoming'] . "</td>";
echo "<td>" . $row['outgoing'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
after adding code provided below:
<style>
</style>
<?php
include 'db-connect.php';
$result = mysqli_query($con,"SELECT * FROM payments");
$totalIncoming = 0;
$totalOutgoing = 0;
while($row = mysqli_fetch_array($result))
{
$totalIncoming .= $row['incoming'];
$totalOutgoing .= $row['outgoing'];
}
echo "<table border='0' align='center' text-align='left'>
<tr>
<th>Title:</th>
<th>Date:</th>
<th>Incoming:</th>
<th>Outgoing:</th>
<th>Notes:</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['incoming'] . "</td>";
echo "<td>" . $row['outgoing'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "</tr>";
}
echo "</table>";
$profit = $totalIncoming - $totalOutgoing;
echo "Profit :".$profit;
mysqli_close($con);
?>
ok is this right?
<style>
</style>
<?php
include 'db-connect.php';
$result = mysqli_query($con,"SELECT * FROM payments");
$totalIncoming = 0;
$totalOutgoing = 0;
while($row = mysqli_fetch_array($result))
{
$totalIncoming += $row['incoming'];
$totalOutgoing += $row['outgoing'];
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['incoming'] . "</td>";
echo "<td>" . $row['outgoing'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "</tr>";
}
echo "</table>";
$profit = $totalIncoming - $totalOutgoing;
echo "Profit :".$profit;
mysqli_close($con);
?>
Try This
$totalIncoming = 0;
$totalOutgoing = 0;
while($row = mysqli_fetch_array($result))
{
$totalIncoming += $row['incoming'];
$totalOutgoing += $row['outgoing'];
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['incoming'] . "</td>";
echo "<td>" . $row['outgoing'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "</tr>";
}
echo "</table>";
$profit = $totalIncoming - $totalOutgoing;
echo "Profit :".$profit;

Categories