How to change date into name - php

The code below works, but I want to change the months into names, such as "October", "November" and "December" using MONTHNAME. When I do, I get an error like the following:
JulyError in query: unknown column 'July' in 'where clause'.
What am I doing wrong?
<html>
<body>
<?php
$connection = mysql_connect('localhost','root','') or die ('unable to connect');
mysql_select_db("naurah")or die ("cannot select db");
$query = "SELECT YEAR(date) AS year, SUM(total) AS total FROM `order` GROUP BY year";
$result = mysql_query($query) or die ("Error in query:".mysql_error());
if (mysql_num_rows($result) > 0) {
while ($row=mysql_fetch_array($result)) {
echo $row[0];
$query1 = "SELECT * FROM `order` WHERE YEAR(date) = $row[0]";
$result1=mysql_query($query1) or die ("Error in query:".mysql_error());
if (mysql_num_rows($result1) > 0) {
echo '<table border="1" cellpadding="0" cellspacing="0" class="tg">
<strong>
<tr>
<th class="tg-031e">Order ID</th>
<th class="tg-031e" width="100">Date</th>
<th class="tg-031e">Name</th>
<th class="tg-031e">Email</th>
<th class="tg-031e" width="100">Telephone</th>
<th class="tg-031e">Address</th>
<th class="tg-031e">City</th>
<th class="tg-031e">Postcode</th>
<th class="tg-031e">State</th>
<th class="tg-031e">Reference No</th>
<th class="tg-031e">Total</th>
<th class="tg-031e">Order Status</th>
<th class="tg-031e">Postage</th>
<th class="tg-031e">Action</th>
</tr>
</strong>';
while ($row1 = mysql_fetch_array($result1)) {
echo "
<tr>
<td class=\"tg-4eph\" ><div align=\"center\"> $row1[0] </div></td>
<td class=\"tg-031e\" ><div align=\"center\"> $row1[1] </div></td>
<td class=\"tg-031e\" ><div align=\"center\">$row1[3] $row1[2]</div></td>
<td class=\"tg-4eph\" ><div align=\"center\"> $row1[4]</div></td>
<td class=\"tg-031e\" ><div align=\"center\"> $row1[5]</div></td>
<td class=\"tg-4eph\" ><div align=\"center\"> $row1[6]</div></td>
<td class=\"tg-031e\" ><div align=\"center\"> $row1[7]</div></td>
<td class=\"tg-4eph\" ><div align=\"center\"> $row1[8]</div></td>
<td class=\"tg-031e\"><div align=\"center\"> $row1[9]</div></td>
<td class=\"tg-031e\"><div align=\"center\"> $row1[10]</div></td>
<td class=\"tg-031e\"><div align=\"center\"> $row1[11]</div></td>
<td class=\"tg-031e\"><div align=\"center\"> $row1[12]</div></td>
<td class=\"tg-031e\"><div align=\"center\"> $row1[13]</div></td>
<td class=\"tg-4eph\" width=\"100\"><div align=\"center\">
<a href=\"adminupdatetrans.php?orderID= $row1[0]\">
<img src=\"../images/icn_edit_article.png\" title=\"Remove\"/>
</a>
<a href=\"admindeleteorder.php?orderID= $row1[0] \" onclick=\"return confirm('Are you sure want to delete ?')\">
<img src=\"../images/icn_trash.png\" title=\"Remove\"/>
</a>
</td>
</tr>
";
}
echo '
<tr>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>
<td >Total All:</td>
';
echo '<td>RM';
echo $row[1];
echo '</td></tr>';
echo '</table>';
}
}
}
?>
</body>
</html>

Assuming that the date is in a timestamp format :),
You can get the names of months thanks to this method :
date("F",mktime(0,0,0,date('m', $timestamp),1,2011));Or like this :
date('F', strtotime("2015-date('m', $timestamp)-01"));

Related

Create table with different PHP variables including if statement

