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
                     	
              	$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_price, 2, '.', '');
				
				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… :smiley:

#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:


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:


tr
count = 0
while new row
  output row info
  if count % 2 = 1
    output /tr tr
  endif
  count++
endwhile
/tr

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??

@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.

Thank you will give this a go :smiley: