Submit multiple values only from one button - php

Hi i am trying to submit the multiple form values only from one button i have 10 records and one record has one button of submit and 10 records have 10 buttons of submit what i want there should be only one button of submit and i can submit he whole 10 records from one button:
echo "<table class='table table-hover table-responsive table-bordered'>";
// our table heading
echo "<tr>";
echo "<th class='textAlignLeft'>Product Name</th>";
echo "<th>Price</th>";
echo "<th style='width:5em;'>Quantity</th>";
echo "<th>Image</th>";
echo "<th>Action</th>";
echo "</tr>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
//creating new table row per record
echo "<tr>";
echo "<td>";
echo "<div class='product-id' style='display:none;'>{$id}</div>";
echo "<div class='product-name'>{$name}</div>";
echo "</td>";
echo "<td>$" . number_format($price, 2, '.' , ',') . "</td>";
if(isset($quantity)){
echo "<td>";
echo "<input type='text' name='quantity' value='{$quantity}' disabled class='form-control' />";
echo "</td>";
echo "<td>";
echo "<td>";
echo "<button class='btn btn-success' disabled>";
echo "<span></span> Submitted!";
echo "</button>";
echo "</td>";
echo "</td>";
}else{
echo "<td>";
echo "<input type='number' name='quantity[]' value='1' class='form-control'/>";
echo "</td>";
echo "<td><img src='product-images/{$image}' width='60' height='60'</td>";
echo "<td>";
echo "<button class='btn btn-primary add-to-cart'>";
echo "<span></span>Submit to cart";
echo "</button>";
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
}
what i want only one button from i can submit all my values: Any help would be high appreciated:
$action = isset($_GET['action']) ? $_GET['action'] : "";
$product_id = isset($_GET['product_id']) ? $_GET['product_id'] : "1";
$name = isset($_GET['name']) ? $_GET['name'] : "";
$quantity = isset($_GET['quantity']) ? $_GET['quantity'] : "1";
$image = isset($_GET['image']) ? $_GET['image'] : "1";
if($action=='added'){
echo "<div class='alert alert-info'>";
echo "<strong>{$name}</strong> added to your cart!";
echo "</div>";
}
else if($action=='failed'){
echo "<div class='alert alert-info'>";
echo "<strong>{$name}</strong> failed to add to your cart!";
echo "</div>";
}
// left join to select products
$query = "SELECT p.id, p.name, p.price, p.image, ci.quantity
FROM products p
LEFT JOIN cart_items ci
ON p.id = ci.product_id
ORDER BY p.name";
$stmt = $con->prepare( $query );
$stmt->execute();
// count number of products returned
$num = $stmt->rowCount();
HTML CODE
<?php
// connect to database
include 'config/db_connect.php';
// page headers
$page_title="Cart";
include 'layout_head.php';
// parameters
$action = isset($_GET['action']) ? $_GET['action'] : "";
$name = isset($_GET['name']) ? $_GET['name'] : "";
$image = isset($_GET['image']) ? $_GET['image'] : "";
// display a message
if($action=='removed'){
echo "<div class='alert alert-info'>";
echo "<strong>{$name}</strong> was removed from your cart!";
echo "</div>";
}
else if($action=='quantity_updated'){
echo "<div class='alert alert-info'>";
echo "<strong>{$name}</strong> quantity was updated!";
echo "</div>";
}
else if($action=='failed'){
echo "<div class='alert alert-info'>";
echo "<strong>{$name}</strong> quantity failed to updated!";
echo "</div>";
}
else if($action=='invalid_value'){
echo "<div class='alert alert-info'>";
echo "<strong>{$name}</strong> quantity is invalid!";
echo "</div>";
}
// select products in the cart
$query="SELECT p.id, p.name, p.price, p.image, ci.quantity, ci.quantity * p.price AS subtotal
FROM cart_items ci
LEFT JOIN products p
ON ci.product_id = p.id";
$stmt=$con->prepare( $query );
$stmt->execute();
// count number of rows returned
$num=$stmt->rowCount();
if($num>0){
//start table
echo "<table class='table table-hover table-responsive table-bordered'>";
// our table heading
echo "<tr>";
echo "<th class='textAlignLeft'>Product Name</th>";
echo "<th>Price</th>";
echo "<th style='width:15em;'>Quantity</th>";
echo "<th>Sub Total</th>";
echo "<th>Action</th>";
echo "</tr>";
$total=0;
while( $row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
echo "<tr>";
echo "<td>";
echo "<div class='product-id' style='display:none;'>{$id}</div>";
echo "<div class='product-name'>{$name}</div>";
echo "</td>";
echo "<td>$" . number_format($price, 2, '.', ',') . "</td>";
echo "<td>";
echo "<div class='input-group'>";
echo "<input type='number' name='quantity[]' value='{$quantity}' class='form-control'>";
echo "<span class='input-group-btn'>";
echo "<button class='btn btn-default update-quantity' type='button'>Update</button>";
echo "</span>";
echo "</div>";
echo "</td>";
echo "<td>$" . number_format($subtotal, 2, '.', ',') . "</td>";
echo "<td>";
echo "<a href='remove_from_cart.php?id={$id}&name={$name}' class='btn btn-danger'>";
echo "<span></span> Delete from cart";
echo "</a>";
echo "</td>";
echo "</tr>";
$total += $subtotal;
}
echo "<tr>";
echo "<td><b>Total</b></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td>$" . number_format($total, 2, '.', ',') . "</td>";
echo "<td>";
echo "</a>";
echo "</td>";
echo "</tr>";
echo "</table>";
}else{
echo "<div class='alert alert-danger'>";
echo "<strong>No products found</strong> in your cart!";
echo "</div>";
}
include 'layout_foot.php';
?>