I have multiple quires retrieving data from mysql database (multiple tables)
SELECT description FROM table1 As descr WHERE type='MYTYPE'; //this shows description of each product type
SELECT count(id) FROM table2 As tp1 WHERE ACTIVE='Y' AND type=1"); //this is to count records that has this product type =1
SELECT count(id) FROM table2 As tp2 WHERE ACTIVE='Y' AND type=2");
SELECT count(id) FROM table2 As tp3 WHERE ACTIVE='Y' AND type=3");
SELECT count(id) FROM table2 As tp4 WHERE ACTIVE='Y' AND type=4");
What I'm trying to have is a table of each type description, along with count of active products has this type: ignoring the count if the count is 0
Here is modest try, I know it isn't perfect, therefore seeking help here, perhaps different approach will be also helpful, but this doesn't work as it have defining variable which has if statement and also logic is not correct?
$tbl_header = '<tr>
<th width=220px align=left>Type description</th>
<th width=10px align=left>Active products of this type</th>
</tr>';
$tbl_data =
if (tp1>0) {
'<tr>
<td width=220px align=left><font color=#000000>'. {descr[0][0]}.'</td></font>
<td width=10px align=left><font color=#FF0000>.'$tp1.'</td></font>
</tr>';}
if (tp2>0) {
'<tr>
<td width=220px align=left><font color=#000000>'. {descr[1][0]}.'</td></font>
<td width=10px align=left><font color=#FF0000>.'$tp2.'</td></font>
</tr>';}
;
echo "<table> $tbl_header $tbl_data </table>";
Try this method. It should work and you don't have to worry about if statements being in the middle of variable
<table>
<tr>
<th width=220px align=left>Type description</th>
<th width=10px align=left>Active products of this type</th>
</tr>
<?php
if ($tp1 > 0) {
?>
<tr>
<td width=2 20 px align=l eft>
< font color=# 000000>
<?php echo descr[0][0]; ?>
</font>
</td>
<td width=10px align=left>
<font color=#FF0000>
<?php echo $tp1; ?>
</font>
</td>
</tr>
<?php
}
?>
<?php
if ($tp2 > 0) {
?>
<tr>
<td width=2 20 px align=l eft>
< font color=# 000000>
<?php echo descr[1][0]; ?>
</font>
</td>
<td width=10px align=left>
<font color=#FF0000>
<?php echo $tp2; ?>
</font>
</td>
</tr>
<?php
}
?>
</table>
How about this?
<?php
$table_head = "<tr>
<th width=220px align=left>Type description</th>
<th width=10px align=left>Active products of this type</th>
</tr>";
if ($tp1 > 0) {
$table_data_1 = "<tr>
<td width=2 20 px align=l eft>
< font color=# 000000>". descr[0][0] . "</font>
</td>
<td width=10px align=left>
<font color=#FF0000>". $tp1. "</font>
</td>
</tr>";
}
if ($tp2 > 0) {
$table_data_2 = "<tr>
<td width=2 20 px align=l eft>
< font color=# 000000>". descr[1][0] . "</font>
</td>
<td width=10px align=left>
<font color=#FF0000>". $tp2. "</font>
</td>
</tr>";
}
echo "<table> $table_ head $table_data_1 $table_data_2 </table>";
?>
<?php
$table_data = "";
$table_head = "<tr>
<th width=220px align=left>Type description</th>
<th width=10px align=left>Active products of this type</th>
</tr>";
if ($tp1 > 0) {
$table_data = "<tr>
<td width=2 20 px align=l eft>
< font color=# 000000>". descr[0][0] . "</font>
</td>
<td width=10px align=left>
<font color=#FF0000>". $tp1. "</font>
</td>
</tr>";
}
if ($tp2 > 0) {
$table_data .= "<tr>
<td width=2 20 px align=l eft>
< font color=# 000000>". descr[1][0] . "</font>
</td>
<td width=10px align=left>
<font color=#FF0000>". $tp2. "</font>
</td>
</tr>";
}
echo "<table> $table_ head $table_data </table>";
?>

How to add value to existing value using MySQL?

hi dear friends yesterday I asked about a teachers rating system but did not get any idea please help me. i am making a teacher rating system where i want to rate every teacher by making text boxes in front of the name of each teacher .the number that the user will add in text box for a specific teacher is to be added to a current rate of teacher.but the code is not working properly.when i add values through text boxes it adds the value from the last text box to all rows.for example for the first teacher i want to add 2 ,for the second i want to add 4 and for the 3rd teacher i want to rate 3. so instead of 2 and 3 all the rating of all teachers are incremented by 3.how to solve the problem?please help i am giving code of 2 pages m.php and n.php
m.php
<form action="n.php" method="post" enctype="multipart/form-data">
<table width="642" height="215" border="10" align="left" cellspacing="0" >
<tr>
<th class="style5">Teacher ID</th>
<th width="90" class="style5">Teacher Name</th>
<th width="127" class="style5">Teacher Registration</th>
<th width="135" class="style5">Teacher Qualification</th>
<th width="92" class="style5">Teacher Subject</th>
<th width="92" class="style5">Action</th>
</tr>
<?php
include 'conn.php';
$sql = "SELECT * FROM teacher ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_array()){
$id=$row['tid'];
?>
<tr>
<td height="50" align="center" class="style5"><?php echo $row['tid'];?></td>
<td align="center" class="style5"><?php echo $row['tname'];?></td>
<td align="center" class="style5"><?php echo $row['treg'];?></td>
<td align="center" class="style5"><?php echo $row['qualification'];?></td>
<td align="center" class="style5"><?php echo $row['subject'];?></td>
<td align="center"> <input type="text" name="rating">
</td>
</tr>
<?php
}
}else{
echo "<center><p><font size=10/> No Records</p></center>";
}
$conn->close();
?><tr><td colspan="6">
<input type="submit" name="submit" value="Enter"></td></tr>
</table>
</form>
n.php
<?php
mysql_connect("localhost","root","") or die ("couldnt connnect to server");
mysql_select_db("project") or die ("couldnt connnect to database");
include 'conn.php';
if(isset($_POST['submit']))
{
$sql = "SELECT * FROM teacher";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_array()){
$id=$row['tid'];
$newvalue=$_POST['rating'];
$sql_update="UPDATE teacher set hits = hits +$newvalue where tid=".$id."";
mysql_query($sql_update) or die(mysql_error());
header("location:m.php");
}
}
}
Like this ... give rating name array with id from table and update the record with the right value and id ... but never do this way for username and password case ...
<form action="n.php" method="post" enctype="multipart/form-data">
<table width="642" height="215" border="10" align="left" cellspacing="0" >
<tr>
<th class="style5">Teacher ID</th>
<th width="90" class="style5">Teacher Name</th>
<th width="127" class="style5">Teacher Registration</th>
<th width="135" class="style5">Teacher Qualification</th>
<th width="92" class="style5">Teacher Subject</th>
<th width="92" class="style5">Action</th>
</tr>
<?php
include 'conn.php';
$sql = "SELECT * FROM teacher ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_array()){
$id=$row['tid'];
?>
<tr>
<td height="50" align="center" class="style5"><?php echo $row['tid'];?></td>
<td align="center" class="style5"><?php echo $row['tname'];?></td>
<td align="center" class="style5"><?php echo $row['treg'];?></td>
<td align="center" class="style5"><?php echo $row['qualification'];?></td>
<td align="center" class="style5"><?php echo $row['subject'];?></td>
<td align="center"> <input type="text" name="rating[<?php echo $id; ?>]">
</td>
</tr>
<?php
}
}else{
echo "<center><p><font size=10/> No Records</p></center>";
}
$conn->close();
?><tr><td colspan="6">
<input type="submit" name="submit" value="Enter"></td></tr>
</table>
</form>
<?php
mysql_connect("localhost","root","") or die ("couldnt connnect to server");
mysql_select_db("project") or die ("couldnt connnect to database");
include 'conn.php';
if(isset($_POST['submit']))
{
$sql = "SELECT * FROM teacher";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_array()){
$id=$row['tid'];
$newvalue=$_POST['rating'];
if(isset($newvalue[$id])) {
$new_temp_value = $newvalue[$id];
$sql_update="UPDATE teacher set hits = hits +$new_temp_value where tid=".$id."";
mysql_query($sql_update) or die(mysql_error());
}
}
}
header("location:m.php");
}

