SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question first row of data

    PHP Code:
    <body onLoad="window.print()">
        <?php
        $id
    $_SESSION["bill_no"];
    // select for bill
        
    $sql=mysql_query("select category from category");
        
    $rowmysql_fetch_array($sql);
        while(
    $rowmysql_fetch_array($sql)){ 
        
    $sql_item=mysql_query("select p.item_name,c.qty 
                                from bill as c 
                                LEFT JOIN items as p ON p.id=c.id
                                where p.category_name='"
    .addslashes($row[0])."' and c.bill_id=$id")
                                 or die (
    "Error in query: $sql_item. <br />" mysql_error() ."<br /><br />");
        if (
    mysql_num_rows ($sql_item) >0) {
        echo 
    "<table align='left'>
        <tr><th colspan=5><a href='dup_databse.php'> GURU SWEETS AND SNACKS PARLOUR</a></th></tr>
        <tr><td align='center' colspan=5> Opp.Vishal Cinema,Sevoke Road,Siliguri</td></tr>
        <tr><td align='center' colspan=5> Contact&nbsp;:&nbsp;0353-2004422,&nbsp;2545505,&nbsp;2545506</td></tr>
        <tr>
        <td><font size='-1'><b>Item</td>
        <td><font size='-1'><b>Qty</td>
        </tr>"
    ;    
        while(
    $row_itemmysql_fetch_array($sql_item)){ 
          echo 
    "<tr>
            <td> <font size='-1'>" 
    $row_item[0] ."</td>
             <td><font size='-2'>" 
    $row_item[1] . "</td>
              </tr>"
    ;        
        }
        echo 
    "</table>";
       }
     }
    ?>
    the problem in this code is- it is missing the first category from the category table
    i cant see the problem
    and secondly the tables are getting printed horizontally one after the other
    i want them vertically
    wat changes are to be done?
    thanks in advance

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
        $rowmysql_fetch_array($sql); 
        while(
    $rowmysql_fetch_array($sql)){ 
    See the two lines above? mysql_fetch_array retrieves one row from the result set, and moves the row pointer forward. You've called it twice before any code that uses $row, so you essentially threw that row out.

    Remove the first call to mysql_fetch_array, it's not doing anything useful

  3. #3
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This line before the while loop causes the first row to disappear. You should remove it.
    PHP Code:
    $rowmysql_fetch_array($sql); 
    And now you are printing new table everytime inside the loops and they are aligned to left so all these tables will be side by side. You should add one "parent" outside the loops, that holds all the tables printed inside the loop together and for example print one of these tables inside the loop into one cell of this parent. Or just organize your structure that way you will manage with one table. Meaning still you must open and close the table outside the loops.

    edit:
    Something like this
    PHP Code:
    $result mysql_query("SELECT category_id, category FROM category");
    echo 
    '<table><tr>';
    while (
    $row mysql_fetch_object($result))
    {
        echo 
    '<td><table><tr><th>' $row->category '</th></tr>';
        
    $result2 mysql_query("SELECT product FROM products WHERE category = $row->category_id");
        while(
    $row2 mysql_fetch_object($result2))
        {
            echo 
    '<tr><td>'$row2->product .'</td></tr>';
        }
        echo 
    '</table></td>';
    }
    echo 
    '</tr></table>'
    And output is like
    Code:
    kategory1 | kategory2 | kategory3
    -----------------------------------
    product    | product     | product   
    product    | product     | product    
    product    | product     | product    
    product    | product     | product

  4. #4
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TeNDoLLA View Post
    This line before the while loop causes the first row to disappear. You should remove it.
    PHP Code:
    $rowmysql_fetch_array($sql); 
    And now you are printing new table everytime inside the loops and they are aligned to left so all these tables will be side by side. You should add one "parent" outside the loops, that holds all the tables printed inside the loop together and for example print one of these tables inside the loop into one cell of this parent. Or just organize your structure that way you will manage with one table. Meaning still you must open and close the table outside the loops.

    edit:
    Something like this
    PHP Code:
    $result mysql_query("SELECT category_id, category FROM category");
    echo 
    '<table><tr>';
    while (
    $row mysql_fetch_object($result))
    {
        echo 
    '<td><table><tr><th>' $row->category '</th></tr>';
        
    $result2 mysql_query("SELECT product FROM products WHERE category = $row->category_id");
        while(
    $row2 mysql_fetch_object($result2))
        {
            echo 
    '<tr><td>'$row2->product .'</td></tr>';
        }
        echo 
    '</table></td>';
    }
    echo 
    '</tr></table>'
    And output is like
    Code:
    kategory1 | kategory2 | kategory3
    -----------------------------------
    product    | product     | product   
    product    | product     | product    
    product    | product     | product    
    product    | product     | product

    thanks for the help u both
    but the output i want is in this form
    kategory1
    ----------
    product
    product
    product
    product

    kategory2
    ----------
    product
    product
    product
    product

    kategory3
    ----------
    product
    product
    product
    product

  5. #5
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for ur guidance
    its done
    one thing tat i want to know is
    TeNDoLLA u ve used mysql_fetch_object($result)
    whereas i generally use mysql_fetch_array($result)
    which is preferable to be used?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •