How to fetch the values using inner join - php

I have two tables, from that i need to fetch each row value, and print it like below.
<table id="contact" class="display">
<thead>
<tr>
<td>Name</td>
<td>Business Email</td>
<td>Mobile No</td>
<td>Company Name</td>
<td>Message</td>
</tr>
</thead>
<tr>
<td><?php echo $result['0']; ?></td>
<td><?php echo $result['1']; ?></td>
<td><?php echo $result['2']; ?></td>
<td><?php echo $result['3']; ?></td>
<td><?php echo $result['4']; ?></td>
</tr><?php
}
?>
</table>
I have tried
SELECT GROUP_CONCAT(field_value)
FROM `table2` GROUP BY `submit_time`
But it will make problem in explode step.
If comma separator came in message field, explode function increase array value, i cant fetch the correct value and print it.
Kindly share the idea to fix this.

You should have a primary key or atleast an auto increment key on the first table in order to associate it with the second table. The plugin you are using might be using it by ordering it. So every table 2 entry has a table 1 entry ordered by timestamp. What you have to do is create a new table1 column called ID and make it auto increment, it wont effect the working of the plugin. Then you can use a custom query like this.
SELECT $wpdb->table1.*
FROM $wpdb->table1
INNER JOIN $wpdb->table2 ON ($wpdb->table1.ID = $wpdb->table2.ID)
WHERE $wpdb->table2.field_name = 'text-506'

Related

How to hide table rows that fetch the info from a database based on string given and when a button is pressed

