Showing Data with 2 tables on php mysql - php

I have a problem with showing data from 2 tables
Table 1 (kegiatan): id_kgt (PK), nama_kgt,ket, tg_mulai, tg_akhir, nm_pengirim, indi01, indi02, indi03, indi04, indi05, indi06, indi07, indi08, indi09, indi010,
Table 2 (pilihan): kdpilih (PK), nmpilih
kdpilih contain number which linked with indi01 to indi010
nmpilih contain plain text, ex: kdpilih: 1 = nmpilih: car
I already create the script, but the problem is when someone entry the data less or more than 3 field (indi01, indi02, indi03) from the entry page it will show nothing. Is there any solution to fix this? Is my join tables script wrong? So, i want showing all the data although the data on entry page only 1, 2, 3 or etc.
This is my script, any help wil so helpfull. Thanks
<?php
$sql = "
SELECT a.id_kgt
, a.nama_kgt
, a.ket
, a.tg_mulai
, a.tg_akhir
, a.nm_pengirim
, a.file
, b.nmpilih AS indi01
, c.nmpilih AS indi02
, d.nmpilih as indi03
FROM kegiatan a
JOIN pilihan b
ON b.kdpilih = a.indi01
JOIN pilihan c
ON c.kdpilih = a.indi02
JOIN pilihan d
ON d.kdpilih = a.indi03
ORDER
BY a.id_kgt ASC
";
$result = mysqli_query($conn, $sql);
$no = 1;
if (mysqli_num_rows($result) > 0)
{
while ($data = mysqli_fetch_array($result))
{
echo "<tr>
<td>".$data['id_kgt']."</a></td>
<td>".$data['nama_kgt']."</a></td>
<td>".$data['ket']."</td>
<td>".tglindo($data['tg_mulai'])."</td>
<td>".tglindo($data['tg_akhir'])."</td>
<td>".$data['indi01'].", ".$data['indi02'].", ".$data['indi03']."</td>
<td>".$data['nm_pengirim']."</td>
<td>
<a href='".$data['file']."'>Download</a>
<a href='kegiatan_ubah.php?id_file=$data[id_kgt]'>Ubah</a>
<a href='kegiatan_hapus.php?id_file=$data[id_kgt]'>Hapus</a>
</td>
</tr>";
$no++;
}
}
else
{
echo "No data.";
}

If you want to show the data on the other tables do not do a join, you may consider left join instead.
Cheers

SOLVED
i use this magic:
"SELECT a.id_kgt, a.nama_kgt, a.ket, a.tg_mulai, a.tg_akhir, a.nm_pengirim, a.file,
b.nmpilih as indi01, c.nmpilih as indi02, d.nmpilih as indi03, e.nmpilih as indi04, f.nmpilih as indi05,
g.nmpilih as indi06, h.nmpilih as indi07, i.nmpilih as indi08, j.nmpilih as indi09, k.nmpilih as indi010
FROM kegiatan a
LEFT JOIN pilihan b ON b.kdpilih = a.indi01 LEFT JOIN pilihan c ON c.kdpilih = a.indi02
LEFT JOIN pilihan d ON d.kdpilih = a.indi03 LEFT JOIN pilihan e ON e.kdpilih = a.indi04
LEFT JOIN pilihan f ON f.kdpilih = a.indi05 LEFT JOIN pilihan g ON g.kdpilih = a.indi06
LEFT JOIN pilihan h ON h.kdpilih = a.indi07 LEFT JOIN pilihan i ON i.kdpilih = a.indi08
LEFT JOIN pilihan j ON j.kdpilih = a.indi09 LEFT JOIN pilihan k ON k.kdpilih = a.indi010
ORDER BY a.id_kgt ASC"

Related

how to not get duplicate data in php mysql

