Merging data into one column mysql using php - php

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

Related

PHP MySQL join two tables query

I have two tables ie abstract table and author with one to many relation, for each iteration on while loop I want to display a html table of unique row of data from abstract table with corresponding rows from author table.
This what I did:
public function getAll() {
try {
$sql = " SELECT tbl_abstract.abstract_id, tbl_abstract.first_name,
tbl_abstract.last_name,tbl_abstract.content,
tbl_author.afirst_name, tbl_author.alast_name,
tbl_author.aaffilition
FROM tbl_abstract
INNER JOIN tbl_author ON tbl_abstract.abstract_id = tbl_author.abstract_id
GROUP BY tbl_abstract.abstract_id";
$stmt= $this->pdo->prepare($sql);
$stmt->execute();
$count = $stmt ->rowCount();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
?>
<table class="table" >
<tr>
<td align="center" >
//data from tbl_abstract
<?php echo $row["abstract_id"]; ?>. <?php print($row["abstract_title"]); ?>
<?php echo $row["first_name"].' '.$row["last_name"]; ?>,
//data from tbl_author
<?php echo $row["afirst_name"].' '.$row["alast_name"];?>
</td>
</tr>
<tr>
<td align="center" ">
//data from tbl_abstract
<?php print($row["content"]); ?>
</td>
</tr>
</table>
<?php
}
}catch(PDOException $e){
echo $e->getMessage();
return false;
}
}
There are three records from tbl_author associated with the abstract_id from tbl_abstract but i only get one record instead of 3 of them.Please help
Try this one :-
<?php
$sql = "SELECT *
FROM tbl_abstract
where abstract_id IN (SELECT distinct abstract_id
FROM tbl_abstract)";
$stmt= $this->pdo->prepare($sql);
$stmt->execute();
$count = $stmt ->rowCount();
?>
<table class="table" >
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
?>
<tr>
<td align="center" >
//data from tbl_abstract
<?php echo $row["abstract_id"]; ?> <?php print($row["abstract_title"]); ?>
<?php echo $row["first_name"].' '.$row["last_name"]; ?>,
<?php
$sql1 = "SELECT *
FROM tbl_author
WHERE abstract_id = '".$row["abstract_id"]."'" ;
$stmt1= $this->pdo->prepare($sql1);
$stmt1->execute();
while($row1 = $stmt1->fetch(PDO::FETCH_ASSOC)){
//data from tbl_author
echo $row1["afirst_name"].' '.$row1["alast_name"];
} ?>
</td>
<td align="center">
//data from tbl_abstract
<?php print($row["content"]); ?>
</td>
</tr>
<?php } ?>
</table>
Try removing the GROUP BY clause like this:
SELECT
tbl_abstract.abstract_id, tbl_abstract.first_name,
tbl_abstract.last_name,tbl_abstract.content, tbl_author.afirst_name,
tbl_author.alast_name, tbl_author.aaffilition
FROM
tbl_abstract
INNER JOIN
tbl_author ON tbl_abstract.abstract_id = tbl_author.abstract_id
Group by is grouping all the authors by the field abstract_id, which means, that it won't return all the authors, but only one for one abstract_id (based on the sorting field, which in this case is probably the primary key, because it is not explicitly defined)..

while loop in mysql inner join

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
}

how to fetch colums data from two table using mysql with php using where

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

Generate an id in the td of a row, from the Mysql DB

I have a table where I need to fill an ID in a td using the value retrieved from a Mysql Table. I tried like this:
<?php
$ww = db_query("SELECT id, item1 FROM table1 WHERE table1.id = '".$record->main_data."'" );
?>
<td id="id_field1" class="<?php echo $ww->id;?>" >
<?php
foreach($ww as $ee)
{ echo $ee->item1;}
?>
</td>
I know the use of:
<td id="id_field1" class="<?php echo $ww->id;?>" >
is wrong, but I am not getting any other idea can you help me to generate an id for this td from the id retrieved from the Query?
EDIT
After the use of:
$ww = db_query("SELECT id, item1, FROM table1 WHERE table1.id = '" . $record->main_data . "'");
foreach ($ww as $row) {
?>
<td id="id_field<?php echo $row->id; ?>" class="<?php echo $row->id; ?>" >
<?php echo $row->item1; ?>
</td>
<?php
}
I am getting two issues--
1 ) Getting a warning from the DATATABLES plugin for fixedheader which says
Requesetd unknown parameter '11' for row 0
2 ) For empty td , the next <td> gets moved , instead of first <td> and I am getting the second <td> values of class and id inside the first... In this case, the table row is shifted to left with empty in the last column td
EDIT2 : this worked with no issues
I tried like this - is it ok to do like this: please comment on it-
<?php $ww = db_query("SELECT id FROM table1 WHERE table1.id = '".$record->main_data."'" );?>
<td id="id_field1" class="cart_wonid<?php foreach($ww as $ee) { echo $ee->id;}?>">
<?php
$ww = db_query("SELECT item1 FROM table1 WHERE table1.id = '".$record->main_data."'" );
foreach($ww as $ee){
echo $ee->main_data;
}
?>
</td>
EDIT
OP using drupal.
Put your td into the loop.
$ww = db_query("SELECT id, item1, FROM table1 WHERE table1.id = '" . $record->main_data . "'");
foreach ($ww as $row) {
?>
<td id="id_field<?php echo $row->id; ?>" class="<?php echo $row->id; ?>" >
<?php echo $row->item1; ?>
</td>
<?php
}

Display information from associated table?

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

Categories