Display Form Fields Based on User Input - php

I'm displaying a set of rows here for every car I have in my database.
Each row has a form field where a logged in user can submit an offer.
When a user has made an offer for any car, the form field is replaced by a text displaying the value of the offer submitted.
What I'm experiencing, however, is less than ideal results.
If I make an offer for one row, great, the logic works. If I go ahead and make another offer for another row then the logic works, except the fact that the previous row now displays the form again.
I can provide more details if necessary but perhaps someone is familiar with this already.
Thanks in advance.
<?php
require("db-connect.php");
$display = "SELECT filename, car_id, make, model, year, mileage, vin, description, GROUP_CONCAT(filename) FROM scraplis_cars LEFT JOIN scraplis_images USING (car_id) GROUP BY car_id ORDER BY date_time DESC";
$dResult = mysql_query($display) or die('error:' . mysql_error());
$offer = "SELECT car_id, user_id, offer_id, value FROM scraplis_offers WHERE user_id = '".$_SESSION['user_id']."'";
$oResult = mysql_query($offer) or die('Error ' . mysql_error());
$oRow = mysql_fetch_array($oResult);
if(!isset($_SESSION['access'])){
header("location:index.php");
}
?>
<?php if($dResult): ?>
<table class="post">
<thead>
<tr>
<?php if(isset($_SESSION['email']) && $_SESSION['access'] == 0) : ?>
<th scope="col">Images</th>
<th scope="col">Make</th>
<th scope="col">Model</th>
<th scope="col">Year</th>
<th scope="col">Mileage</th>
<th scope="col">VIN #</th>
<th scope="col">Description</th>
<th scope="col">Offer</th>
</tr>
</thead>
<tbody>
<?php while($dRow = mysql_fetch_array($dResult)) : ?>
<?php $str = $dRow[8]; ?>
<?php $images = explode(',', $str); ?>
<tr>
<td>
<ul>
<?php if(!empty($str)) : ?>
<?php foreach($images as $value) :?>
<li>
<a href="images/<?php echo $value; ?>" rel="lightbox[<?php echo $row['car_id']; ?>]">
<img src="images/<?php echo $value; ?>"/>
</a>
</li>
<?php endforeach; ?>
<?php endif; ?>
<ul>
</td>
<td><?php echo $dRow['make']; ?></td>
<td><?php echo $dRow['model']; ?></td>
<td><?php echo $dRow['year']; ?></td>
<td><?php echo number_format($dRow['mileage']); ?></td>
<td><?php echo $dRow['vin']; ?></td>
<td><span><?php echo $dRow['description']; ?></span></td>
<td>
<?php if($oRow['car_id'] == $dRow['car_id']) : ?>
Offer pending approval - $<?php echo $oRow['value']; ?>
<?php else : ?>
<form id="offer" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<input type="text" id="price" name="offer" />
<input type="hidden" name="submitted" value="<?php echo $dRow['car_id']; ?>" />
<input type="submit" name="price" value="Submit" />
</form>
<?php endif; ?>
</td>
</tr>
<?php endwhile; ?>
<?php else : ?>
<th scope="col">Delete</th>
<th scope="col">Images</th>
<th scope="col">Make</th>
<th scope="col">Model</th>
<th scope="col">Year</th>
<th scope="col">Mileage</th>
<th scope="col">VIN #</th>
<th scope="col">Description</th>
</tr>
</thead>
<tbody>
<?php while($dRow = mysql_fetch_array($dResult)) : ?>
<?php $str = $dRow[8]; ?>
<?php $images = explode(',', $str); ?>
<tr>
<td>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<input type="checkbox" name="record" value="<?php echo $row['car_id']; ?>" />
<input type="submit" name="delete-car" value="Delete" />
</form>
</td>
<td>
<ul>
<?php if(!empty($str)) : ?>
<?php foreach($images as $value) :?>
<li>
<a href="images/<?php echo $value; ?>" rel="lightbox[<?php echo $row['car_id']; ?>]">
<img src="images/<?php echo $value; ?>"/>
</a>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
</td>
<td><?php echo $dRow['make']; ?></td>
<td><?php echo $dRow['model']; ?></td>
<td><?php echo $dRow['year']; ?></td>
<td><?php echo number_format($dRow['mileage']); ?></td>
<td><?php echo $dRow['vin']; ?></td>
<td><span><?php echo $dRow['description']; ?></span></td>
</tr>
<?php endwhile; ?>
<?php endif; ?>
</tbody>
</table>
<?php endif; ?>

