Running to Selects and grouping results with PHP - php

I am trying to display a row of data from my main table (parents details) and then under each of those rows display the children's names from another table. I have had a go using the following code:
$sql="select distinct members_main.first_name as main_firstname,
members_main.first_name as main_firstname,
members_main.last_name as main_lastname,
members_main.address_1 as main_address_1,
members_main.address_2 as main_address_2,
members_main.address_3 as main_address_3,
members_main.address_4 as main_address_4,
members_main.post_code as main_post_code,
members_main.home_tel as main_home_tel,
members_main.mobile as main_mobile,
members_main.home_email as main_home_email
from members_main, members_family where members_main.contact_id=members_family.contact_id";
$sql2="select members_family.first_name as fam_firstname,
members_family.last_name as fam_lastname
from members_family, members_main
where members_main.contact_id=members_family.contact_id";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
echo '<table width="100%" cellpadding="3" cellspacing="0">
<tr>
<th align="left" valign="top">First Name</th>
<th align="left" valign="top">Last Name</th>
<th align="left" valign="top">Address 1</th>
<th align="left" valign="top">Address 2</th>
<th align="left" valign="top">Address 3</th>
<th align="left" valign="top">Address 4</th>
<th align="left" valign="top">Post Code</th>
<th align="left" valign="top">Home Tel</th>
<th align="left" valign="top">Mobile</th>
<th align="left" valign="top">Email</th>
</tr>';
echo '<tr>
<td valign="top">'.$rows['main_firstname'].'</td>
<td valign="top">'.$rows['main_lastname'].'</td>
<td valign="top">'.$rows['main_address_1'].'</td>
<td valign="top">'.$rows['main_address_2'].'</td>
<td valign="top">'.$rows['main_address_3'].'</td>
<td valign="top">'.$rows['main_address_4'].'</td>
<td valign="top">'.$rows['main_post_code'].'</td>
<td valign="top">'.$rows['main_home_tel'].'</td>
<td valign="top">'.$rows['main_mobile'].'</td>
<td valign="top">'.$rows['main_home_email'].'</td>
</tr>';
$result2=mysql_query($sql2);
echo '<table width="100%" cellpadding="3" cellspacing="0">
<tr>
<th align="left" valign="top">First Name</th>
<th align="left" valign="top">Last Name</th>
</tr>';
while($rows=mysql_fetch_array($result2)){
echo '<tr>
<td valign="top">'.$rows['fam_firstname'].'</td>
<td valign="top">'.$rows['fam_lastname'].'</td>
</tr>';
}
}
echo "</table>";
I am getting all rows from the members_family table per each record from the members_main. There can be more than one record in the members_family which will be associated to one record in the members_main. Bascially, members_main (parent) can have multiple children in the members_main.

