Danial Kosarifa
Danial Kosarifa

Reputation: 1074

selected row of a table and it's value with PHP

I have inserted all the payments done from my database into a table and all their information they I was meant to show to the visitor. However, I'd like to add this feature that by the time that the visitor select each row they can visit all the products they've bought on that day. In other words I want a modal to pops out and show the invoice for that purchase (Meaning that all the products that were bought at the same day). However how do I get which date has been selected?

<?php 
    $myQuery = mysql_query("SELECT * FROM `payment` WHERE UserID = $uid;");
?> 
<table class="table table-striped"> 
    <thead>
        <tr>
            <th>ID</th>
            <th>Product Name</th>
            <th>Brand</th>
            <th>Price</th>
            <th>Quantity</th>
            <th>Date</th>
        </tr>
    </thead>
    <tbody>
        <?php
            while ($row = mysql_fetch_assoc($myQuery)){
        ?>
            <tr data-toggle="modal" data-id="1" data-target="#orderModal">
                <td><?php echo $row["ProductID"] ?></td>
                <td><?php echo $row["ProductName"] ?></td>
                <td><?php echo $row["Brand"] ?></td>
                <td><?php echo $row["Price"] ?></td>
                <td><?php echo $row["Quantity"] ?></td>
                <td><?php echo $row["Date"] ?></td>
            </tr>
        <?php
            }
        ?>
    </tbody>
</table> 

Upvotes: 1

Views: 3302

Answers (4)

Manjeet Barnala
Manjeet Barnala

Reputation: 2995

Use Jquery DATEPICKER to choose date and submit form on your target file where query is written

Get the date on that page like this:

$date = date('Y-m-d');
if(iseet($_POST['date']))
{
    $date = date("Y-m-d", strtotime($_POST['date']));
}
$myQuery = mysql_query("SELECT * FROM `payment` WHERE UserID = $uid AND Date = '$date' ");

Here is the code for datepicker

  $(function() {
    $( "#datepicker" ).datepicker();
  });
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<form action="yourfile.php" method ="post">
<p>Date: <input type="text" name="date" id="datepicker"></p>
<input type="submit" value="Submit">
</form>
 

Upvotes: 1

Moussa Khalil
Moussa Khalil

Reputation: 635

mysql_query("SELECT * FROM `payment` WHERE UserID = $uid AND Date  between '$date1' and '$date2'");

$date1 is the date of purchase with hour, minutes and seconds set to 0s

$date2 is date of purchase with hours set to 23, minutes to 59 and seconds to 59

if $date is 2015-05-05 16:34:22, $date1 is 2015-05-05 00:00:00 and $date2 is 2015-05-05 23:59:59 , same day :)

Upvotes: 0

prabodhtiwari
prabodhtiwari

Reputation: 228

Use a date selector for selecting the date and after that make a ajax call to you php function with that selected date and then fire a query like SELECT * FROMpaymentWHERE UserID = $uid and Date = $date; and the popup a model with returned data from ajax call.

Upvotes: 0

aynber
aynber

Reputation: 23001

There are several ways to go about it. First, you can use PHP to select the date:

$date = date('Y-m-d');
$myQuery = mysql_query("SELECT * FROM `payment` WHERE UserID = $uid AND Date = '$date';");

Or you can use MySQL's CURDATE() method:

$myQuery = mysql_query("SELECT * FROM `payment` WHERE UserID = $uid AND Date = CURDATE();");

This will work if your date format is in Y-m-d (2016-06-03) format. If it's in another format, you're going to have to do a bit of DATE_FORMAT manipulation. This will only work for "today". Otherwise, go with the first method and pass in the desired date.

As an aside, mysql_* functions are deprecated, and removed as of PHP7. I'd really recommend using PDO or mysqli, and using it to bind your parameters so you don't have to worry about mysql injection.

Upvotes: 1

Related Questions