I am running this statement which works fine....
$sql = "SELECT skill,SUM(quantity) as sum FROM skills
WHERE userid = $userid
GROUP BY skill";
My problem is the column skill stores the value from table agency_skills which has a column that contains the name of the skill.
I am trying to figure out how to display the name of the skill rather than the value stored in the skills table?
This is the entire code I am working with.....
<?php
defined( '_JEXEC' ) or die;
$db = JFactory::getDBO();
$user =& JFactory::getUser();
$userid = $user->get('id');
$sql = "SELECT skill,SUM(quantity) as sum FROM skills
WHERE userid = $userid
GROUP BY skill";
$db->setQuery($sql);
$rows = $db->loadObjectList();
?>
<style>
table, td, th
{
border-bottom:1px solid black;
}
{
th
background-color:#000000
color:#FFF
}
</style>
<table>
<tr>
<th width="195">Skill</th> <th width="195">Total Completed</th>
</tr>
</table>
<table>
<?PHP foreach ($rows as $row): ?>
<tr>
<td width="200"> <?php echo $row->skill?> </td>
<td width="190"> <?php echo $row->sum ?> </td>
</tr>
<?php endforeach ?>
</table>
You can join that table like this
SELECT as.skillname,
SUM(s.quantity) as sum
FROM agency_skills as `as`
left outer join skills as s on s.skill = as.id
WHERE s.userid = $userid
GROUP BY as.skillname
If you use a left outer join instead of the default inner join you get even skills that are not present in the skills table.
Simplified SQL Fiddle demo
Related
My code join 4 table using INNER JOIN. I can't customize loop while using inner join. How to control while clause so unwanted loop wouldn't happen.
My code output (student and school name is looping in every subject field)
I want to display like this where student and school loops at once.
MY PHP Code
<table id="customers">
<?php
$query=$con->prepare("SELECT subjectcomb.subjectid, subjectcomb.schoolid, student.student, school.school, subject.name
FROM ((( subjectcomb
INNER JOIN school ON subjectcomb.schoolid=school.id)
INNER JOIN student ON subjectcomb.schoolid=student.schoolID)
INNER JOIN subject ON subjectcomb.subjectid=subject.id)");
$query->execute();
while ($row = $query->fetch(PDO::FETCH_ASSOC)){?>
<tr>
<td>
<?php echo $row['school']."->".$row['student']?>
</td>
</tr>
<tr>
<td>
<?php echo $row['name']?>
<input type="text" name="mark"></input>
</td>
</tr>
<?php } ?>
</table>
Try adding an ORDER BY to your query:
$query=$con->prepare("SELECT subjectcomb.subjectid, subjectcomb.schoolid, student.student, school.school, subject.name
FROM ((( subjectcomb
INNER JOIN school ON subjectcomb.schoolid=school.id)
INNER JOIN student ON subjectcomb.schoolid=student.schoolID)
INNER JOIN subject ON subjectcomb.subjectid=subject.id)
ORDER BY school.school, student.student");
then in the while loop use this check:
<?php
$last_student = ""
while ($row = $query->fetch(PDO::FETCH_ASSOC)){
if ($last_student != $row['student']) {
?>
<tr>
<td>
<?php echo $row['school']."->".$row['student']?>
</td>
</tr>
<?php } ?>
<tr>
<td>
<?php echo $row['name']?>
<input type="text" name="mark"></input>
</td>
</tr>
<?php $last_student = $row['student'] } ?>
I think what Phil meant was something like the following. You keep track of the previous school and student. If the current school and student are the same as the previous, you skip printing the school/student row, but if they not the same, you print them and also update the previous school's and previous student's values.
$prev_school = '';
$prev_student = '';
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
if ($row['school'] != $prev_school && $row['student'] != $prev_student) { ?>
<tr>
<td>
<?php echo $row['school']."->".$row['student'] ?>
</td>
</tr>
<?php
$prev_school = $row['school'];
$prev_student = $row['student'];
}
?>
<tr>
<td>
<?php echo $row['name']?>
<input type="text" name="mark"></input>
</td>
</tr>
<?php
}
This question already has answers here:
Mysql Query with two tables php
(4 answers)
Closed 5 years ago.
So I have this simple search code
<div id="page">
<!-- start content -->
<?php
if(isset($_POST['search']))
{
$valueToSearch = $_POST['valueToSearch'];
// search in all table columns
// using concat mysql function
$query = "SELECT * FROM `pdspersonalinfo` WHERE CONCAT(`personid`, `surname`, `firstname`, `sex`) LIKE '%".$valueToSearch."%'";
$search_result = filterTable($query);
}
else {
$query = "SELECT * FROM `pdspersonalinfo`";
$search_result = filterTable($query);
}
// function to connect and execute the query
function filterTable($query)
{
$connect = mysqli_connect("localhost", "root", "", "ucwd");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP HTML TABLE DATA SEARCH</title>
<style>
table,tr,th,td
{
border: 1px solid black;
}
</style>
</head>
<body>
<form action="searchtrain[orig].php" method="post">
<input type="text" name="valueToSearch" placeholder="Value To Search" autocomplete="off"><br><br>
<input type="submit" name="search" value="Filter"><br><br>
<table bgcolor="#FFFFFF">
<tr>
<th>ID</th>
<th>Surame</th>
<th>First Name</th>
<th>Sex</th>
<th>Training</th>
</tr>
<!-- populate table from mysql database -->
<?php while($row = mysqli_fetch_array($search_result)):?>
<tr>
<td><?php echo $row['personid'];?></td>
<td><?php echo $row['surname'];?></td>
<td><?php echo $row['firstname'];?></td>
<td><?php echo $row['sex'];?></td>
<td><?php echo $row['traintitle'];?></td>
</tr>
<?php endwhile;?>
</table>
</form>
</body>
</html>
</div>
But I want my search_results to retrieve data from two tables having a common column
My first table is named pdspersonlinfo and has the following rows personid, position, day, month, year, surname, firstname, middlename, sex
My second table is named pdstrain and has the following rows personid, trainid, traintitle, trainfrom, trainto
When I search, for example the name Jacob, the data surname,firstname middlename - traintitle will be retrieved
I'm very much a beginner on php and mysql. I hope this can be comprehended.
I thank you in advance ^_^
You can join the tables in your select query
SELECT
I.surname, I.firstname, I.middlename, P.traintitle
FROM pdspersonlinfo I
LEFT JOIN pdstrain P ON P.personid = I.personid
WHERE CONCAT(I.personid, I.surname, I.firstname, I.sex) LIKE '%valueToSearch%'
Don't forget to properly escape the user inputted search value!
You need a join.
SELECT a.something, b.something
FROM tableA a JOIN tableB b
ON a.id = b.id
i have two tables one item table and customer table:
in the table you can see the second item item id 1002 have two entries.and i want to add colspan to that item for column 1 and 3.
<table>
<tr>
<th>Item ID</th>
<th>Item Color</th>
<th>Customer</th>
</tr>
<?php
$sql = mysqi_query($con,"select * from item_table");
while($row = mysqli_fetch_array($sql)){
?>
<tr>
<td><?=$row['item_id'];?></td>
<td><?=$row['item_color'];?></td>
<td>
<select>
<?php
$sql_cust = mysqli_query($con,"select * from customer_tbl");
while($row_cust = mysqli_fetch_array()){
if($row['customer_id'] == $row_cust['customer_id']){
echo "<option selected='selected' >".$row['customer_name']."</option>";
}else{
echo "<option>".$row['customer_name']."</option>";
}
<?php
}
?>
</select>
</td>
</tr>
<?php
}
?>
</table>
But it print in normal way, i have no idea how to add rowspan in loop..please suggest some logic to solve its appreciated.
You can try it this way:
First, add a counter to your query that will indicate how many entries has a given item.
$sql_cust = mysqli_query($con,
"SELECT *, (SELECT COUNT(*) FROM item_table as it
WHERE it.item_id = item_table.item_id) as c
FROM item_table");
Then, when looping through the items you will set the rowspan to the number of entries the item has. Below is the whole code adjusted.
<?php
$sql = mysqi_query($con,
"SELECT *, (SELECT COUNT(*) FROM item_table as it
WHERE it.item_id = item_table.item_id) as entry_count
FROM item_table");
$buffer = [];
while($row = mysqli_fetch_array($sql)){
if(!isset($buffer[$row[$item_id]])) {
$buffer[$row[$item_id]] = 1;
}
?>
<tr>
<?php if(!isset($buffer[$row[$item_id]])) {?>
<td rowspan="<?=$row['entry_count']?>"><?=$row['item_id'];?></td>
<?php }?>
<td><?=$row['item_color'];?></td>
<?php if(!isset($buffer[$row[$item_id]])) {?>
<td rowspan="<?=$row['entry_count']?>">
<select>
<?php
$sql_cust = mysqli_query($con,"select * from customer_tbl");
while($row_cust = mysqli_fetch_array()){
if($row['customer_id'] == $row_cust['customer_id']){
echo "<option selected='selected' >".$row['customer_name']."</option>";
}else{
echo "<option>".$row['customer_name']."</option>";
}
<?php
}
?>
</select>
</td>
<?php }?>
</tr>
<?php
}
?>
Note that I added a buffer where I set which item was already displayed. That array is used so you only open one td with the wanted rowspan, instead of doing it on every iteration.
i have a simple idea
give TD a ID like
<td id="dynamically-Generate"> (you need to verify that TD id need to be equal in .rowSpan ="here" inside script )
and set this TD if dynamically-Generate is greater than 1 then don't show
and again if dynamically-Generate is greater than 1 then then use this script
<script>
document.getElementById("dynamically-Generate").rowSpan = "dynamically-Generate";
</script>
use script inside loop and both dynamically-Generate need to be same inside everyloop and change after each loop
I have two Table ownership_profile and socity_unit.
Query For table1: select * from ownership_profile where SID='$id'
Query For Table2: select * from socity_unit where socity_id='$sid'
I have to join with one query, but i don't have idea how to do it.
This is my Php code but gives error:
<!-----------------Table For User Names-------------------------------------->
<table border="1" align="center">
<tr>
<th>Unit No</th>
<th>Member Name</th>
<th>Wing</th>
<th>Unit</th>
</tr>
<?php
if(isset($_GET['submit']))
{
$sql = "select * from ownership_profile o inner join society_unit s on o.sid = s.society_id where o.sid = '$sid' ";
$result = mysql_query($sql);
$i=1;
while($row=mysql_fetch_array($result)){
?>
<?php
$name = $row['NAME'];
$unitid = $row['UNIT_ID'];
$sid = $row['SID'];
$wings = $row['wings'];
$unit_no = $row['unit_no'];
{
?>
<!--User Submit Result-->
<tr>
<td><?php echo $unitid; ?></td>
<td><?php echo $name; ?></td>
<td><?php echo $wings; ?></td>
<td><?php echo $unit_no; ?></td>
</tr>
<?php }?>
<?php
//echo "<br>";
$i++;
}
}
?>
inner join is what you want.
select *
from ownership_profile o
inner join society_unit s
on o.sid = s.society_id
where o.sid = '$sid'
This assumes that 'sid' and 'society_id' are the relationship identifiers.
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;
select * from ownership_profile o inner join socity_unit s on o.SID = s.socity_id where o.SID = '$SOCIETY_ID'
This query working fine but...... its generate dublicate entry from table
good day i am building a website for recipes and im trying to display a recipe with multiple ingredients but the ingredients are in a separate table so when i run the query if i have three ingredients the recipe id will appear three times along with the other data because the ingredients column is populated by only one entry...is there anyway to merge these entries?
here is my code and link to an image of how the data is appearing..any help would be great
[img]http://forums.trinituner.com/upload/data/62/all recipes duplicate.jpg[/img]
my code
$db=mysql_select_db("tasteofhome");
$query = "select RECIPES.recipe_ID,RECIPES.recipe_name,RECIPES.recipe_author,RECIPES.cook_time,RECIPES.stages_description, INGREDIENTS.ingredients_name, USERS.user_name from
RECIPES,USERS,RECIPE_INGREDIENTS,INGREDIENTS where USERS.user_ID=RECIPES.user_ID AND
RECIPES.recipe_ID = RECIPE_INGREDIENTS.recipe_ID
AND RECIPE_INGREDIENTS.ingredients_ID = INGREDIENTS.ingredients_ID
";
$result = mysql_query ($query);
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}
while ($rows = mysql_fetch_array ($result))
{ //begin of while loop
?>
<tr>
<td> <?php echo $rows [0]; ?> </td>
<td> <?php echo $rows [1]; ?> </td>
<td> <?php echo $rows [2]; ?> </td>
<td> <?php echo $rows [3]; ?> </td>
<td> <?php echo $rows [4]; ?> </td>
<td> <?php echo $rows [5]; ?> </td>
<td> <?php echo $rows [6]; ?> </td>
</tr>
<?php
} //end of while loop
?>
You could join the recipes and users tables with an aggregate query that uses group_concat to join the ingrediants to a coma-delimited string:
SELECT r.recipe_ID,
r.recipe_name,
r.recipe_author,
r.cook_time,
r.stages_description,
gi.ingredients_names,
u.user_name
FROM RECIPES r
JOIN USERS u ON u.user_ID = r.user_ID
JOIN (SELECT ri.recipe_ID,
GROUP_CONCAT(i.ingredients_name) AS ingredients_names
FROM RECIPE_INGREDIENTS ri
JOIN INGREDIENTS i ON ri.ingredients_ID = i.ingredients_ID
GROUP BY recipe_ID) gi ON gi.recipe_ID = r.recipe_ID