php and mysql. view topics date

I can final exams assignment of lecturers to create a website forum.
and now there is a problem in my coding.
please fix.
when creating a topic / thread, and we opened the topic / threadnya, there is a date that tell us when the topic / thread made.
but in my coding, showing all existing date in the database.
<?php
session_start();
if($_SESSION['logged'] == true)
{
if(isset($_SESSION['username']))
{ echo $username=$_SESSION["username"];
}else
{
header('Location:login.php');
}
}
?>
<!DOCTYPE html>
<html>
<head>
<!==CSS>
<style>
#clock{
pointer-events: none
}
</style>
<!close css>
<img src="head1.jpg" alt="icon" width="100%" height="110">
<img src="line1.jpg" alt="icon" width="100%" height="20">
<title>Venray</title>
<body background="alienbackground.jpg">
<font face="comic sans MS">
<!==Table Login==>
<table border="0" style="width:99%" bgcolor="#424242" align="center">
<tr>
<table border="0" style="width:98%" bgcolor="#424242" align="center">
<tr>
<td> <img src="start.gif" alt="icon" width="26" height="26"> </td>
<td rowspan="2" align="right">
<table border="0" bgcolor="#2E2E2E">
<tr>
<td><b>
<?php
{
echo "<tr><td><b><font color=green> Welcome, ";
echo $username;
echo '<span>, [Log Out]</span></li>';
echo "</tr></td></b></font>";
}
?>
</b></td>
</tr>
<tr>
<td id=clock align="right"><b><iframe src="http://free.timeanddate.com/clock/i4eh41xm/n108/tlsg/fn7/fs12/tct/pct/ftb/tt0/tw1/tm1/th1" frameborder="0" width="182" height="20" allowTransparency="true"></iframe>
</b></td>
</tr>
</table>
</td>
</tr>
<tr>
<td valign="top"><b><font size="4">Community</font></b></td>
</tr>
</table>
</tr>
<p style="color:#ff0000">
<!==Table please welcome==>
<table border="0" style="width:98%" bgcolor="#424242" align="center">
<tr>
<td align="right">
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
require("conn.php");
$con = mysql_connect($server,$user,$pass);
$db = mysql_select_db($db);
$sql = "select * from d_admin order by username";
//echo $sql;
$ambil_data = mysql_query($sql);
while($data = mysql_fetch_array($ambil_data))
$result = mysql_query("SELECT * FROM d_admin");
$rows = mysql_num_rows($result);
echo "Member " . $rows . " * ";
?>
Post 1 * Topics 1 </td>
</tr>
</table>
<br>
<table border="1" width="98%" height="6%" align="center" bgcolor=#424242 >
<tr>
<td align="left" ><img src="hometopic.gif" width="40" height="40"> </td>
</tr>
<tr>
<table border="1" width="98%" height="40" align="center" bgcolor="#86B404" bordercolor=green>
<tr>
<th width="70%" align="left"><img src="open.png" width="40" height="40">Posting Display</th>
<td align="right"><img src="replay.png" width="150" height="40"></td>
</tr>
</tr>
<tr>
<table border="1" width="98%" height="70" align="center" bgcolor=#424242>
***<?php
error_reporting(E_ALL ^ E_DEPRECATED);
$con = mysql_connect($server,$user,$pass);
$db = mysql_select_db($db);
$sql = "select * from tabel_topik ";
$ambil_data = mysql_query($sql);
while($data = mysql_fetch_array($ambil_data))
{
echo '<tr><th align="left" valign="top" colspan="2" >'.$data['date'].'</th></tr>';
echo '</tr>';
}
?>***
<tr><th align="center" width="15%">Venray</th><td align="left" width="85%">title</td></tr>
<tr><th align="top" width="15%">join dates</th><td align="left" rowspan="2" valign="top">awd</td></tr>
<tr><th align="center" valign="top" width="15%" height="400">9 Post</th></tr>
<tr>
<td align="left">*</td>
<td align="right" width="85%"><img src="replay.png" width="150" height="40"></td></tr>
</table>
</tr>
</table>
</body>
</font>
</head>
</html>
Get forum specific datas by passing the ID or any similar field in the query for starters. Then if you are looking to format the way in which the date is displayed, you could use the php date function like this:
while($data = mysql_fetch_array($ambil_data))
{
echo '<tr><th align="left" valign="top" colspan="2" >'.date('jS M, Y',strtotime($data['date'])).'</th></tr>'; //format the date as required
echo '</tr>';
}

