Jump to content

Pulling data from one table to use in the next


sparedog

Recommended Posts

Hi all,

I am trying to use the data i have received from one mysql table, table a, to select data in table b, which in turn selects data from a third table, table c.

This is then echoed into a list of two different colors. I tried this code, but the DISTINCT command has no effect on the final results and the results don't separate into two color lines.

 

Here is an example of the code i tried. Sorry if it's messy, I'm just learning  :D

 

<?php

session_start();

$myuname=$_SESSION['uname'];

?>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html><head><title></title>

    <link rel="stylesheet" type="text/css" media="screen,print" href="list_files/list.css" />

    <!--[if IE]><link rel='stylesheet' type='text/css' media='screen,print' href='list_files/listIE.css'/><![endif]-->

 

<style type="text/css">

/*<![CDATA[*/

@import "Scripts/Widgets/HTMLRegion/Paste.css";

/*]]>*/

body

{

font-family:"ARIAL NARROW"; font-size:12px;

}

.container

{

text-align:center;

}

.center_div

{

text-align:left;

}

</style>

    <script type="text/javascript" src="Scripts/iWebSite.js"></script>

    <script type="text/javascript" src="Scripts/Widgets/SharedResources/WidgetCommon.js"></script>

    <script type="text/javascript" src="Scripts/Widgets/Navbar/navbar.js"></script>

    <script type="text/javascript" src="Scripts/Widgets/HTMLRegion/Paste.js"></script>

    <script type="text/javascript" src="list_files/list.js"></script>

</head><body>

 

<?php

$host="localhost"; // Host name

$username="xxxxxxxxxxx"; // Mysql username

$password="xxxxxxxxxxx"; // Mysql password

$db_name="xxxxxxxxxxx"; // Database name

 

// Connect to server and select databse.

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

// pull data from first list

 

$myfriend_list = mysql_query("SELECT DISTINCT example FROM example1 WHERE example3 = '$myuname'");

while ($row1 = mysql_fetch_array($myfriend_list)) {

$result1= "$row1[example]";

 

$myoutput      = mysql_query("SELECT DISTINCT example4 FROM example5 WHERE example6 ='$result1'");

while ($row2 = mysql_fetch_array($myoutput)) {

$result2= "$row2[example4]";

 

$myfinal        = mysql_query("SELECT DISTINCT * FROM example7 WHERE example8 ='$result2'");

 

// Define $color=1

$color="1";

echo '<table width="300" border="0" align="center" cellpadding="0" cellspacing="0">';

while($rows=mysql_fetch_array($myfinal)){

// If $color==1 table row color = #FFC600

if($color==1){

echo "<tr bgcolor='#F0F0F0'>

<td>".$rows['example9']."</td><td>".$rows['example10']."</td><td>".$rows['example11']."

</td>

</tr>";

// Set $color==2, for switching to other color

$color="2";

}

// When $color not equal 1, use this table row color

else {

echo "<tr bgcolor='#D8D8D8'>

<td>".$rows['example9']."</td><td>".$rows['example10']."</td><td>".$rows['example11']."</td>

</tr>";

// Set $color back to 1

$color="1";

}

}

echo '</table>';

}}

 

mysql_close();

?></body></html>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.