help grouping rows

Discussion in 'PHP' started by ataloss, Oct 15, 2014.

  1. #1
    hey, hey, I have this query; I have this pogram Working great-kinda.
    It prints a page for each row. At EOF a total line is
    displayed. Each row has an index like "account#. If there are
    multiple rows for the same account# I want to print one page for
    that account# with a total for that page. My php code (edited for
    this post)follows:

    <?php
    error_reporting(E_ALL ^ E_NOTICE);
    // error_reporting(0);
    // define variables 
    $acctno=$_POST['acctno'];$pd=$_POST['pd'];
      {pl
    // CONNECT TO THE DATABASE
    $DB_NAME = 'homedb';
    $DB_HOST = 'localhost';
    $DB_USER = 'root';
    $DB_PASS = 'cookie';
    $mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
      if (mysqli_connect_errno())
      {printf("Connect failed: %s\n", mysqli_connect_error());exit();}
    // QUERY ON "oocust" TABLE
    $query = "SELECT * FROM `oocust` WHERE payrec = 'R' AND pd = 'N'";
    $result = $mysqli->query($query) or die($mysqli->error.__LINE__);
    echo "<center>";
    // Associative array
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
      { 
    ?>
    <TABLE BORDER="0" CELLPADDING="12" CELLSPACING="5" >
      <TD>
    <TABLE BORDER="30" CELLPADDING="12" CELLSPACING="5" background="oldorchard.jpg">
    <td>
    <input type="text" size = 25 STYLE="color: #000000; background-color: #7FFF55;" name="Name" value="Old Orchard Plumbing"><br>
    <input type="text" size = 25 STYLE="color: #000000; background-color: #7FFF55;" name="Addy1" value="2210 E. Hogan Hollow Road"><br>
    <input type="text" size = 25 STYLE="color: #000000; background-color: #7FFF55;" name="Addy2" value="Margate, Fl  33063"></td><br>
     
      <TD>
    <input type='text' size = 40 STYLE="color: #000000; background-color: #D4FF55;" name="terms" value="terms <?php echo( htmlspecialchars( $row['terms'] ) ); ?>" /><br>
    <input type='text' size = 40 STYLE="color: #000000; background-color: #D4FF55;" name="duedate" value="duedate <?php echo( htmlspecialchars( $row['duedate'] ) ); ?>" /><br>
    <input type='text' size = 40 STYLE="color: #000000; background-color: #D4FF55;" name="orderno" value="orderno <?php echo( htmlspecialchars( $row['orderno'] ) ); ?>" /><br>
    <input type='text' size = 40 STYLE="color: #000000; background-color: #D4FF55;" name="amtdue" value="amtdue <?php echo( htmlspecialchars( $row['amtdue'] ) ); ?>" /></td><br>
    <TD align="center"><img src="dave pic.jpg" width=150 height=150></td>
      </tr></table>
    </body></html>
    PHP:

     
    Last edited by a moderator: Oct 15, 2014
    ataloss, Oct 15, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    My eyes are hurting...
    just compare the account# from the previous row with the current row in the loop, and if they're NOT the same, create a new row, new table or whatever it is you're trying to do.
     
    PoPSiCLe, Oct 15, 2014 IP
  3. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #3
    It seems like this is a bit of a complicated way of doing things but you could do this. Im removing the body and html tags as I will assume you want a table for each account. Maybe you should order these by account# as well.
    
    <?php
    $prevaccount = -1;
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
      {
    if ($row['accountnumber'] != $prevaccount) {
    if ($prevaccount != -1) {
    ?>
    </tr></table>
    </td>
    <tr></table>
    <?php
    }
    ?>
    <TABLE BORDER="0" CELLPADDING="12" CELLSPACING="5" >
    <tr>
      <TD>
    <TABLE BORDER="30" CELLPADDING="12" CELLSPACING="5" background="oldorchard.jpg">
    <?php } ?>
    <tr>
    <td>
    <input type="text" size = 25 STYLE="color: #000000; background-color: #7FFF55;" name="Name" value="Old Orchard Plumbing"><br>
    <input type="text" size = 25 STYLE="color: #000000; background-color: #7FFF55;" name="Addy1" value="2210 E. Hogan Hollow Road"><br>
    <input type="text" size = 25 STYLE="color: #000000; background-color: #7FFF55;" name="Addy2" value="Margate, Fl  33063"></td><br>
    
      <TD>
    <input type='text' size = 40 STYLE="color: #000000; background-color: #D4FF55;" name="terms" value="terms <?php echo( htmlspecialchars( $row['terms'] ) ); ?>" /><br>
    <input type='text' size = 40 STYLE="color: #000000; background-color: #D4FF55;" name="duedate" value="duedate <?php echo( htmlspecialchars( $row['duedate'] ) ); ?>" /><br>
    <input type='text' size = 40 STYLE="color: #000000; background-color: #D4FF55;" name="orderno" value="orderno <?php echo( htmlspecialchars( $row['orderno'] ) ); ?>" /><br>
    <input type='text' size = 40 STYLE="color: #000000; background-color: #D4FF55;" name="amtdue" value="amtdue <?php echo( htmlspecialchars( $row['amtdue'] ) ); ?>" /></td><br>
    <TD align="center"><img src="dave pic.jpg" width=150 height=150></td>
    <?php
    $prevaccount = $row['accountnumber'];
    }
    ?>
    </tr></table>
    </td>
    <tr></table>
    
    
    PHP:
     
    Anveto, Oct 15, 2014 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #4
    1) what's with the tables for nothing?

    2) what's with the static redundant style bloating your output? (there's a reason I say style should be obsolete as a tag and deprecated as an attribute)

    3) or the CENTER tag that has no business in any code written after 1997?

    4) why are you making extra copies of the $_POST variables for nothing?

    5) why are you wasting printf (a function IMHO shouldn't even EXIST in PHP) on something you could string add and pass to exit?
     
    deathshadow, Oct 17, 2014 IP