One important thing for security first:
SEARCH:
if(!isset($_SESSION['access'])){
header("location:index.php");
}
REPLACE WITH:
if(!isset($_SESSION['access'])) {
header("Location: index.php");
exit;
}
Take a look in the PHP documentation for header() or exit() - both describe the need (or security issue) of exit() here afair.
To your question:
You just have the first row of $oResult in $oRow - so you have (for example) 1000 cars but just one offer. You need to fetch the results of $oResult within a loop (while(), for(),... - what you prefer...) and then check wether you can find car_id (within $dRow also in the offers).
code sample (very easy for understanding):
<?php
// ...
// get the offers
// info: user_id would not be necessary here ;-)
$offer = "SELECT car_id, user_id, offer_id, value FROM scraplis_offers WHERE user_id = '".$_SESSION['user_id']."'";
$oResult = mysql_query($offer) or die('Error ' . mysql_error());
$oRows = array();
while($oRow = mysql_fetch_array($oResult)) {
$oRows[$oRow['car_id']] = array(
'offer_id' => $oRow['offer_id'],
'value' => $oRow['value']
);
}
// looping the through the cars
// just the while()-loop based on your code
while($dRow = mysql_fetch_array($dResult)) {
// check if offer exists
if(array_key_exists($dRow['car_id'], $oRows)) {
// H A V E an offer for that car ;-) - show offer details
} else {
// H A V E N O offer that car - show form
}
}
// ...
?>
I hope I didn't get you wrong, made no mistakes (needed to get up early) and this helps you ;-).

Related

How to view images from database on to the website document?

I'm trying to show pictures stored in a database but when I run the code, a brunch of text of the picture comes up. (shown in the image below)
here's my code for displaying the contents of the database
<?php
include ("connectdb.php");
try {
$rows = $db -> query("SELECT animal_id, title, animal_dob, animal_type, animal_description,picture_of_animal FROM animals");
?> <table>
<tr>
<th> Animal id <th> Title </th> <th> Date of birth </th> <th> Type </th><th>Description</th><th>Picture</th>
</tr> <?php
foreach ($rows as $rows => $r) {
?>
<tr>
<td><?php echo $r['animal_id']; ?></td>
<td><?php echo $r['title']; ?> </td>
<td><?php echo $r['animal_dob']; ?> </td>
<td><?php echo $r['animal_description']; ?> </td>
<td><img src "<?php echo $r['picture_of_animal']; ?>" </td>
</tr>
<?php
}
?>
</table>;
<?php
}catch (PDOException $ex) {
?>
<p>Sorry, a database error occurred.</p>
<p> Error details: <em> <?= $ex->getMessage() ?> </em></p>
<?php
}
?>
Heres a picture of my datatable
you missed equal sign so add it and alao image close tag
<td><img src= "<?php echo $r['picture_of_animal']; ?>"/> </td>

Table with select filter