PHP Roster only showing one player on click

I am trying to put together a guild roster for my WoW guilds website. I have the roster pulling all of the right information and listing it in a table properly. The next step is to pop up a modal and display more information when clicking on one of my guild members names in the list.
Unfortunately, when I click on anyone's name in the list it only displays information of the first person in the list. Can someone please help me understand what I am doing wrong?
So you can see what I mean, go to the following link and click on any random players name in the table. No matter who you choose, it will display information about "Alisyn":
http://www.astalder.net/roster.php
Here is part of the code:
//Guild Roster Table Headers
echo " <div width='600px' align#'center'>";
echo '
<div align="center" id="roster" class="roster" style="float: none;">
<table class="warcraft sortable" border="3" cellspacing="0" cellpadding="0" align="center">
<tr>
<th width="80px" align="center" valign="top" ><strong>Race/Class</strong></a></th>
<th width="140px" align="center" valign="top" ><strong>Name</strong></a></th>
<th width="80px" align="center" valign="top" ><strong>Level</strong></a></th>
<th width="140px" align="center" valign="top" ><strong>Rank</strong></a></th>
<!-- <th width="80px" align="center" valign="top" ><strong>Message</strong></a></th> -->
</tr>';
//Character Arrays
foreach($rows as $p) {
$mrank = $p['rank'];
$mname = $p['name'];
$mclass = $p['class'];
$mrace = $p['race'];
$mlevel = $p['level'];
$mgender = $p['gender'];
$mthumbnail = $p['thumbnail'];
echo "<div id='myModal' class='reveal-modal'>
<h1><center>Details for " . $mname . "</center></h1>
<p>Name: " . $mname . "<br />
Class: " . $mclass . "<br /></p>
<a class='close-reveal-modal'>×</a>
</div>";
//#$json = file_get_contents("http://$region.battle.net/api/wow/character/$realm/$mname?fields=items", true);
//$decode = json_decode($json, true);
//$milvl = " ". $decode['items']['averageItemLevel'] . "";
//Table of Guild Members
echo "
<tr>
<td align='center'><strong><img style=\"padding-left: 5px;\" src=\"race/race_$mrace-$mgender.jpg\"></img><img style=\"padding-left: 5px;\" src=\"class/class_$mclass.jpg\"></img></strong></td>
<!-- <td class='class_$mclass' width=\"140px\" align=\"center\" valign=\"top\" ><div class=\"hover_img\"><strong>$mname<span><img src=\"http://us.battle.net/static-render/us/$mthumbnail\" alt=\"$mname\" height=\"100\"></span></strong></div></td> -->
<td class='class_$mclass' width=\"140px\" align=\"center\" valign=\"top\" ><div class=\"hover_img\"><strong>$mname<span><img src=\"http://us.battle.net/static-render/us/$mthumbnail\" alt=\"$mname\" height=\"100\"></span></strong></div></td>
<td width=\"80px\" align=\"center\" valign=\"top\" ><strong>$mlevel</strong></td>
<td sorttable_customkey='$mrank' width=\"140px\" align=\"center\" valign=\"top\" ><strong>$ranks[$mrank]</strong></td>
<!-- <td width=\"90px\" aligh=\"center\" valign=\"top\" ><div class=\"hover_img\"><img src=\"http://us.battle.net/static-render/us/$mthumbnail\"></div></td> -->
</tr>
";
}
echo " </table></div>";
Reference the rows by their unique id's. Hence when you click the name, the respective ID will be called and the player info will be displayed as per that id:
//Guild Roster Table Headers
echo " <div width='600px' align#'center'>";
echo '
<div align="center" id="roster" class="roster" style="float: none;">
<table class="warcraft sortable" border="3" cellspacing="0" cellpadding="0" align="center">
<tr>
<th width="80px" align="center" valign="top" ><strong>Race/Class</strong></a></th>
<th width="140px" align="center" valign="top" ><strong>Name</strong></a></th>
<th width="80px" align="center" valign="top" ><strong>Level</strong></a></th>
<th width="140px" align="center" valign="top" ><strong>Rank</strong></a></th>
<!-- <th width="80px" align="center" valign="top" ><strong>Message</strong></a></th> -->
</tr>';
//Character Arrays
foreach($rows as $i=>$p) {
$mrank = $p['rank'];
$mname = $p['name'];
$mclass = $p['class'];
$mrace = $p['race'];
$mlevel = $p['level'];
$mgender = $p['gender'];
$mthumbnail = $p['thumbnail'];
echo "<div id='myModal_$i' class='reveal-modal'>
<h1><center>Details for " . $mname . "</center></h1>
<p>Name: " . $mname . "<br />
Class: " . $mclass . "<br /></p>
<a class='close-reveal-modal'>×</a>
</div>";
//#$json = file_get_contents("http://$region.battle.net/api/wow/character/$realm/$mname?fields=items", true);
//$decode = json_decode($json, true);
//$milvl = " ". $decode['items']['averageItemLevel'] . "";
//Table of Guild Members
echo "
<tr>
<td align='center'><strong><img style=\"padding-left: 5px;\" src=\"race/race_$mrace-$mgender.jpg\"></img><img style=\"padding-left: 5px;\" src=\"class/class_$mclass.jpg\"></img></strong></td>
<!-- <td class='class_$mclass' width=\"140px\" align=\"center\" valign=\"top\" ><div class=\"hover_img\"><strong>$mname<span><img src=\"http://us.battle.net/static-render/us/$mthumbnail\" alt=\"$mname\" height=\"100\"></span></strong></div></td> -->
<td class='class_$mclass' width=\"140px\" align=\"center\" valign=\"top\" ><div class=\"hover_img\"><strong>$mname<span><img src=\"http://us.battle.net/static-render/us/$mthumbnail\" alt=\"$mname\" height=\"100\"></span></strong></div></td>
<td width=\"80px\" align=\"center\" valign=\"top\" ><strong>$mlevel</strong></td>
<td sorttable_customkey='$mrank' width=\"140px\" align=\"center\" valign=\"top\" ><strong>$ranks[$mrank]</strong></td>
<!-- <td width=\"90px\" aligh=\"center\" valign=\"top\" ><div class=\"hover_img\"><img src=\"http://us.battle.net/static-render/us/$mthumbnail\"></div></td> -->
</tr>
";
}
echo " </table></div>";

Running to Selects and grouping results with 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>

Categories