Use name quantity[] instead of quantity.By this way you can receive post data as an array. After the loop use only one submit button.
echo "<input type='number' name='quantity[]' value='1' class='form-control'/>";
Then receive it by using foreach
foreach($_POST['quantity'] as $qty)
{
echo $qty;
//Here you will receive all the quantities one by one.
}
Form the code
<?php
echo "<table class='table table-hover table-responsive table-bordered'>";
// our table heading
echo "<tr>";
echo "<th class='textAlignLeft'>Product Name</th>";
echo "<th>Price</th>";
echo "<th style='width:5em;'>Quantity</th>";
echo "<th>Image</th>";
echo "</tr>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
//creating new table row per record
echo "<tr>";
echo "<td>";
echo "<div class='product-id' style='display:none;'>{$id}</div>";
echo "<div class='product-name'>{$name}</div>";
echo "</td>";
echo "<td>$" . number_format($price, 2, '.' , ',') . "</td>";
if(isset($quantity)){
echo "<td>";
echo "<input type='text' name='quantity[]' value='{$quantity}' disabled class='form-control' />";
echo "</td>";
echo "<td>";
echo "</td>";
echo "<td>";
echo "</td>";
}else{
echo "<td>";
echo "<input type='number' name='quantity[]' value='1' class='form-control'/>";
echo "</td>";
echo "<td><img src='product-images/{$image}' width='60' height='60'</td>";
echo "<td>";
echo "</td>";
}
echo "</tr>";
}
echo "<tr><td colspan='4'>";
echo "<button class='btn btn-primary add-to-cart' type='submit'>";
echo "<span></span>Submit to cart";
echo "</button>";
echo "</td></tr>";
echo "</table>";

Related

Href for button PHP HTML