I have a table with 4 columns that I want to sort via a select form.
For example, here I have a select to sort the Type column (column 3) with 3 possibilities:
- Site Sportif
- Site de bloc
- Terrain d’aventure
How do you do it?
<div class="stages">
<form id="form-sne-filtre" method="post">
<div>
<select name="type" id="select_1">
<option value=""><?php _e('Filtrer par type'); ?></option>
<option value="0"> <?php _e('Site Sportif'); ?></option>
<option value="1"><?php _e('Site de bloc'); ?></option>
<option value="2"><?php _e('Terrain d\'aventure'); ?></option>
</select>
</div>
</form>
<table id="myTable">
<thead>
<tr>
<th><?php _e('Nom'); ?></th>
<th><?php _e('Ville'); ?></th>
<th><?php _e('Type'); ?></th>
<th><?php _e('Niveau'); ?></th>
</tr>
</thead>
<tbody>
<?php foreach($_POST as $key => $value){
if($value != "") $args[$key] = $value;
}
foreach($snes->items as $sne) { ?>
<tr>
<td><?php echo $sne->SNE_VILLE; ?> <?php echo $sne->SNE_NOM; ?></td>
<td><?php echo $sne->SNE_COMMUNES; ?></td>
<td><?php
$array = array(0 => __('Site sportif'),1 => __('Site de bloc'), 2 => __('Terrain d\'aventure'));
echo $array[$sne->SNE_TYPESITE];
?></td>
<td><?php _e("Nombre de voies : "); ?><?php echo $sne->SNE_NBRVOIES; ?> <?php _e('De'); ?> <?php echo $sne->SNE_NIVEAUVOIE_MIN; ?> <?php _e('à'); ?> <?php echo $sne->SNE_NIVEAUVOIE_MAX;; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
If you're using jQuery, you can submit the form whenever a user selects a sort option:
1. Bind events
Add the following tag below div.stages tag
<script>
$("#select_1").on("change", function(e) => {
$("#form-sne-filtre").submit();
})
</script>
2. Handle post param in php
<?php
$sort = $_POST["type"] ?? "";
$snes = sortSnes($snes, $sort); // you have to implement sortSnes function by yourself, don't ask me
?>
<tbody>
<?php foreach($snes as $sne): ?>
<tr>
// render table rows
</tr>
<?php endforeach;?>
</tbody>

Generate multiple table (For viewing)

I would like to ask on how to make my table to appear as following:
But I only manage to get my table to appear like this:
As you can see, the table is separated according to the different year or semester of when they stored their stuff. It is basically a history storage of that particular person. Unfortunately, I don't know how to make the generated table to attach together for the one with same year and semester instead of separating it. Even the numbering is affected. Below is the code that I have so far:
<?php
include("connect.php");
include("header.php");
if(isset($_GET['invstuview_bag'], $_GET['invstuview_name']))
{
$get_id = $_GET['invstuview_bag'];
$get_name = $_GET['invstuview_name'];
?>
<br/>
<ul class="breadcrumb">
<li class="breadcrumb-item">View History</li>
<li class="breadcrumb-item-active">Baggage Detail History</a></li>
</ul>
<div id="cssword">Baggage Detail History For <?php echo $get_name; ?>(<?php echo $get_id; ?>)</div>
<div class="container" style="width:70%;">
<div class="table-responsive">
<?php
$sql_join = "SELECT student.*,location.*, inventory.*, baggage.*
FROM
student,location, inventory, baggage
WHERE
student.student_id = inventory.invstu_id AND
baggage.baggage_id = inventory.invbag_id AND
location.location_id = inventory.invloc_id AND
invstu_id = '$get_id'
ORDER BY inventory_id";
$result_join= mysqli_query($connect,$sql_join);
$prev_year = "";
$prev_sem = 0;
$get_year = "";
$get_sem = 0;
while($row_join=mysqli_fetch_assoc($result_join))
{
$counter = 1;
$prev_year = $row_join["invstu_year"];
$prev_sem = $row_join["invstu_sem"];
//if the data is of same year and semester
if(($prev_year!="") && ($prev_sem!=0) && ($prev_year == $get_year) && ($prev_sem == $get_sem))
{
$get_year = $row_join["invstu_year"];
$get_sem = $row_join["invstu_sem"];
?>
<table class="table table-bordered">
<tr>
<th>No.</th>
<th>Baggage Types</th>
<th>Quantity</th>
<th>Location</th>
</tr>
<tr>
<td><?php echo $counter; ?></td>
<td><?php echo $row_join["baggage_type"]; ?></td>
<td><?php echo $row_join["invbag_quantity"]; ?></td>
<td><?php echo $row_join["location_house"]; ?></td>
</tr>
<?php
$counter++;
echo'</table>';
}
//if data is not of same year or semester
else
{
$get_year = $row_join["invstu_year"];
$get_sem = $row_join["invstu_sem"];
?>
</br></br>
Room: <?php echo $row_join["invstu_room"]; ?><br/>
Year: <?php echo $row_join["invstu_year"]; ?><br/>
Semester: <?php echo $row_join["invstu_sem"]; ?><br/>
<table class="table table-bordered">
<tr>
<th>No.</th>
<th>Baggage Types</th>
<th>Quantity</th>
<th>Location</th>
</tr>
<tr>
<td><?php echo $counter; ?></td>
<td><?php echo $row_join["baggage_type"]; ?></td>
<td><?php echo $row_join["invbag_quantity"]; ?></td>
<td><?php echo $row_join["location_house"]; ?></td>
</tr>
<?php
$counter++;
echo'</table>';
}
}
?>
</div>
<div align="right">
<button type="button" name="back" class="btn btn-success" id="back" style="width:200px; display:inline-block; margin-top:2%; margin-bottom:1%; background-color: #4CAF50" onclick="window.location.href='view.php'">Back</button>
</div>
</div>
<?php
}
?>
Any ideas is highly appreciated.
Move these out of the while loop:
<table class="table table-bordered">
<tr>
<th>No.</th>
<th>Baggage Types</th>
<th>Quantity</th>
<th>Location</th>
</tr>
echo'</table>';

Inserting foreign keys into a table from the website using php (phpMyAdmin)

So, I have 3 tables in my database (phpMyAdmin) bikes, book and users. I have made bike_id and user_id a foreign key in the book table. Now I want these 2 to get inserted into the book table from the web page.
I've got 3 php files which are book1.php, book2.php and book3.php. In book1, you can select a bike which you want to book which results in showing that particular bike in new page. In book2, you select the date when you want to book and when you go to book3, the user_id and bike_id should be automatically inserted into the book table? But I only get ERROR when I press book in book2 page.
Can someone help me plz.. Below is the php codes for 3 php files.
book1.php:
<?php
require 'connect.php';
$select_posts = "select * from bikes ";
$run_posts = mysql_query($select_posts);
?>
<table cellpadding="2" cellspacing="2" border="2">
<tr>
<th>Name</th>
<th>Image</th>
<th>Available</th>
<th>Select Bike</th>
</tr>
<?php while($row=mysql_fetch_array($run_posts)){ ?>
<tr>
<td><?php echo $row[bike_name]; ?></td>
<?php echo "<td>";?> <img src="<?php echo $row[bike_image]; ?>" height="250" width="300"> <?php echo "</td>";?>
<td><?php echo $row[avail]; ?></td>
<td><a href="book2.php?id=<?php echo $row[bike_id];?>">Select</td>
</tr>
<?php } ?>
book2.php:
<?php
session_start();
?>
<?php
require 'connect.php';
if(isset($_GET['id'])){
$took_id = $_GET['id'];
$select_query = "select * from bikes where bike_id='$took_id'";
$run_query = mysql_query($select_query);
?>
<table cellpadding="2" cellspacing="2" border="2">
<tr>
<th>Name</th>
<th>Image</th>
<th>Available</th>
<th></th>
<th>Select Date</th>
<th>Book</th>
</tr>
<?php while($row=mysql_fetch_array($run_query)){ ?>
<tr>
<form action="book3.php" method="POST">
<td><?php echo $row[bike_name]; ?></td>
<?php echo "<td>";?> <img src="<?php echo $row[bike_image]; ?>" height="250" width="300"> <?php echo "</td>";?>
<td><?php echo $row[avail]; ?></td>
<td><?php echo $took_id; ?></td>
<td>Select Date: <input type="text" id="datepicker" name="datepicker"></td>
<td><input type="submit" name="Submit" value="Book"></td>
</tr>
<?php }} ?>
</table>
book3.php:
<?php
session_start();
?>
<?php
require 'connect.php';
// Get values from form
$datepicker = $_POST['datepicker'];
$sql="INSERT INTO $tbl_name(datepicker)VALUES('$datepicker')";
$sql="INSERT INTO $tbl_name(user_id)VALUES(select user_id from users)";
$sql="INSERT INTO $tbl_name(bike_id)VALUES(select bike_id from bikes)";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
}
else {
echo "ERROR";
}
?>