I have a mysql query in which i am fetching details from two tables now i want data of users whose requests are pending and from other data centers i want the pin codes that they have so i made this query which is working fine but it is giving me duplicate data for name and center too. this is my query
$sql = "
SELECT u.username
, u.district
, u.requests
, c.username
, c.id
FROM users u
JOIN center c
ON u.username = c.username
where u.requests = 'pending'
";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result)>0) {
while ($row = mysqli_fetch_assoc($result)) {
$name = $row['username'];
$district = $row['district'];
$center = $row['id'];?>
<tr><td><?php echo $row['username']?></td><td><?php echo $row['district']?></td><td><?php echo $row['id']?></td></tr>
<?php
}
}
?>
The following provides a text representation of current output for better understanding.
name center pin code
dhruv mumbai 5545454
dhruv mumbai 2618165165
I want the $center variable in commas and don't want it to repeat the name and district variable, so something like this:
In MySQL you can use GROUP_CONCAT to get a comma separated list of values. Change your query to
$sql = "
SELECT u.username
, u.district
, u.requests
, GROUP_CONCAT(c.id) AS id
FROM users u
JOIN center c
ON u.username = c.username
where u.requests = 'pending'
GROUP BY u.username
, u.district
, u.requests";
Note I have removed c.username from the SELECT list as it is the same value as u.username (because of the JOIN condition) and since it has the same column name it will overwrite u.username in the output data.

Select - Rows for Columns possible?

i have this select:
SELECT
a.`cod_oportunidade`,
b.nome nome_cliente,
c.descricao estado,
d.descricao cidade,
e.nome nome_funcionario_criou,
f.nome nome_funcionario_resp,
i.`descricao`,
h.`valor`
FROM
oportunidades_clientes a
LEFT OUTER JOIN empresas_clientes b
ON b.cod_cliente = a.cod_cliente
LEFT OUTER JOIN sistema_estados c
ON c.cod_estado = b.cod_estado
LEFT OUTER JOIN sistema_cidades d
ON d.cod_cidade = b.cod_cidade
LEFT OUTER JOIN empresas_funcionario e
ON e.cod_funcionario = a.cod_funcionario_criou
LEFT OUTER JOIN empresas_funcionario f
ON f.cod_funcionario = a.cod_funcionario_resp
JOIN formulario_valor h
ON h.`cod_oportunidade` = a.`cod_oportunidade`
JOIN formulario_campo i
ON i.`cod_campo` = h.`cod_campo`
WHERE 1 = 1
AND a.`cod_oportunidade` = 3
The result is therefore:
My question is... Need to inves need to stay several lines that the result is only one line. The data in the column "descricao" must be as columns ...
The implode() function will separate an array (columns from a database) with whatever character you prefer, in your case a pipe:
$mysqli = mysqli_connect("host","user","pass","db");
$result = mysqli_query($mysqli, "SELECT ...");
while ($row = mysqli_fetch_array($result)) {
echo implode(" | ", $row) . '<br />';
}

How can I join 3 tables in SQL