I think you just need to retrieve your members_main.contact_id from first table to use later in second while loop. Then you will have to move your second query inside first query while loop so the code will look like that:
$sql="select distinct
members_main.contact_id as ID,
members_main.first_name as main_firstname,
members_main.first_name as main_firstname,
members_main.last_name as main_lastname,
members_main.address_1 as main_address_1,
members_main.address_2 as main_address_2,
members_main.address_3 as main_address_3,
members_main.address_4 as main_address_4,
members_main.post_code as main_post_code,
members_main.home_tel as main_home_tel,
members_main.mobile as main_mobile,
members_main.home_email as main_home_email
from members_main, members_family where members_main.contact_id=members_family.contact_id";
$result = mysql_query($sql);
while($rows=mysql_fetch_array($result)) {
//script goes on with table printing untill
<td valign="top">'.$rows['main_home_email'].'</td>
</tr>';
Now it's time for the query
$sql2="select members_family.first_name as fam_firstname,
members_family.last_name as fam_lastname
from members_family, members_main
where members_main.contact_id=members_family.contact_id and members_main.contact_id = '".$rows['ID']."'";
As you see i added in WHERE condition to limit result for specified ID wich i retrieved from first query. Now you can print the rest of the table. Oh and by the way you closing table should look like that
echo "</table></td></tr></table>";
This is because you opened two tables.
Then I would like you to remember that mysql_* functions are deprecated so i would advise you to switch to mysqli or PDO

I feel some confusion with your second query. I am thinking that you have to construct your second query with the contact_id which is coming from first query(members_main). I assumes in that way and i have coded below. May be it will help you
<?php
$sql = "select distinct members_main.first_name as main_firstname,
members_main.first_name as main_firstname,
members_main.last_name as main_lastname,
members_main.address_1 as main_address_1,
members_main.address_2 as main_address_2,
members_main.address_3 as main_address_3,
members_main.address_4 as main_address_4,
members_main.post_code as main_post_code,
members_main.home_tel as main_home_tel,
members_main.mobile as main_mobile,
members_main.home_email as main_home_email
from members_main, members_family where members_main.contact_id=members_family.contact_id";
$result = mysql_query($sql);
?>
<table width="100%" cellpadding="3" cellspacing="0">
<tr>
<th align="left" valign="top">First Name</th>
<th align="left" valign="top">Last Name</th>
<th align="left" valign="top">Address 1</th>
<th align="left" valign="top">Address 2</th>
<th align="left" valign="top">Address 3</th>
<th align="left" valign="top">Address 4</th>
<th align="left" valign="top">Post Code</th>
<th align="left" valign="top">Home Tel</th>
<th align="left" valign="top">Mobile</th>
<th align="left" valign="top">Email</th>
</tr>
<?php
while ($rows = mysql_fetch_array($result))
{
$conId = $rows['contact_id']; //Which is comming from first Query
$sql2 = "select members_family.first_name as fam_firstname,
members_family.last_name as fam_lastname
from members_family, members_main
where members_family.contact_id=$conId";
$result2 = mysql_query($sql2);
?>
<tr>
<td valign="top"><?= $rows['main_firstname']; ?></td>
<td valign="top"><?= $rows['main_lastname']; ?></td>
<td valign="top"><?= $rows['main_address_1']; ?></td>
<td valign="top"><?= $rows['main_address_2']; ?></td>
<td valign="top"><?= $rows['main_address_3']; ?></td>
<td valign="top"><?= $rows['main_address_4']; ?></td>
<td valign="top"><?= $rows['main_post_code']; ?></td>
<td valign="top"><?= $rows['main_home_tel']; ?></td>
<td valign="top"><?= $rows['main_mobile']; ?></td>
<td valign="top"><?= $rows['main_home_email']; ?></td>
</tr>
<tr>
<td colspan="10">
<table width="100%" cellpadding="3" cellspacing="0">
<tr>
<th align="left" valign="top">First Name</th>
<th align="left" valign="top">Last Name</th>
</tr>
<?php
while ($rowsFamily = mysql_fetch_array($result2))
{
?>
<tr>
<td valign="top"><?= $rowsFamily['fam_firstname']; ?></td>
<td valign="top"><?= $rowsFamily['fam_lastname']; ?></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
<?php
}
?>
</table>

Related

php while loop table repeating fix

I have following do while loop be it keep on repeating the table for each record how to fix it? full table repeat for each record.
Also for every row fetch can I add
<table width="1510" border="1" align="center" cellpadding="2" cellspacing="2" class="table table-bordered" >
<tbody>
<form name="f1">
<? $counter = 1;
$total_marks = 0;
$total_obtain = 0;
while($row_rsDept = mysql_fetch_array($rsDept))
{
if($counter === 1)
{
?>
<tr>
<th width="71" rowspan="3" align="center" valign="middle" scope="col"><h4>Sr. No</h4></th>
<th width="229" align="right" valign="top" scope="col"> </th>
<th colspan="8" align="center" valign="middle" scope="col"><h3>Detail of Marks</h3></th>
</tr>
<tr>
<th rowspan="2" align="left" scope="row"><h4>Subject</h4></th>
<td colspan="2" align="center"><h4>Thoery</h4></td>
<td colspan="2" align="center"><h4>Practical</h4></td>
<td width="134" rowspan="2" align="center" valign="middle"><h4>Marks Obtain</h4></td>
<td width="144" rowspan="2" align="center" valign="middle"><h4>Max Marks</h4></td>
<td width="137" rowspan="2" align="center" valign="middle"><h4>Remarks</h4></td>
<td width="137" rowspan="2" align="center" valign="middle"><h4>Grace Marks</h4></td>
</tr>
<tr>
<td width="134">Obtained Marks</td>
<td width="144">Total Marks</td>
<td width="143">Obtained Marks</td>
<td width="153">Total Marks</td>
</tr>
<? } ?>
<tr>
<th align="left" scope="row"> </th>
<th align="left" scope="row"><?php echo $row_rsDept['SUBJECTS']; ?></th>
<td><?php echo $row_rsDept['THEORYOBTAINEDMARKS']; ?></td>
<td><?php echo $row_rsDept['THEORYTOTALMARKS']; ?></td>
<td><?php echo $row_rsDept['PRACTICALOBATINEDMARKS']; ?></td>
<td><?php echo $row_rsDept['PRACTICALTOTALMARKS']; ?></td>
<td align="center" valign="middle"><?php echo $row_rsDept['MARKSOBTAINED']; ?></td>
<td align="center" valign="middle"><?php echo $row_rsDept['MAXMARKS']; ?></td>
<td align="center" valign="middle"><span class="style8"><?php echo $row_rsDept['REMARKS']; ?></span></td>
<td align="center" valign="middle"><?php echo $row_rsDept['GRACEMARKS']; ?></td>
</tr>
<tr>
<th colspan="6" align="right" valign="middle" scope="row"><h4>Total</h4></th>
<td align="center" valign="middle"><span class="style8"><?php echo $row_rsDept['TOTALMARKS']; ?></span></td>
<td align="center" valign="middle">=total of above ( )</td>
<td align="center" valign="middle"> </td>
<td align="center" valign="middle"> </td>
</tr>
<?php
} ?>
<tr>
<th colspan="10" align="left" valign="middle" scope="row"> </th>
</tr>
<tr>
<th colspan="8" align="left" valign="middle" scope="row"> </th>
<th align="left" valign="middle" scope="row"> </th>
<th align="left" valign="middle" scope="row"> </th>
</tr>
<tr>
<th colspan="8" align="left" valign="middle" scope="row"><br></th>
<th align="left" valign="middle" scope="row"> </th>
<th align="left" valign="middle" scope="row"> </th>
</tr>
</form>
</table>
I tried to fix but not working for me
Inside loop you need to increment counter variable
Inside loop add this line
$counter++;

Multiple Checkbox Update and Delete Query

I want to update tutor table and tutor course table, i have multiple checkbox for tutor course and if a teacher update his course or other info both table should also update.... tutor can increase or decrease course. I'm using following query but its not working for me
$update_content = mysql_query("UPDATE wp_tutor
JOIN wp_tutor_courses
ON wp_tutor.tutor_id=wp_tutor_courses.tutor_id
SET wp_tutor.tutor_name='$tNameVar',
wp_tutor.tutor_qualification='$tQualVar',
wp_tutor.tutor_skype='$tSkyVar',
wp_tutor.tutor_specialization='$tuSAreaVar',
wp_tutor.tutor_gender='$tuGenderVar',
wp_tutor_courses.course_id='$tuCourseVar'
WHERE tutor_id='$tid'");
<table cellpadding="5" cellspacing="0" border="1">
<caption>Tutor Table</caption>
<thead>
<tr>
<th>Tutor ID</th>
<th>Tutor Name</th>
<th>Tutor Skype ID</th>
<th>Tutor Specialization</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">1</td>
<td align="center">Kashif</td>
<td align="center">kashiflatif</td>
<td align="center">Financial Accounting</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">Ammar</td>
<td align="center">ammar.90</td>
<td align="center">Research Methods</td>
</tr>
<tr>
<td align="center">3</td>
<td align="center">Bilal</td>
<td align="center">bilalhaider95</td>
<td align="center">Islamic Bond & Practices</td>
</tr>
</tbody>
</table>
<br />
<br />
<table cellpadding="5" cellspacing="0" border="1" style="float:left">
<caption>Tutor Course Table</caption>
<thead>
<tr>
<th>Tutor Course ID</th>
<th>Tutor ID</th>
<th>Course ID</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">1</td>
<td align="center">1</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">1</td>
<td align="center">2</td>
</tr>
<tr>
<td align="center">3</td>
<td align="center">1</td>
<td align="center">3</td>
</tr>
<tr>
<td align="center">4</td>
<td align="center">2</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">5</td>
<td align="center">2</td>
<td align="center">3</td>
</tr>
<tr>
<td align="center">6</td>
<td align="center">3</td>
<td align="center">1</td>
</tr>
</tbody>
</table>
<table cellpadding="5" cellspacing="0" border="1" style="float:left; margin-left:20px;">
<caption>Course Table</caption>
<thead>
<tr>
<th>Course ID</th>
<th>Course Name</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">1</td>
<td align="center">Certificate</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">Diploma</td>
</tr>
<tr>
<td align="center">3</td>
<td align="center">PGD</td>
</tr>
</tbody>
</table>
Try this:
$update_content = mysql_query("UPDATE wp_tutor a
INNER JOIN wp_tutor_courses b
ON a.tutor_id=b.tutor_id
SET a.tutor_name='$tNameVar',
a.tutor_qualification='$tQualVar',
a.tutor_skype='$tSkyVar',
a.tutor_specialization='$tuSAreaVar',
a.tutor_gender='$tuGenderVar',
b.course_id='$tuCourseVar'
WHERE a.tutor_id='$tid'");

How to create an html link to another page from "<?=$objResult["id"];?>"

I'm working on a php/mysql application and need to make the output of one column, one row an html link to another html page. Have not been able to find any relavant information. The "" needs to be the link. Thanks for any help.
<table id="display" style="width:800px;">
<tr>
<th width="40">ID</th>
<th width="70">Last</th>
<th width="70">First</th>
<th width="10">Middle</th>
<th width="70">Birth</th>
<th width="70">Death</th>
<th width="170">Notes</th>
<th width="100">Cemetery</th>
</tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
<tr>
<td style='text-align:center;'><?=$objResult["id"];?></td>
<td style='text-align:center;'><?=$objResult["last"];?></td>
<td style='text-align:center;'><?=$objResult["first"];?></td>
<td style='text-align:center;'><?=$objResult["middle"];?></td>
<td style='text-align:center;'><?=$objResult["birth"];?></td>
<td style='text-align:center;'><?=$objResult["death"];?></td>
<td><?=$objResult["notes"];?></td>
<td style='text-align:center;'><?=$objResult["cemetery"];?></td>
</tr>
<?
}
?>
</table>

display text with if/else statement if value not found in mysql table?

I'm trying to find a way of displaying an else or if statement in this code so that if the record/value doesn't exist in the mysql table then it will echo a piece of text like 'ask me'.
heres the code:
<?php
$rates_set = get_rates();
while ($rates = mysql_fetch_array($rates_set)) {
?>
<table width="110%" border="0" cellspacing="5" cellpadding="5">
<tr>
<th align="left" valign="middle" bgcolor="#EBEBEB" scope="col">Rates</th>
<th align="center" valign="middle" bgcolor="#EBEBEB" scope="col">money in</th>
<th align="center" valign="middle" bgcolor="#EBEBEB" scope="col">money Out</th>
</tr>
<tr>
<th align="left" valign="middle" bgcolor="#EBEBEB" scope="row">cost</th>
<td align="center" valign="middle">£<?php echo "{$rates['labour']} "; ?></td>
<td align="center" valign="middle">£<?php echo "{$rates['material']}"; ?></td>
</tr>
<tr>
<th align="left" valign="middle" bgcolor="#EBEBEB" scope="row">cost/th>
<td align="center" valign="middle">£<?php echo "{$rates['money']}"; ?></td>
<td align="center" valign="middle">£<?php echo "{$rates['expense']}"; ?></td>
</tr>
<tr>
<th align="left" valign="middle" bgcolor="#EBEBEB" scope="row">Overnight</th>
<td align="center" valign="middle">£<?php echo "{$rates['charges']}"; ?></td>
<td align="center" valign="middle">£<?php echo "{$rates['fees']}"; ?></td>
</tr>
</table>
<?php } ?>
hope someone can help. thank you.
Try this,
<?php echo isset($rates['fees'])?"{$rates['fees']}":"ask me"; ?>
You can loop through the values in the $rates array and if empty, assign a default:
$rates_set = get_rates();
while ($rates = mysql_fetch_array($rates_set)) {
// Set defaults
foreach($rates as $key => $value)
{
if(strlen(trim($value)) == 0)
{
// Assign default
$rates[$key] = 'Ask Me';
}
}
?>
There is some tweaking to be done with this method, such as how you determine a value is "empty"...but essentially, this will get the job done.
The other option is to handle it locally within the markup using the ternary operator:
<td align="center" valign="middle">£
<?php isset($rates['money']) ? $rates['money'] : 'Ask Me'; ?>
</td>
<?php
/* Use This */
$rates_set = get_rates();
if (mysql_num_rows($rates_set)>0) {
while ($rates = mysql_fetch_array($rates_set)) {
?>
<table width="110%" border="0" cellspacing="5" cellpadding="5">
<tr>
<th align="left" valign="middle" bgcolor="#EBEBEB" scope="col">Rates</th>
<th align="center" valign="middle" bgcolor="#EBEBEB" scope="col">money in</th>
<th align="center" valign="middle" bgcolor="#EBEBEB" scope="col">money Out</th>
</tr>
<tr>
<th align="left" valign="middle" bgcolor="#EBEBEB" scope="row">cost</th>
<td align="center" valign="middle">£<?php echo "{$rates['labour']} "; ?></td>
<td align="center" valign="middle">£<?php echo "{$rates['material']}"; ?></td>
</tr>
<tr>
<th align="left" valign="middle" bgcolor="#EBEBEB" scope="row">cost/th>
<td align="center" valign="middle">£<?php echo "{$rates['money']}"; ?></td>
<td align="center" valign="middle">£<?php echo "{$rates['expense']}"; ?></td>
</tr>
<tr>
<th align="left" valign="middle" bgcolor="#EBEBEB" scope="row">Overnight</th>
<td align="center" valign="middle">£<?php echo "{$rates['charges']}"; ?></td>
<td align="center" valign="middle">£<?php echo "{$rates['fees']}"; ?></td>
</tr>
</table>
<?php }
} else {
echo 'Ask me';
}
?>
You can use this
<?php
$rates_set = get_rates();
if(mysql_num_rows($rates_set)<1) {
echo "Ask me";
}else{
while ($rates = mysql_fetch_array($rates_set)) {
//your code
}
}
<?php echo ( (!empty($rates['labour'])) ? $rates['labour'] : 'Default Text'); ?>
Is that what you are looking for?
Edit:
Alter the top of the loop like this:
while ($rates = mysql_fetch_array($rates_set)) {
if (empty($rates_set)) continue;

Data in table not displaying the way I want it to

I have a spinner and what happens is that whatever number is in the spinner, when the form is submitted, it should display the word "quest" as many times as the number in the spinner.. E.g if number in spinner is 3, then it will display "quest" 3 times in the table.
The problem is displaying it in the table.
At the moment with my current code it is displaying it like this:
quest
quest
quest
Question Id, Option Type, Duration .... These are table headings
It is displaying the words quest outside the table
Instead I want the word "quest" to be displayed in the Question Id column like this:
Question Id, Option Type, Duration...
quest
quest
quest
How can I get it to display it like the example above?
Below is code
<table border=1 id="qandatbl" align="center">
<tr>
<th class="col1">Question No</th>
<th class="col2">Option Type</th>
<th class="col1">Duration</th>
<th class="col2">Weight(%)</th>
<th class="col1">Answer</th>
<th class="col2">Video</th>
<th class="col1">Audio</th>
<th class="col2">Image</th>
</tr>
<?php
$spinnerCount = $_POST['txtQuestion'];
if($spinnerCount > 0) {
for($i = 1; $i <= $spinnerCount; $i++) {
echo "<tr>quest";
}
}
?>
<td class='qid'></td>
<td class="options"></td>
<td class="duration"></td>
<td class="weight"></td>
<td class="answer"></td>
<td class="video"></td>
<td class="audio"></td>
<td class="image"></td>
</tr>
</table>
I did try echo "<td class='qid'></td>"; but this completely failed as well
Try this:
<table border=1 id="qandatbl" align="center">
<tr>
<th class="col1">Question No</th>
<th class="col2">Option Type</th>
<th class="col1">Duration</th>
<th class="col2">Weight(%)</th>
<th class="col1">Answer</th>
<th class="col2">Video</th>
<th class="col1">Audio</th>
<th class="col2">Image</th>
</tr>
<?php
$spinnerCount = $_POST['txtQuestion'];
if($spinnerCount > 0) {
for($i = 1; $i <= $spinnerCount; $i++) {
?>
<tr>
<td class='qid'><?php echo $quest; ?></td>
<td class="options"></td>
<td class="duration"></td>
<td class="weight"></td>
<td class="answer"></td>
<td class="video"></td>
<td class="audio"></td>
<td class="image"></td>
</tr>
<?php
} // For
} // If
?>
</table>
Is this what you want to do? Display "quest" in the first column?
<table border=1 id="qandatbl" align="center">
<tr>
<th class="col1">Question No</th>
<th class="col2">Option Type</th>
<th class="col1">Duration</th>
<th class="col2">Weight(%)</th>
<th class="col1">Answer</th>
<th class="col2">Video</th>
<th class="col1">Audio</th>
<th class="col2">Image</th>
</tr>
<?php
$spinnerCount = $_POST['txtQuestion'];
if($spinnerCount > 0) {
for($i = 1; $i <= $spinnerCount; $i++) { ?>
<tr>
<td class='qid'>quest</td>
<td class="options"></td>
<td class="duration"></td>
<td class="weight"></td>
<td class="answer"></td>
<td class="video"></td>
<td class="audio"></td>
<td class="image"></td>
</tr>
<?php
}
}
?></table>
?>

Categories