An HTML table is given in which there are people (agents in this case) who have the following information displayed Id, Name, Rank, Location, and Status. Status can have 3 values. Online, Offline, Disconnected. Based on the status that one of the agents has I want to put 3 buttons as such Online, Offline, and Disconnected, and when a button is pressed to hide the other rows with different values. For example, if I press Online, table rows on the HTML side that contain the status Offline and Disconnected disappear, this goes for the others too the other way around. I have no idea to achieve what I said earlier and I am open to any solution that is deemed to resolve my problem.
<table id="main_table">
<tr>
<td>Id</td>
<td>Agent Name</td>
<td>Agent Rank</td>
<td>Agent Location</td>
<td>Status</td>
</tr>
<?php
require 'CMS/processing/conn.php';
$result = mysqli_query($con,"SELECT * FROM agents");
while($info = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $info['id']; ?></td>
<td><?php echo $info['agentName']; ?></td>
<td><?php echo $info['agentRank']; ?></td>
<td><?php echo $info['locationNames']; ?></td>
<td><?php echo $info['agentStatus']; ?></td>
</tr>
<?php
}
?>
</table>
Try using the below logic : Create 3 buttons with <a> and pass status value as GET to the same page then while fetching check whether it has GET or not
Note:This is not a answer :passing direct GET value to sql query may cause some security issues
Online//put the status value based on your status
Offline//put the status value based on your status
Disconnected//put the status value based on your status
<table id="main_table">
<tr>
<td>Id</td>
<td>Agent Name</td>
<td>Agent Rank</td>
<td>Agent Location</td>
<td>Status</td>
</tr>
<?php
require 'CMS/processing/conn.php';
if(isset($_GET['status']))
{
$result = mysqli_query($con,"SELECT * FROM agents where agentStatus='".$_GET['status']."'");
}
else
{
$result = mysqli_query($con,"SELECT * FROM agents");
}
while($info = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $info['id']; ?></td>
<td><?php echo $info['agentName']; ?></td>
<td><?php echo $info['agentRank']; ?></td>
<td><?php echo $info['locationNames']; ?></td>
<td><?php echo $info['agentStatus']; ?></td>
</tr>
<?php
}
?>
</table>
I'd use javascript within a tag or jQuery since this sounds like you need dynamic functionality on a static webpage.
For your button pass in the name of the function you're creating inside your tag
<button onclick="yourFunctionName(yourString)"></button>
Inside your tag have the function accept the yourString paramater
function yourFunctionName(yourString){
if(yourString === "whatever"){
$('tbody tr').hide()
$('tbody tr').show()
}
etc! Let me know if you need any more clarification on anything
Output echo an attribute on each row <tr data-status="online"> etc.
Then in script use document.querySelectorAll("tr[data-status='online'] to find all rows with that status, loop thru the array and toggle each row style.display to 'table-row' or 'none' as required.
You could also do the same using CSS by assigning a class <tr class="online"> etc. Then in script dynamically altering the CSS definition of that class, changing the display to 'table-row' or 'none' as required.

How do I echo an inner join column name

I have two tables that I have inner joined called milk and jan both has a column named id. trying to make a modal popup, but it wouldn't work bc of the ambigous id column name so i tried milk.id but still wouldn't work. is there a way around it?
this is my php query:
$sql="SELECT milk.pemohon, milk.nokp, milk.keterangan, jan.status
FROM milk
INNER JOIN jan ON milk.id = jan.users_id";
$result = mysqli_query($con,$sql);
?>
<table>
<thead>
<tr>
<th>Pemohon</th>
<th>No. KP</th>
<th>Keterangan</th>
<th>Status</th>
<th>Penghantaran</th>
</tr>
</thead>
<tbody>
<?php
while($row=mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["pemohon"]; ?></td>
<td><?php echo $row["nokp"]; ?></td>
<td><?php echo $row["keterangan"]; ?></td>
<td><?php echo $row["status"]; ?></td>
<td><button data-id='<?php echo $row['milk.id']; ?>' class="userinfo btn btn-success">Info</button></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
$sql="
SELECT
milk.id AS milk_id,
milk.pemohon,
milk.nokp,
milk.keterangan,
jan.id AS jan_id,
jan.status
FROM milk
INNER JOIN jan ON milk.id = jan.users_id
";
Now you can just select milk_id or jan_id in php using $row['milk_id'] or $row['jan_id']
To explain:
MySQL uses the column name as name, thus on milk.id it uses id.
If you have multiple select with the same column name, ex milk.id and jan.id it returns id and id(1) respectively.
Best thing is to use an alias with AS followed by the name. In this way, MySQL returns that as the name.

Click table row to load new table

So I have a table loaded on the my page, Is it possible to click on a row and have it load another table with the same ID from my database? For instance a table with customer orders is already loaded, when i click on one of the rows is it possible to have a second table load with the order record with the same id?
This is my code loading the first table. I was thinking maybe i could use a check box value? Or maybe some sort of jquery all though I dont know much about jquerys
I was able to find this code that prints the selected row to a message box but Im not sure how or even if it can do what ive described?
this is essentially what i want to do http://www.youtube.com/watch?v=cobidLA_KVU&feature=youtu.be
<table border="5",th,td, cellspacing="5", cellpadding="5", width="500", align="center">
<tr>
<th>TP ID</th>
<th>Permit Deny</th>
<th>Level</th>
<th>Session</th>
<th>Information Specialist</th>
</tr>
<?php foreach ($results as $row): ?>
<tr>
<td><?php echo $row->TP_ID ?></td>
<td><?php echo $row->Permit_or_Deny ?></td>
<td><?php echo $row->Level ?></td>
<td><?php echo $row->Session ?></td>
<td><?php echo $row->Information_specialist ?></td>
</tr>
<?php endforeach ?>
</table>
var i = 1;
$('td').each(function(){
$(this).addClass('col'+i);
i = i+1;
});
$('table').find("td:not(.col4)").on('click',function(){
alert($(this).text());
})
You can also use jQuery/Ajax. Make a $_GET from it and request the server for a new GET variable.

Pass the values between two tables

I develop a transaction function here. But I have a trouble with javascript. As you can see below, there are two table, where the first one is for display the item and the second table is for purchasing. What i want to do is, the row for an item in first table is clickable. When the row is clicked, the item that have been choose come out in the second table. And at the second table cell for discount can be editable and the total price is changing by the discount. Can anyone help me to pass the value between these two tables?
<fieldset>
<legend>Item Show</legend>
<table border="1">
<tr>
<td>Item Name</td>
<td>Item Code</td>
<td>Manufacturer</td>
<td>Price</td>
<td>Stock</td>
</tr>
<?php
while($row = mysql_fetch_array($result))
{
?>
<tr>
<td><?php echo $row['item_name']; ?></td>
<td><?php echo $row['item_code']; ?></td>
<td><?php echo $row['item_manufacturer']; ?></td>
<td><?php echo $row['sell_price']; ?></td>
<td><?php echo $row['stock']; ?></td>
</tr>
<?php
}
?>
</table>
</fieldset>
<fieldset>
<legend>Item Sale</legend>
<table border="1">
<tr>
<td>Item name</td>
<td>Item Code</td>
<td>Stock</td>
<td>Price</td>
<td>Quantity</td>
<td>Discount</td>
<td>Total Price</td>
</tr>
</table>
</fieldset>
Here are the steps that you should follow.
give a common class to all your like
use jQuery events like
$(".clickable").on('click',function(e,this){
//a.use "this" here to extract values that you want to pass to other table
//b.append <tr><td></td>....</tr> with this.values to your table
})
If you can do the above - - things will go fine

SELECTING in two tables...

I have two tables that I want to use for viewing my reports which I can get after inputting a date.
Here are my tables: for customers - customer_date, lastname, firstname
for services - room_number, date_in, date_out
Here is my code now : it seems that it can't get any rows from my table
<?php
$conn = mysql_connect("localhost","root","");
mysql_select_db('irm',$conn);
if(isset($_GET['Submit'])){
$customer_date = $_GET['customer_date'];
}
?>
<form method="get">
<table width="252" border="0">
<tr>
<td width="98">Choose Date:</td>
<td width="144"><label>
<input onclick="ds_sh(this);" name="customer_date" id="customer_date" readonly="readonly" style="cursor: text" />
</label></td>
</tr>
<tr>
<td align="right"><input type="submit" value="Submit" /></a></td>
<td></td>
</tr>
</table>
</form>
<form>
<?php
$tryshow = "SELECT * FROM customers,services WHERE customer_date = '$customer_date' ";
$result = #mysql_query($tryshow,$conn)
or die("cannot view error query");
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print...";
}
while($row=mysql_fetch_assoc($result)){
?>
<table width="700" border="0">
<tr>
<td width="100">Customer Date:</td>
<td width="100">Last Name</td>
<td width="100">First Name</td>
<td width="100">Room Number</td>
<td width="100">Date In</td>
<td width="100">Date Out</td>
</tr>
<tr>
<td><?php echo $row["customer_date"]; ?></td>
<td><?php echo $row['lastname']; ?></td>
<td><?php echo $row['firstname']; ?></td>
<td><?php echo $row['room_number']; ?></td>
<td><?php echo $row['date_in']; ?></td>
<td><?php echo $row['date_out']; ?></td>
</tr>
</table>
<?php }?>
</form>
With this I can get a report of any customer who checks in on that date.
I need some advice. Hope you can answer me soon.
You don't appear to have any fields in common between the two tables. How do you store fact that customer A was in room B on date C? To do an SQL join, the tables being joined have to have at least one field in common.
As well, instead of just saying die("cannot view error query"), which is utterly useless for debugging purposes, try doing die(mysql_error(), which will give you the exact reason the query failed.
As well, if the query DOES work, then you're outputting an entire HTML table for each row found. You should have the table headers and footers data OUTSIDE of the fetch loop.
You need to relate the two tables with a JOIN. Based on the information given, customers.customer_date to services.date_in seems to be the most likely candidate. This assumes that the date columns hold only a date and not a date/time.
Also notice that I'm not using select * in my query and neither should you. ;-)
SELECT c.customer_date, c.lastname, c.firstname,
s.room_number, s.date_in, s.date_out
FROM customers c
INNER JOIN services s
ON c.customer_date = s.date_in
WHERE c.customer_date = '$customer_date'
When your making query to database make sure your date format is yyyy-mm-dd
Mysql understand date in this format only so that you have to compare the date format in this format only.
your $customer_date should be in the yyyy-mm-dd format
As an aside, I'd change customer_date to something more meaningful such as "date_in." (It's a good thing when the names are predictable!) You don't need to specify that it's the customer since it's in the customer table already.

Categories