Here is the query I created :
$query = "SELECT address_details.company, address_details.po_box,
address_details.town, letter_details.address_id,
letter_details.attn, letter_details.create_date,
letter_details.ref_no, letter_details.refference,
letter_details.letter_body, letter_details.print_date
FROM letter_details
join address_details ON address_details.id = letter_details.address_id
join signatories ON (letter_details.id = signatories.id)
WHERE letter_details.id='" .$_GET['id'] . "'";
Try using this query.
$query = "SELECT ad.company, ad.po_box,
ad.town, ld.address_id,
ld.attn, ld.create_date,
ld.ref_no, ld.refference,
ld.letter_body, ld.print_date
FROM letter_details ld
left join address_details ad ON (ad.id = ld.address_id)
left join signatories s ON (ld.id = s.id)
WHERE ld.id='" .$_GET['id'] . "'";
First, you should alias for better readibility,
check if you get something in $_GET[id]
echo $_GET[id];
then assign it, (or you may set it as well, if you're using a class)
$fltr = $_GET[id];
And, try to use LEFT JOIN ;
SELECT b.company, b.po_box,
b.town, a.address_id,
a.attn, a.create_date,
a.ref_no, a.refference,
a.letter_body, a.print_date
FROM letter_details AS a LEFT JOIN address_details AS b ON b.id = a.address_id
LEFT JOIN signatories AS c ON (a.id =c.id)
WHERE a.id='$fltr'

PHP while loop returning only one row when SUM added to multiple JOIN query

I have a query like this:
$query = "SELECT a.sender_id,
a.recipient_id,
a.form_id, due_date,
a.completed,
f.name,
p.priority,
u.first_name,
u.last_name,
SUM(a.completed) as completed_sum
FROM form_assignments a
JOIN forms f ON (form_id = f.id)
JOIN users u ON (sender_id = u.id)
JOIN priorities p ON (priority_id = p.id)
WHERE recipient_id = '{$_SESSION['user_id']}'
ORDER BY due_date ASC";
And a while loop like this:
$assignment_count = (mysqli_num_rows($result));
$assignments_row = array();
while ($row = mysqli_fetch_array($result)) {
$sender = $row['first_name'] . ' ' . $row['last_name'];
$form_id = $row['form_id'];
$form_name = $row['name'];
$priority = $row['priority'];
$due_date = date('m/d/Y', strtotime($row['due_date']));
$completed = $row['completed'];
$not_done = $assignment_count - $row['completed_sum'];
}
And it's only returning one row. It seems my SUM(a.completed) as completed_sum is causing the issues because the query worked fine before I added it, but I want to add up all the values in completed to use in my $not_done variable.
Can anyone help clarify what I'm doing wrong?
When you use an aggregate function like SUM, all the results will be aggregated into one row unless you use a GROUP BY clause to segregate them. But it looks to me like you don't need a SUM in the first place. Your loop is subtracting this value from a total, so you just need the value from each row -- when you subtract them all you'll have subtracted the total. So just select a.completed rather than SUM(a.completed).
For $not_done, you need to initialize it before the loop:
$not_done = $assignment_count;
Then during the loop you should do a running subtraction:
$not_done -= $row['completed'];
Try this query :
SELECT
a.sender_id,
a.recipient_id,
a.form_id,
a.due_date,
a.completed,
f.name,
p.priority,
u.first_name,
u.last_name,
b.completed as completed_sum
FROM
form_assignments AS a
LEFT JOIN (
SELECT form_id,SUM(completed) FROM form_assignments GROUP BY form_id
) AS b ON (a.form_id = b.form_id)
LEFT JOIN forms AS f ON (form_id = f.id)
LEFT JOIN users AS u ON (sender_id = u.id)
LEFT JOIN priorities AS p ON (priority_id = p.id)
WHERE
recipient_id = '{$_SESSION['user_id']}'
ORDER BY due_date ASC

Set variable from alias in SQL query

I have a simple JOIN query:
$lstCheck = $dbWHMCS->query('SELECT * FROM tblmonitorports mp
INNER JOIN tblmonitorhosts h ON h.hst_id = port_mon
INNER JOIN tblhosting ho ON ho.id = h.hst_serverid
INNER JOIN tblclients cl ON cl.id = ho.userid');
while ($data = $lstCheck->fetch())
{
$serveridx = $data['ho.id'];
$clientid = $data['cl.id'];
}
My problem is that I have an "id" column in both the tblhosting and tblclients tables, so my variables both have the same id. I tried to set it using an alias in the example above (ho. and cl.) but it doesn't work. How can I do this in the example above?
Thank you!
How about this? (a bit rusty on php details, but this should do it):
$lstCheck = $dbWHMCS->query('SELECT ho.id hid, cl.id cid FROM tblmonitorports mp
INNER JOIN tblmonitorhosts h ON h.hst_id = port_mon
INNER JOIN tblhosting ho ON ho.id = h.hst_serverid
INNER JOIN tblclients cl ON cl.id = ho.userid');
while ($data = $lstCheck->fetch())
{
$serveridx = $data['hid'];
$clientid = $data['cid'];
}
You are selecting the records, with a wild card *, so you can't select the fields like that.
As per your query h.hst_serverid & ho.userid have the exact same value as you want. SO simply do this
while ($data = $lstCheck->fetch())
{
$serveridx = $data['hst_serverid'];
$clientid = $data['userid'];
}
However, selecting specific rows might sound better too
$lstCheck = $dbWHMCS->query('SELECT ho.id hid, cl.id cid, ....');
while ($data = $lstCheck->fetch())
{
$serveridx = $data['hid'];
$clientid = $data['cid'];
}

Categories