Hi i want to href after user click a button
This is my current code but it's not working. After I click, the confirmation will come out but it doesn't go to PHadmin_approveHospital.php after I click on.
echo "<input type=\"submit\" value=\"Approve\">";
Full Function
public function displayAllHospital() {
$sql = "SELECT * FROM hospital";
$result = #mysqli_query($this->conn, $sql);
echo "<table class='table table-bordered'>";
echo "<thead>";
echo "<tr>";
echo "<th>ID # <i class='fa fa-sort'></i></th>";
echo "<th>Name </th>";
echo "<th>Email </th>";
echo "<th>Contact Number <i class='fa fa-sort'></i></th>";
echo "<th>Status </th>";
echo "<th>Actions</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = mysqli_fetch_assoc($result)){
echo "<tr>";
echo "<td>" . $row["HospitalID"] . "</td>";
echo "<td>" . $row["Hospitalname"] . "</td>" ;
echo "<td>" . $row["email"] . "</td>" ;
echo "<td>" . $row["contactno"] . "</td>" ;
echo "<td>" . $row["status"] . "</td>" ;
echo "<td>";
echo "<a href=\"PHadmin_editHospital.php?id=".$row["HospitalID"]."\" class='view' title='View' data-toggle='tooltip'><i class='material-icons'></i></a>";
echo "<a href=\"PHadmin_editHospital.php?id=".$row["HospitalID"]."\" class='edit' title='Edit' data-toggle='tooltip'><i class='material-icons'></i></a>";
echo "<a href=\"PHadmin_deleteHospital.php?id=".$row["HospitalID"]."\" onclick=\"return confirm('do you want to delete Y/N')\" class='delete' title='Delete' data-toggle='tooltip'><i class='material-icons'></i></a>";
echo "</td>";
echo "<td>";
if($row["status"] == "pending"){
echo "<input type=\"submit\" value=\"Approve\">";
}
echo "</td>";
echo "</tr>";
echo "</tbody>";
echo "</form>";
echo "</tr>";
}
echo "</table>";
}
Thanks for helping.
You may call a javascript function when you click the button, and then display the confirmation message, so that the user can confirm or not (if confirmed, jump to the url)
You may use the following code :
<input type="button" value='Approve' onclick="javascript:check1();">
<script>
function check1() {
if(confirm("Do you want to approve?")) {
window.location.href="PHadmin_approveHospital.php?id=<?php echo $row["HospitalID"]. '\'; ?>";
}
}
</script>
So , for your updated code, it will be:
<?php
public function displayAllHospital() {
echo '
<script>
function check1(var1) {
if(confirm("Sure to delete ?")) {
window.location.href="PHadmin_deleteHospital.php?id=" +var1;
}
}
function check2(var2) {
if(confirm("Sure to approve ?")) {
window.location.href="PHadmin_approveHospital.php?id=" + var2;
}
}
</script>';
$sql = "SELECT * FROM hospital";
$result = #mysqli_query($this->conn, $sql);
echo "<table class='table table-bordered'>";
echo "<thead>";
echo "<tr>";
echo "<th>ID # <i class='fa fa-sort'></i></th>";
echo "<th>Name </th>";
echo "<th>Email </th>";
echo "<th>Contact Number <i class='fa fa-sort'></i></th>";
echo "<th>Status </th>";
echo "<th>Actions</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = mysqli_fetch_assoc($result)){
echo "<tr>";
echo "<td>" . $row["HospitalID"] . "</td>";
echo "<td>" . $row["Hospitalname"] . "</td>" ;
echo "<td>" . $row["email"] . "</td>" ;
echo "<td>" . $row["contactno"] . "</td>" ;
echo "<td>" . $row["status"] . "</td>" ;
echo "<td>";
echo "<a href=\"PHadmin_editHospital.php?id=".$row["HospitalID"]."\" class='view' title='View' data-toggle='tooltip'><i class='material-icons'></i></a>";
echo "<a href=\"PHadmin_editHospital.php?id=".$row["HospitalID"]."\" class='edit' title='Edit' data-toggle='tooltip'><i class='material-icons'></i></a>";
echo "<input type=button value=Delete onclick='javascript:check1(". $row["HospitalID"] . ")';>";
echo "</td>";
echo "<td>";
if($row["status"] == "pending"){
echo "<input type=button value=Approve onclick='javascript:check2(". $row["HospitalID"] . ")';>";
}
echo "</td>";
echo "</tr>";
echo "</tbody>";
echo "</form>";
echo "</tr>";
}
echo "</table>";
}
?>

