i’m currently trying to fetch values from two tables using join queries that has distinct values. eg)table one’s id name is update_id and table two’s id is g_id. my question is how to write it in such a way that the for each loop prints the exact variables with out failing to print the table 1 variables and table 2 variables when table 2 or table 1 variable is fetched . so, far i’m writing like this ($update_id || $g_id) to print either $update_id or $g_id in html tags eg)$more="<button class='btn btn-link more' data-id='".($updateid||$postid)."'>...More</button>";
but it isn’t working do you guys have any better methods. i’ll provide the full code if you want to but got to warn its lengthy.
What does the query look like, i.e. what field(s) are these tables joined with?
Can this common field “ID” be used as the button key to grab data from both tables?
Using your button format it might look like this.
$more = '<button class="btn btn-link more" data-id="'.$common_id.'">...More</button>';
Now if you are working with a unique ID say from the first table, e.g. ‘update_id’ that you join the seconded table with, this common ID can be used as an array data KEY so all query row results will be identied by this common ID array KEY. Here is a mysqli sample of how this array is built.
$data = array();
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$data[$row['update_id']] = $row;
}
You mentioned table 1 variables and table 2 variables so I guess you are showing records from each table seperatly with their own buttons with some type of “show more”???
Anyway, here’s a little sample of what I am talking about. Note that I am using the same data to loop through both display types and because of this I am adding “_one” or “_two” along with the common ID to Hide/Show the table in each foreach loop.
<?php
//SAMPLE query results for demo only.
$data = array(
'1' => array('update_id' => 1, 'table_one_name' => 'Name1','g_id' => 10, 'tbl_one_id' => 1, 'table_two_name' => 'Table2 Name1'),
'2' => array('update_id' => 2, 'table_one_name' => 'Name2','g_id' => 20, 'tbl_one_id' => 2, 'table_two_name' => 'Table2 Name2'),
'3' => array('update_id' => 3, 'table_one_name' => 'Name3','g_id' => 30, 'tbl_one_id' => 3, 'table_two_name' => 'Table2 Name3'),
'4' => array('update_id' => 4, 'table_one_name' => 'Name4','g_id' => 40, 'tbl_one_id' => 4, 'table_two_name' => 'Table2 Name4'),
'5' => array('update_id' => 5, 'table_one_name' => 'Name5','g_id' => 50, 'tbl_one_id' => 5, 'table_two_name' => 'Table2 Name5'),
'6' => array('update_id' => 6, 'table_one_name' => 'Name6','g_id' => 60, 'tbl_one_id' => 6, 'table_two_name' => 'Table2 Name6'),
'7' => array('update_id' => 7, 'table_one_name' => 'Name7','g_id' => 70, 'tbl_one_id' => 7, 'table_two_name' => 'Table2 Name7')
);
/*
//mysqli sample of how this array is built.
$data = array();
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$data[$row['update_id']] = $row;
}
*/
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="col-lg-6 col-md-6 col-sm-6">
<h2>TABLE ONE</h2>
<?php
foreach($data as $common_id => $ar):
echo $ar['table_one_name'].' <button type="button" class="btn btn-link more" data-toggle="collapse" data-target="#see_more_one'.$common_id.'">...More</button>
<div id="see_more_one'.$common_id.'" class="collapse">
<table class="table table-bordered">
<tr>
<th>Update ID</th>
<th>Table One Name</th>
<th>G ID</th>
<th>Table One Name</th>
</tr>
<tr>
<td>'.$ar['update_id'].'</td>
<td>'.$ar['table_one_name'].'</td>
<td>'.$ar['g_id'].'</td>
<td>'.$ar['table_two_name'].'</td>
</tr>
</table>
</div>
<div style="clear:both"></div>';
endforeach;
?>
</div>
<div class="col-lg-6 col-md-6 col-sm-6">
<h2>TABLE TWO</h2>
<?php
foreach($data as $common_id => $ar):
echo $ar['table_two_name'].' <button type="button" class="btn btn-link more" data-toggle="collapse" data-target="#see_more_two'.$common_id.'">...More</button>
<div id="see_more_two'.$common_id.'" class="collapse">
<table class="table table-bordered">
<tr>
<th>Update ID</th>
<th>Table One Name</th>
<th>G ID</th>
<th>Table One Name</th>
</tr>
<tr>
<td>'.$ar['update_id'].'</td>
<td>'.$ar['table_one_name'].'</td>
<td>'.$ar['g_id'].'</td>
<td>'.$ar['table_two_name'].'</td>
</tr>
</table>
</div>
<div style="clear:both"></div>';
endforeach;
?>
</div>
</div>
</body>
</html>
I suspect that the answer is going to be a refactoring of the query, but from the OP’s description and attempted solution, I would assume it’s an outer join/union query such that update_id xor g_id is true.
Something to the tune of
"string".((is_null($ar['update_id'])) ? $ar['g_id'] : $ar['update_id'])."anotherstring"
Is what my brain is doing at the moment.
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.