0

Im trying to teach myself php.... I have created a database with 4 elements... region, details, store_name, web_address.

I have created ado-while loop that displays all the records in a 'region'... it displays 'store_name', then 'web_address', then 'details' sorted so all the records with the same 'details' are grouped together;

<table width="600" align="center" border="0" cellspacing="4" >
  <tr>
    <td colspan="2"><h2>Web Stockists: <strong></strong></h2></td>
    <td width="251" colspan="2" align="right"><h3>Region: <?php echo $region_text ?></h3></td>
  </tr>
  <?php do { ?>

    <tr>
      <td colspan="2"><strong><?php echo $row_RegionList['store_name']; ?></strong></td>
      <td colspan="2" rowspan="2" align="center">    <?php echo '<a href="http://' . $row_RegionList['web_address'] . '" target="_blank">' . $row_RegionList['web_address'] . '</a>'; ?></td>
    </tr>
    <tr>
      <td width="14">
      <td width="313"><?php echo $row_RegionList['details']; ?>          </tr>
    <?php } while ($row_RegionList = mysql_fetch_assoc($RegionList)); ?>
</table>

I now want to make it so every record with the same 'details' value get listed under a 'details' sub heading.

This is my attempt;

<table width="600" align="center" border="0" cellspacing="4" >
  <tr>
    <td colspan="2"><h2>Web Stockists: <strong></strong></h2></td>
    <td width="251" colspan="2" align="right"><h3>Region: <?php echo $region_text ?></h3></td>
  </tr>
  <?php     $details = $row_RegionList['details'];
        do {  ?>
  <tr>
  <td width="313"> <h3><?php echo $row_RegionList['details']; ?> </h3></td>
  </tr>
  <?php do { ?>
    <tr>
      <td colspan="2"><strong><?php echo $row_RegionList['store_name']; ?></strong></td>
      <td colspan="2" rowspan="2" align="center">    <?php echo '<a href="http://' . $row_RegionList['web_address'] . '" target="_blank">' . $row_RegionList['web_address'] . '</a>'; ?></td>
    </tr>
  <?php } while ($row_RegionList['details'] == $details); 

    $details = $row_RegionList['details'];

     } while ($row_RegionList = mysql_fetch_assoc($RegionList)); ?>
</table>

This makes a heading of 'region' with a sub heading of 'details' but then creates an eternal loop of the first record in the database. can someone show me what I have done wrong?

  • 2
    FYI, [you shouldn't use `mysql_*` functions in new code](http://stackoverflow.com/questions/12859942/). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://php.net/manual/en/function.mysql-connect.php)? Learn about [*prepared statements*](https://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which one is best for you. – John Conde Oct 02 '17 at 23:35
  • The PHP control logic would be easier to read if you moved the HTML tags into the PHP echo statements instead of a mix of HTML + – Dave S Oct 03 '17 at 00:17

0 Answers0