Multi Column Database Output -- DIVs, not TABLE

My little franken project is to display one MYSQL query accross 4 columns (A,B,C,D). Each column is a separate div. Each div will contain a separate table.

Here is my strategy to divide one MYSQL query into four separate arrays (1-4)

  1. start with MYSQL query and single $dbq_record_set (ex. 100 total records);

  2. Divide: (total number of records) 100/4 (number of columns) equals (default number of records per column) 25;

  3. Create four variables for each column using #2 base value. (EG. $col_A_records, $col_B_records, $col_C_records, $col_D_records);

  4. Some kind of loop divides $dbq_record_set (100) into four arrays using column variables ($col_A_records, $col_B_records, etc). (EG. $n_array_1[records 1-25], $n_array_2[records 26-50], $n_array_3[records 51-75], $n_array_4[records 76-100]);

  5. Start Column N DIV, and Column N TABLE;

  6. Loop–Assign one record at a time to ASSOC arry with mysql_fetch_array($n_array_1) = $a_array_1;
    6.1) Print each record’s contents in a table using $a_array_1[“col_name”] for how many bits of data in each record;
    6.2) End Column N DIV and TABLE.

  7. Use ‘some kind of conditional statement’ to adjust number of records assigned to each column with variable #3 at location #4 when the variable is changed and the page is reloaded; (EG, page is displayed with 25 records each column. An edit field allows the column variables to be changed and SAVE button or some such device reloads the page displaying the new record count for each column).

Questions? I got the idea. It would be helpful if anyone recognizes this and can suggest changes before I proceed to attempting the code.


Here is the rough–working–cut of the code for this project. This will slice up a database request into even or variable row counts.

$dbq_Result = mysql_query('SELECT stuff FROM thingy', $connectID) or die (error stuff);
while ($full_menu_array[] = mysql_fetch_array($dbq_Result, MYSQL_ASSOC));
$num_records = count($full_menu_array);
// #1 Temp values for var a-d
$a = 12;
$b = 11;
$c = 8;
$d = 17; 
//This is my baby frankenscript...
$a = isset($a) ? $a : NULL; 	
$b = isset($b) ? $b : NULL;
$c = isset($c) ? $c : NULL;
$d = isset($d) ? $d : NULL;	
if ($a!=$b||$a!=$c||$a!=$d||$b!=$c||$b!=$d||$c!=$d) {
	//simple slices of the array matching the row count vars...
	$col1_records = array_slice($full_menu_array, 0, $a);
	$col2_records = array_slice($full_menu_array, $a, $b);
	$col3_records = array_slice($full_menu_array, $a+$b, $c);
	$col4_records = array_slice($full_menu_array, ($a+$b)+$d, $d);
} elseif (($a==$b)==($c==$d)){
//This script is from the link above and slices the array evenly...
	$listLength = count( $full_menu_array );
	$partLength = floor( $listLength / $p );
	$partRemainder = $listLength % $p;
	$partition = array();
	$mark = 0;
	for ($px = 0; $px < $p; $px++) {
		$incr = ($px < $partRemainder) ? $partLength + 1 : $partLength;
		$partition[$px] = array_slice( $full_menu_array, $mark, $incr );
		// alter start location after the last set 			
		$mark += $incr;
	//I added this to change the array names and column counts to the preset variables...
	$col1_records = $partition[0];	
	$a = count( $col1_records );
	$col2_records = $partition[1];
	$b = count( $col2_records );
	$col3_records = $partition[2];
	$c = count( $col3_records );
	$col4_records = $partition[3];
	$d = count( $col4_records );
} else {
echo 'error';	

And the table script uses the almost usual array code,

for ($row = 0; $row < $a; $row++) {...some stuff here}
// start the TR some more stuff here...
print '<td align="right">'.$col1_records[$row]['special_id'].'</td></tr>'."\
// end the table whatever

Since my goal was to have a predetermined number of columns, I can use the $a, $b, $c, and $d vars in a for() loop in each column’s table script and then use the appropriate $row var to access the Associative Array values. The var scope doesn’t leave that column’s PHP chunk.

The #1 reference above is where I hard code the variable column counts. I have yet to determine the best way to adjust each column between the edit and end-user views. Any suggestions?

I think you’re over complicated the problem.

You can just do a while loop and do an if statement to check if it’s displayed a number of records (in this case 25, assuming that it’s always 100). If that returned true, close and open a new DIV.

Step 6 should be step 4.

My plan to achieve this:
Step 0: Create array to hold results.
Step 1: mysql_query to pull all data.
Step 2: Determine column size (ceiling mysql_num_rows / num_columns).
Step 3: While mysql_fetch_array loop; Count variable incrementor.
Step 3.1: Assign resultarray to appropriate array. ($n_array[$i] = $result)
Step 3.2: If count = column size, increment $i, reset count. Next While.
Step 4: For Loop J; Start DIV, Start Table
Step 5: Foreach $n_array[$j], Output Table Row
Step 6: Close TABLE, Close DIV, Next J.


Another way-

These posts have been very helpful. What I have so far is this.
A) I need to divide a mysql database request into editable chunks, there is a function that does just that: array_slice ($array, $offset, $length).
B) I want to begin rendering the page dividing the number of records by the number of columns and malibu’s comment above includes several notable examples (I was able to recognize from our discussions).
This could take the form of an IF, ELSEIF or SWITCH to capture a NULL value for the column variables (first time) or all the same value (redivide, as number of records may have changed) ELSE divide using the new column variables.
C) Put the chunks into a multi-dimensional array where value 1 is the index of the record and value two is the associative column name for one record.

Still working on if A and C can work together.