* Thus wrote John Taylor-Johnston:
> I want to open table 2 inside table1. See $sql2 below. I know this is illegal.

what your doing is perfectly legal.

> How can I work around this? Or can I? Lost cause?

I'm not sure what kind of behaviour your getting but I'm going to
blow through your code pointing out things that will contribute to
unforseen things happening, and revise your code at the end that
should, in theory fix whatever problem your having.

> 
> $myconnection = mysql_connect($server,$user,$pass);
> #mysql_select_db($db,$myconnection); #not necessary
> $sql1 = 'select * from '.$db.'.'.$table.' order by district,number asc';
> $myconnection2 = mysql_connect($server,$user,$pass);

You don't need two connections to the database in order to do
multiple queries on the same database. Doing this will lead to
troubles.


> #mysql_select_db($db2,$myconnection2); #not necessary

Why isn't this necessary? if you have just opened a connection you
must select a database. Besides the fact that the second connection
isn't necessary.

> 
> $news = mysql_query($sql1); //desc => z-a

You should almost always specify the second parameter to mysql_query()
unless, and only in controlled envrionments, you know that the
query is always going to the same connection.

 see: http://php.net/mysql_query

mysql_query will use the last opened connection handle. So the
above query will be executed on $connection2


> while ($mydata1 = mysql_fetch_object($news))
> {
> echo "$mydata1->district<br />$mydata1->meeting<br />$mydata1->time<br />\n";
> 
> $sql2 = 'select participant,biography,district from '.$db.'.'.$table2.' where 
> district=$mydata1->district';

This kind of explains why you are not wanting to select the
database on connection2. Using the format 'database.table', should
be avoided with mysql.


> $news2 = mysql_query($sql2); //desc => z-a
>       while ($mydata2 = mysql_fetch_object($news2))
>       {
>        echo "<li>$mydata2->participant</li>\n";
>        echo "<li>$mydata2->biography</li>\n";
>       }
> 
>   }

Data Structure assumed:
$table (
  district
  meeting,
  number,
  time,
  /* ... */
)

$table2 {
  district
  particpant,
  biography,
  /* ... */
)

Keep in mind, the approach that follows may be a little advanced,
but I consider it the most efficiant approach to your setup. This
is also untested.

the code:

<!-- the beginning of the list of disticts and particpants -->
<div class="districtlist">
<?php

/* Establish a connection to the server and validate */
$conn = mysql_connect($server,$user,$pass);
if (!$conn) {
  die('Couldnt make connection to db');
}

/* Obtain the database and validate */
if (! mysql_select_db($db,$myconnection) ) {
  die('Couldnt obtain database');
}

/* the data we are looking for */
$sql = "
  select t1.district, t1.meeting, t1.time,
         t2.particpant, t2.particpant
    from $table1 t1, $table2
  where t1.district = t2.district
  order by t1.district, t1.number
  ";


/* fetch the data and error if we are wrong */
$result = mysql_query($sql, $conn);
if(! $result) {
  echo mysql_error();
  die("<br>error with executing query");
}

/* Now, for the meat of it all */

$last_district = ''; /* assuming no district is '' */

/* go through all results */
while ($row = mysql_fetch_object($result) ) {

  /* php is doing the grouping */
  $new_district = $last_district != $row->district

  if ($new_district) {
    ?>
    <div class="district"><?php echo $row->district ?></div>
    <div class="meeting"><?php echo $row->meeting ?></div>
    <div class="time"><?php echo $row->time ?></div>
    <ul class="participants">
    <?php
  }

  /* and show all the items under the ul */
  ?>
  <li class="particpant"><?php echo $row->participant</li>
  <li class="biography"><?php echo $row->biography</li>
  <?php

  /*  And the grouping at the end */
  if ($new_district ) {
    ?></ul></php
  }

  /* save the last district for the grouping */
  $last_district = $row->district;
}
?>
</div> <!-- end of list -->
<?php

mysql_free_result($result);
myslq_close($conn);

^D

If you need the stylesheet to go along with this see
[EMAIL PROTECTED] :)

Opinions, flames, or comments welcome :)

HTH.

Curt
-- 
The above comments may offend you. flame at will.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to