SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict stu567blue's Avatar
    Join Date
    Aug 2007
    Posts
    316
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    While Loop Out Put 2 or more Columns from One SQL Column

    Hi All,

    Hope all is well.

    I have a question that relates to a puzzling piece of code. I have three tables with records in, all of these are needed. The tables are selecting the right records I just need to output them correctly. What I would like to do is from the one selected column is split the results over two HTML columns, I've created a while loop however whilst it's outputting the information it is not splitting the data over two columns
    .

    Please see below for the code:

    PHP Code:
      <?php 
                             
                      $queryb 
    mysql_query("SELECT option_value_description.name, option_value_description.option_value_id, product_option_value.option_value_id, product_option_value.ob_sku, product_option_value.price
                  FROM option_value_description, product_option_value WHERE option_value_description.option_value_id = product_option_value.option_value_id AND product_option_value.product_id='"
    .$_GET['product_id']."'GROUP BY option_value_description.name") or die (mysql_error());


                
    $querya  mysql_query("SELECT option_value_description.name AS optname, option_description.name, option_description.option_id, option_value_description.option_id, product_option_value.product_id 
                FROM option_description, option_value_description, product_option_value WHERE option_description.option_id = product_option_value.option_id AND product_option_value.product_id='"
    .$_GET['product_id']."'GROUP BY option_description.option_id") or die (mysql_error());   
                
                    
    $queryd  mysql_query("SELECT option_description.name, option_description.option_id, option_value_description.name AS opname, option_value_description.option_id, product_option_value.product_id 
                FROM option_description, option_value_description, product_option_value WHERE option_description.option_id = product_option_value.option_id AND product_option_value.product_id='"
    .$_GET['product_id']."'ORDER BY option_value_description.option_id DESC") or die (mysql_error());   
                    
    $numberresults mysql_num_rows($queryb); 
                    
    $halfresults = ($numberresults 2);
                    
    $counter 0;
                    
                    echo 
    '<table class="attribute">
                                <thead>
                                    <tr>'
    ;
                               
                 
                  while (
    $row mysql_fetch_array($querya)) {
                  
                        echo 

                        <td>'
    .$row['name'].'</td>'
                        
                }
                
                echo 
    '    <td>'.$text_sku.'</td>
                        <td>'
    .$text_price.'</td>
                        </tr>
                        </thead> 
                        '
    ;
                        
                
                while (
    $rowb mysql_fetch_array($queryb) and $rowa mysql_fetch_array($queryd)) {
                
                 
    $queryc mysql_query("SELECT product.price FROM product WHERE product_id ='".$_GET['product_id']."'"); 
                  
                  
    $rowc  mysql_fetch_array($queryc); 
                  
                      
    $total_price $rowb['price'] + $rowc['price'];
                      
    number_format((float)$total_price2'.''');
                    
                    echo 
    '<tr>
                        <td>'
    .$rowb['name'].'</td>
                        <td>'
    .$rowb['name'].'</td>
                        <td>'
    .$rowb['name'].'</td>
                        <td>'
    .$rowb['ob_sku'].'</td>
                        <td>'
    .$total_price.'</td>
                        </tr>'
    ;
                
            }
                
                    echo 
    '
            </table>
          </div>'
    ;
                  
    ?>
    Any help would be great..

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    #1: At a cursory glance, Query "C" is likely unnecessary - can be folded into query "B" with a join.

    #2: Well, there's vertical splitting, and horizontal splitting.

    Vertical Splitting = "Output the first (CEILING(X/2)) entries in column one, the remainder in column two."
    Horizontal Splitting = "Output first and second record in the first row, third and fourth in the second row, etc.."

    The logic for the two I would use is:

    Vertical:
    Code:
    While new row
     store row in array.
    endwhile
    R = ceiling(count array / 2) - 1 //Note: -1 for 0-indexed array
    for I = 0, i <= R, i++
      start row
      output entry I
      output entry R+I+1 //Note: This has the potential to throw an error if count array is odd.
      end row
    endfor
    Horizontal:
    Code:
    tr
    count = 0
    while new row
      output row info
      if count % 2 = 1
        output /tr tr
      endif
      count++
    endwhile
    /tr
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Addict stu567blue's Avatar
    Join Date
    Aug 2007
    Posts
    316
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi There,

    I don't understand the above code... is that PHP?? I've not heard of vertical and/or horizontal splitting. Could you provide me with further details so that I can research this concept further??

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    @StarLion ; showed you the basic flow you should be aiming for using pseudocode, a plain text shorthand human readable version of what you ought to be attempting.

    Why don't you have a go at converting those ideas into PHP, for your chosen method (horizontal or vertical), and show us where you get up to and any errors you incur if it does not work for you.

  5. #5
    SitePoint Addict stu567blue's Avatar
    Join Date
    Aug 2007
    Posts
    316
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you will give this a go


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
  •