Skip navigation
Currently Being Moderated

php looping

Jul 2, 2013 11:15 PM

Using DW 5.5

I am trying to loop thru a DB table.  My connection to the DB works.  The table has multiple fields although I'm only trying to print 2 of them; the holder of the event and the event name in a vertical column with the holder above the event name.

Result I'm trying to get should look like this:

 

Holder

Event name

 

Holder

Event name

 

I get the desired results but only after a repitition of the 'if....{' and the $holder == ....holder'];' lines; it appears as follows:

 

if ($holder != $row_getEvents['event_holder']) { } $holder == $row_getEvents['event_holder']; if ($holder != $row_getEvents['event_holder']) { } $holder == $row_getEvents['event_holder']; if ($holder != $row_getEvents['event_holder']) { } $holder == $row_getEvents['event_holder'];

The above repeats ('if' thru 'if') for the total of the number of records in the table; then it echos the Holder and Event Name lines for all the records in the table as desired.

 

My recordset query is:  $query_getEvents = "SELECT * FROM event_schedule ORDER BY event_holder, event_date ASC";  (this obviously works because the data is there and presents in desired sequence); so, why doesn't the following produce only the data?

 

<body>
<h1>Manage Events</h1>
<table width="600">

<?php $holder = "holder is blank";   (I put 'holder is blank' here solely for testing, it will be null and the next line eliminated)
echo $holder; ?><br />

<?php do { ?>
      if ($holder != $row_getEvents['event_holder']) {
  <tr align="center" valign="top">
        <td><?php echo $row_getEvents['event_holder'] . " / "; ?><br />
        </td></tr>
        }
  <tr align="center" valign="top">
        <td><?php echo $row_getEvents['event_name'] . " / "; ?><br />
  </td></tr>
        $holder == $row_getEvents['event_holder'];
 
<?php } while ($row_getEvents = mysql_fetch_assoc($getEvents)); ?>
</table>

<p> </p>

</body>
</html>
<?php
mysql_free_result($getEvents);
?>

 

Thanks in advance,

Tom Smith

 
Replies
  • Currently Being Moderated
    Jul 3, 2013 5:55 AM   in reply to TjmSmith

    <table width="600">

      <?php do { ?>

        <tr>

          <td><?php echo $row_getEvents['event_holder']; ?></td>

          <td><?php echo $row_getEvents['event_name']; ?></td>

        </tr>

        <?php } while ($row_getEvents = mysql_fetch_assoc($getEvents)); ?>

    </table>

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 3, 2013 6:21 PM   in reply to TjmSmith

    Try

    <?php $holder = null;

    do {

        if ($holder != $row_getEvents['event_holder']) {

            $holder = $row_getEvents['event_holder'];

            echo $holder;

        }

    ?>

    <p>

        <?php echo $row_getEvents['event_holder']; ?>

      <?php echo $row_getEvents['event_name']; ?>

    </p>

    <?php } while ($row_getEvents = mysql_fetch_assoc($getEvents)); ?>

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points