Dynamic rowspan table in PHP

I have a problem getting a table to work.
I want to adjust a printable form for order list (opencart),
but I want address box on left and products ordered on right.
The problem is the number of products varies per order, so using rowspan on the address box does not work (I left the rowspan in the code).
<thead>
<tr>
<td><b><?php echo $text_to; ?></b></td>
<td><b><?php echo $column_product; ?></b></td>
<td class="text-center"><b><?php echo $column_quantity; ?></b></td>
</tr>
</thead>
<tbody><tr>
<td rowspan="10"><?php echo $order['shipping_address']; ?><br/><br/>
<?php echo $order['telephone']; ?>
<br />
<?php if ($order['shipping_method']) { ?>
<b><?php echo $text_shipping_method; ?></b> <?php echo $order['shipping_method']; ?><br />
<?php } ?></td></tr>
<?php foreach ($order['product'] as $product) { ?>
<td><?php echo $product['name']; ?>
<?php foreach ($product['option'] as $option) { ?>
<br />
<small> - <?php echo $option['name']; ?>: <?php echo $option['value']; ?></small>
<?php } ?></td>
<td class="text-center"><?php echo $product['quantity']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
You have all the information available it seems so you just need to change:
<td rowspan="10">
to:
<td rowspan="<?php echo count($order['product']); ?>">
However, you do need to check the generated html as it looks like you are not generating valid rows so you might need to change the logic a bit.

Categories