I am getting "Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result" Error in Php [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 2 years ago.
Attempting to create display order page for admin, but getting this error in my code. I am not getting the error after so much effort. help me!!
Error: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result
Here's my Code:
<div class="grid_10">
<div class="box round first">
<h2>Display Order</h2>
<div class="block">
<?php
$res = mysqli_query($link, "select * from 'confirm_order_address' order by id desc");
echo "<table border='1'>";
echo "<tr>";
echo "<td style='font-weight:bold'>"; echo "fullname"; echo "</td>";
echo "<td style='font-weight:bold'>"; echo "mobileno"; echo "</td>";
echo "<td style='font-weight:bold'>"; echo "email"; echo "</td>";
echo "<td style='font-weight:bold'>"; echo "pincode"; echo "</td>";
echo "<td style='font-weight:bold'>"; echo "address"; echo "</td>";
echo "<td style='font-weight:bold'>"; echo "landmark"; echo "</td>";
echo "<td style='font-weight:bold'>"; echo "city"; echo "</td>";
echo "</tr>";
while($row = mysqli_fetch_array($res))
{
echo "<tr>";
echo "<td>"; echo $row["fullname"]; echo "</td>";
echo "<td>"; echo $row["mobileno"]; echo "</td>";
echo "<td>"; echo $row["email"]; echo "</td>";
echo "<td>"; echo $row["pincode"]; echo "</td>";
echo "<td>"; echo $row["address"]; echo "</td>";
echo "<td>"; echo $row["landmark"]; echo "</td>";
echo "<td>"; echo $row["city"]; echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
</div>
</div>
</div>
<div class="grid_10">
<div class="box round first">
<h2>Display Order</h2>
<div class="block">
<?php
$sql_select = 'SELECT * FROM confirm_order_address ORDER BY id DESC';
$result_select = $link->query($sql_select);
if ($result_select->num_rows > 0) {
echo "<table border='1'>";
echo "<tr>";
echo "<td style='font-weight:bold'>"; echo "fullname"; echo "</td>";
echo "<td style='font-weight:bold'>"; echo "mobileno"; echo "</td>";
echo "<td style='font-weight:bold'>"; echo "email"; echo "</td>";
echo "<td style='font-weight:bold'>"; echo "pincode"; echo "</td>";
echo "<td style='font-weight:bold'>"; echo "address"; echo "</td>";
echo "<td style='font-weight:bold'>"; echo "landmark"; echo "</td>";
echo "<td style='font-weight:bold'>"; echo "city"; echo "</td>";
echo "</tr>";
while ($row_select = $result_select->fetch_assoc()) {
echo "<tr>";
echo "<td>"; echo $row["fullname"]; echo "</td>";
echo "<td>"; echo $row["mobileno"]; echo "</td>";
echo "<td>"; echo $row["email"]; echo "</td>";
echo "<td>"; echo $row["pincode"]; echo "</td>";
echo "<td>"; echo $row["address"]; echo "</td>";
echo "<td>"; echo $row["landmark"]; echo "</td>";
echo "<td>"; echo $row["city"]; echo "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
</div>
</div>
</div>

PHP / JSON: How do I know if JSON data is empty or not?

Currently, I create a table that displays data from the database (JSON). In that table, I want to check is the data exists or not. Let's say if data is empty, I want to display "No booking data available at these moments".
The problem is, I don't know how to check the existing data. For now, if the data is empty, the table appears with <th> only and no words "No booking data available at these moments".
Below is my current code:
<?php
//retrieve json
$url = "http://172.20.0.45/TGWebService/TGWebService.asmx/displayAdminBookingDashboard?adminEmail=$Email";
$data = file_get_contents($url);
$json = json_decode($data);
if(empty($json)){
echo "<div class='card bg-light'>";
echo "<div class='card-body double' style='height: 400px;>";
echo "<h4 class='card-title'><i>No booking data available at this moments</i></h4>";
}else{
echo "<div class='card bg-light'>";
echo "<div class='card-body double' style='height: 400px; overflow-y: scroll;'>";
echo "<h4 class='card-title'>All Booking</h4>";
echo "<table>";
echo "<thead>";
echo "<tr>";
echo "<th>#</th>
<th>Requester</th>
<th>Factory</th>
<th>Room</th>
<th>Purpose</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody >";
foreach ($json->bookingList as $row) {
$status=$row->status;
if($status=="Approve"){
$color="color:green";
}else if($status=="Pending"){
$color="color:blue";
}else{
$color="color:red";
}
echo "<tr>";
echo "<td>" . $row->bookNo. "</td>";
echo "<td>" . $row->requestedBy. "</td>";
echo "<td>" . $row->facID. "</td>";
echo "<td>" . $row->roomName. "</td>";
echo "<td>" . $row->desc. "</td>";
echo "<td style='$color'><strong>" . $status ."</strong></td>";
echo "<td>";
echo "<a class='btn-view btn-primary btn-sm' href='../../view_booking/admin/view_booking_admin.php?Book_No=". $row->bookNo ."' data-toggle='tooltip'>View</a>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table><br>";
echo "</div>";
echo "<div>";
}
?>
Try the following code:
<?php
//retrieve json
$url = "http://172.20.0.45/TGWebService/TGWebService.asmx/displayAdminBookingDashboard?adminEmail=$Email";
$data = #file_get_contents($url);
$json = json_decode($data);
if($json === FALSE && empty($json)){
echo "<div class='card bg-light'>";
echo "<div class='card-body double' style='height: 400px;>";
echo "<h4 class='card-title'><i>No booking data available at this moments</i></h4>";
}else{
echo "<div class='card bg-light'>";
echo "<div class='card-body double' style='height: 400px; overflow-y: scroll;'>";
echo "<h4 class='card-title'>All Booking</h4>";
echo "<table>";
echo "<thead>";
echo "<tr>";
echo "<th>#</th>
<th>Requester</th>
<th>Factory</th>
<th>Room</th>
<th>Purpose</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody >";
foreach ($json->bookingList as $row) {
$status=$row->status;
if($status=="Approve"){
$color="color:green";
}else if($status=="Pending"){
$color="color:blue";
}else{
$color="color:red";
}
echo "<tr>";
echo "<td>" . $row->bookNo. "</td>";
echo "<td>" . $row->requestedBy. "</td>";
echo "<td>" . $row->facID. "</td>";
echo "<td>" . $row->roomName. "</td>";
echo "<td>" . $row->desc. "</td>";
echo "<td style='$color'><strong>" . $status ."</strong></td>";
echo "<td>";
echo "<a class='btn-view btn-primary btn-sm' href='../../view_booking/admin/view_booking_admin.php?Book_No=". $row->bookNo ."' data-toggle='tooltip'>View</a>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table><br>";
echo "</div>";
echo "<div>";
}
?>
I already solved my question. I just add "bookingList" at the empty json
<?php
//retrieve json
$url = "http://172.20.0.45/TGWebService/TGWebService.asmx/displayAdminBookingDashboard?adminEmail=$Email";
$data = file_get_contents($url);
$json = json_decode($data);
if(empty($json->bookingList)){
echo "<div class='card bg-light'>";
echo "<div class='card-body double' style='height: 400px;>";
echo "<h4 class='card-title'><i>No booking data available at this moments</i></h4>";
}else{
echo "<div class='card bg-light'>";
echo "<div class='card-body double' style='height: 400px; overflow-y: scroll;'>";
echo "<h4 class='card-title'>All Booking</h4>";
echo "<table>";
echo "<thead>";
echo "<tr>";
echo "<th>#</th>
<th>Requester</th>
<th>Factory</th>
<th>Room</th>
<th>Purpose</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody >";
foreach ($json->bookingList as $row) {
$status=$row->status;
if($status=="Approve"){
$color="color:green";
}else if($status=="Pending"){
$color="color:blue";
}else{
$color="color:red";
}
echo "<tr>";
echo "<td>" . $row->bookNo. "</td>";
echo "<td>" . $row->requestedBy. "</td>";
echo "<td>" . $row->facID. "</td>";
echo "<td>" . $row->roomName. "</td>";
echo "<td>" . $row->desc. "</td>";
echo "<td style='$color'><strong>" . $status ."</strong></td>";
echo "<td>";
echo "<a class='btn-view btn-primary btn-sm' href='../../view_booking/admin/view_booking_admin.php?Book_No=". $row->bookNo ."' data-toggle='tooltip'>View</a>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table><br>";
echo "</div>";
echo "<div>";
}
?>

Displaying information from a table then showing related information from another table

Hello I have 2 tables.
One is a table (nameinfo) that consists of contact information (account, name, email, address, city, state, zip, tags)
Table Two is an events tables (events) (email, event_type, event_name, date, keywords)
Here is what I am trying to do...
I would like to show a list of all the events that the person went to based on the matching email address. I would like to return the results like this...
nameinfo person 1
events person 1 went to
nameinfo person 2
events person 2 went to
nameinfo person 3
events person 3 went to
$query = mysql_query("SELECT * FROM nameinfo WHERE account='$a'");
while($row = mysql_fetch_array($query))
{
$i++;
echo "<div>";
echo "<table><tr style='font-weight:bold;color:green;font-size:9pt;text-align:left;'><td style='text-align:left;'
>";
echo "<button class='toggles' id='".$i."'/>"; echo $row['fullname']."</button>";
echo "</td>";
echo "<td >";
echo $row['target'];
echo "</td><td >";
echo $row['lname'];
echo "</td>";
echo "<td >";
echo $row['fname'];
echo "</td><td >";
echo $row['title'];
echo "</td>";
echo "<td >";
echo $row['company'];
echo "</td><td >";
echo $row['address'];
echo "</td>";
echo "<td >";
echo $row['address2'];
echo "</td><td>";
echo $row['city'];
echo "</td>";
echo "<td>";
echo $row['state'];
echo "</td><td>";
echo $row['zip'];
echo "</td>";
echo "<td>";
echo $row['email'];
echo "</td><td>";
echo $row['officenum'];
echo "</td>";
echo "<td>";
echo $row['cellnum'];
echo "</td>";
echo "<td>";
echo $row['date'];
echo "</td><td>";
echo $row['rep'];
echo "</td></tr></table></div>";
echo "<div style='width:100%;background:#000;color:#fff;display:none;' id='show-".$i."'>";
**echo THIS IS WHERE I WOULD LIKE TO PUT THE EVENT INFORMATION BUT HAVE NO IDEA HOW TO DO IT.;**
echo "</div>";
}
<script>
$("button.toggles").click(function() {
var value = $(this).attr('id');
$("#show-" + value).toggle();
});
</script>
Thank you for any assistance.
Run another query inside the loop. Here I am calling a function to do the job:
$i=0;
$link = ""\\your database connection
$query = mysql_query("SELECT * FROM nameinfo WHERE account='$a'");
while($row = mysql_fetch_array($query)){
$i++;
echo "<div>";
echo "<table><tr style='font-weight:bold;color:green;font-size:9pt;text-align:left;'><td style='text-align:left;'>";
echo "<button class='toggles' id='".$i."'/>"; echo $row['fullname']."</button>";
echo "</td>";
echo "<td >";
echo $row['target'];
echo "</td><td >";
echo $row['lname'];
echo "</td>";
echo "<td >";
echo $row['fname'];
echo "</td><td >";
echo $row['title'];
echo "</td>";
echo "<td >";
echo $row['company'];
echo "</td><td >";
echo $row['address'];
echo "</td>";
echo "<td >";
echo $row['address2'];
echo "</td><td>";
echo $row['city'];
echo "</td>";
echo "<td>";
echo $row['state'];
echo "</td><td>";
echo $row['zip'];
echo "</td>";
echo "<td>";
echo $row['email'];
echo "</td><td>";
echo $row['officenum'];
echo "</td>";
echo "<td>";
echo $row['cellnum'];
echo "</td>";
echo "<td>";
echo $row['date'];
echo "</td><td>";
echo $row['rep'];
echo "</td></tr></table></div>";
echo "<div style='width:100%;background:#000;color:#fff;display:none;' id='show-".$i."'>";
getEvent($row['email']);//Pass the email to the function here
echo "</div>";
}
function getEvent($email){
global $link;
$evant = "";
$query = mysql_query("SELECT * FROM events WHERE email='$email'", $link);
while($row = mysql_fetch_array($query)){
$event = $event . $row["event_name"] . "<br />";
//Format this however you wish to format
}
return $event;
}
<script>
$("button.toggles").click(function() {
var value = $(this).attr('id');
$("#show-" + value).toggle();
});
</script>

php mysql how to join two table

I have these codes:
<?php
$records = mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db('records', $records);
if(isset($_GET['src']))
{
$sql = "SELECT * FROM students where studentnumber like '%{$_GET['src']}%'";
$cnt = mysql_num_rows(mysql_query($sql));
if ($cnt == 0)
{
echo "<script>alert('No Record Found');</script>";
}
$result = mysql_query($sql, $records);
echo "<table border='0' class='table table-striped table-bordered table-hover'>";
echo "<tr class='info'><td width='10%'>Name</td><td width='11%'>Course Yr-Sec</td><td width='10%'>Student Number</td><td width='10%'>Violation</td><td width='10%'>Punishment</td><td width='9%'>Violation Date</td><td width='7%'>Punishment Date</td><td width='5%'>CS Length</td><td width='4%'>CS Done</td><td width='4%'>CS Left</td><td width='17%'><center>Action</center></td></tr></tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>";
echo $row['Lastname'];
echo ", ";
echo $row['Firstname'];
echo " ";
echo $row['Middleinitial'];
echo "</td>";
echo "<td>";
echo $row['Course'];
echo " ";
echo $row['Year'];
echo "-";
echo $row['Section'];
echo "</td>";
echo "<td>";
echo $row['Studentnumber'];
echo "</td>";
echo "<td>";
echo $row['Violation'];
echo "</td>";
echo "<td>";
echo $row['Punishment'];
echo "</td>";
echo "<td>";
echo $row['Violationdate'];
echo "</td>";
echo "<td>";
echo $row['Punishmentstartdate'];
echo "</td>";
echo "<td>";
echo $row['CSlength'];
echo "</td>";
echo "<td>";
echo $row['CSDone'];
echo "</td>";
echo "<td>";
echo $row['CSLeft'];
echo "</td>";
echo "<td>";
echo "<a href='edit.php?no={$row['ID']}'><input type='button' name='edit' value='Edit' class='btn btn-success'></a>";
echo " <a href='delete.php?no={$row['ID']}'><input type='button' name='delete' value='Delete' class='btn btn-danger'></a>";
echo " <input type='button' name='view' value='View' class='btn btn-info'>";echo "</td>";
echo "</tr>";
}
echo "</table>";
}
else
{
$sql = 'SELECT * FROM students';
$result = mysql_query($sql, $records);
echo "<table border='0' class='table table-striped table-bordered table-hover'>";
echo "<tr class='info'><td width='10%'>Name</td><td width='11%'>Course Yr-Sec</td><td width='10%'>Student Number</td><td width='10%'>Violation</td><td width='10%'>Punishment</td><td width='9%'>Violation Date</td><td width='7%'>Punishment Date</td><td width='5%'>CS Length</td><td width='4%'>CS Done</td><td width='4%'>CS Left</td><td width='17%'><center>Action</center></td></tr></tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>";
echo $row['Lastname'];
echo ", ";
echo $row['Firstname'];
echo " ";
echo $row['Middleinitial'];
echo "</td>";
echo "<td>";
echo $row['Course'];
echo " ";
echo $row['Year'];
echo "-";
echo $row['Section'];
echo "</td>";
echo "<td>";
echo $row['Studentnumber'];
echo "</td>";
echo "<td>";
echo $row['Violation'];
echo "</td>";
echo "<td>";
echo $row['Punishment'];
echo "</td>";
echo "<td>";
echo $row['Violationdate'];
echo "</td>";
echo "<td>";
echo $row['Punishmentstartdate'];
echo "</td>";
echo "<td>";
echo $row['CSlength'];
echo "</td>";
echo "<td>";
echo $row['CSDone'];
echo "</td>";
echo "<td>";
echo $row['CSLeft'];
echo "</td>";
echo "<td>";
echo "<a href='edit.php?no={$row['ID']}'><input type='button' name='edit' value='Edit' class='btn btn-success'></a>";
echo " <a href='delete.php?no={$row['ID']}'><input type='button' name='delete' value='Delete' class='btn btn-danger'></a>";
echo " <input type='button' name='view' value='View' class='btn btn-info'>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
It contains search, edit, delete and view functions...now my question is...I wanted to join the two tables in the database by the column studentnumber...
my table students contains the column Lastname, Firstname, Middleinitial, Course, Year, Section, Studentnumber, Violation, Punishment, Violationdate, Punishmentstartdate, CSlength, ID, CSDone, CSLeft...now my another table named students2 contains the following rows ID, Studentnumber, Violation, Punishment, Violationdate, Punishmentstartdate, CSlength, CSDone, CSLeft...I want to display the information from my both tables...for example I want to view all the records from database with a studentnumber of 20101000...do I have to inner join the tables?
I'm just a newbie in php...
Thank you in advance... :)
This is a LEFT JOIN. It will return all of the records from students1, as well as any records from students2 where the record has the same studentnumber as a record in students1:
SELECT * FROM students1
LEFT JOIN students2
ON students1.studentnumber = students2.studentnumber
AND students1.studentnumber = 20101000
An INNER JOIN returns only records that produce a match, so you will only get records where there is an identical studentnumber in both students1 and students2. Based on your comments, I believe this is the style you are looking for:
SELECT * FROM students1
INNER JOIN students2
ON students1.studentnumber = students2.studentnumber
AND students1.studentnumber = 20101000
If you want to get your head around using JOINs, I'd recommend trying both of these statements to observe the results. Then try a few other approaches, perhaps using this excellent tutorial on the Coding Horror Blog.

Categories