I've been trying for a while but still, I'm stuck here. Actually I need to get rank from average values. Here my result
Average Rank
39 39 rank is 1
32 32 rank is 1
51 51 rank is 1
57 57 rank is 1
what I really need is
Average Rank
39 3
32 4
51 2
57 1
I've tried several method, but nothing seem to be works. Btw Here's my code
<?php $avg = ($sumc)/($data['total']); echo number_format((float)$avg, 2, ',', ''); ?>
<?php
$array = array($avg);
$i=1;
foreach($array as $key=>$value)
{
$max = max($array);
echo "\n".$max." rank is ". $i."\n";
$keys = array_search($max, $array);
unset($array[$keys]);
if(sizeof($array) >0)
if(!in_array($max,$array))
$i++;
}
And Here's the full Code
<?php $nama_mapel=$ux['nama_mapel'];
$an = mysql_fetch_array(mysql_query("SELECT*FROM mapel where kode_kelas='$kode_kelas' and nama_mapel='$nama_mapel'"))?>
<text style="display:none;"><?php if($NILAI_AKHIR!=0){$NILAI_AKHIR4=substr($NILAI_AKHIR/100*4,0,4); echo $NILAI_AKHIR4;} ?></text>
<tr>
<td width="5%" class="ki1234" height="auto" valign="center" align="center"><?php echo $no++ ;?></td>
<td width="40%" class="ki1234" height="auto" valign="center" align="left"> <?php echo $mmm['nama'];?></td>
<td width="10%" class="ki1234" height="auto" valign="center" align="center"><?php echo $nis; ?></td>
<?php
$mapelz=mysql_query("SELECT*FROM mapel where kode_kelas='$kode_kelas' and id_tp='$id_tp'");
$arrg=[];
while($uxe = mysql_fetch_array($mapelz)){
$id_mapel=$uxe['id_mapel'];
//awal nilai akhir//
$dren = mysql_query("SELECT*FROM nilai_siswa,data_siswa,penilaian_judul where nilai_siswa.id_penilaian_judul=penilaian_judul.id_penilaian_judul and nilai_siswa.nis=data_siswa.nis and nilai_siswa.nis=$nis and penilaian_judul.id_mapel=$id_mapel and penilaian_judul.id_semester='$id_semester' and penilaian_judul.id_tp='$id_tp' order by data_siswa.id_siswa asc"); $jm=mysql_num_rows($dren); $jumlah_tugas=$jm;
if($jm==0){$jumlah="1";} else {$jumlah=$jm; };
$queryj=mysql_query("SELECT sum(n3_100) as rn3_100 FROM nilai_siswa,penilaian_judul where nilai_siswa.id_penilaian_judul=penilaian_judul.id_penilaian_judul and nilai_siswa.nis=$nis and penilaian_judul.id_mapel=$id_mapel and penilaian_judul.id_semester='$id_semester' and penilaian_judul.id_tp='$id_tp'");
while($hasil=mysql_fetch_array($queryj))
{$rn3_100=substr($hasil['rn3_100']/$jumlah,0,5); $RATA_TUGAS=$rn3_100;}
$dren = mysql_query("SELECT*FROM nilai_siswa2,data_siswa,penilaian_judul where nilai_siswa2.id_penilaian_judul=penilaian_judul.id_penilaian_judul and nilai_siswa2.nis=data_siswa.nis and nilai_siswa2.nis=$nis and penilaian_judul.id_mapel=$id_mapel and nilai_siswa2.jenis=2 and penilaian_judul.id_semester='$id_semester' and penilaian_judul.id_tp='$id_tp' order by data_siswa.id_siswa asc"); $jm=mysql_num_rows($dren); $jumlah_uh=$jm;
if($jm==0){$jumlah="1";} else {$jumlah=$jm;};
$queryj=mysql_query("SELECT sum(n_100) as rn_100 FROM nilai_siswa2,penilaian_judul where nilai_siswa2.id_penilaian_judul=penilaian_judul.id_penilaian_judul and nilai_siswa2.nis=$nis and penilaian_judul.id_mapel=$id_mapel and nilai_siswa2.jenis=2 and penilaian_judul.id_semester='$id_semester' and penilaian_judul.id_tp='$id_tp'");
while($hasil=mysql_fetch_array($queryj))
{$rn_100=substr($hasil['rn_100']/$jumlah,0,5); $RATA_UH= $rn_100;}
$dren = mysql_query("SELECT*FROM nilai_siswa2,data_siswa,penilaian_judul where nilai_siswa2.id_penilaian_judul=penilaian_judul.id_penilaian_judul and nilai_siswa2.nis=data_siswa.nis and nilai_siswa2.nis=$nis and penilaian_judul.id_mapel=$id_mapel and nilai_siswa2.jenis=3 and penilaian_judul.id_semester='$id_semester' and penilaian_judul.id_tp='$id_tp' order by data_siswa.id_siswa asc"); $jm=mysql_num_rows($dren); $jumlah_uts=$jm;
if($jm==0){$jumlah="1";} else {$jumlah=$jm;};
$queryj=mysql_query("SELECT sum(n_100) as rn_100 FROM nilai_siswa2,penilaian_judul where nilai_siswa2.id_penilaian_judul=penilaian_judul.id_penilaian_judul and nilai_siswa2.nis=$nis and penilaian_judul.id_mapel=$id_mapel and nilai_siswa2.jenis=3 and penilaian_judul.id_semester='$id_semester' and penilaian_judul.id_tp='$id_tp'");
while($hasil=mysql_fetch_array($queryj))
{$rn_100=substr($hasil['rn_100']/$jumlah,0,5); $RATA_UTS= $rn_100;}
$dren = mysql_query("SELECT*FROM nilai_siswa2,data_siswa,penilaian_judul where nilai_siswa2.id_penilaian_judul=penilaian_judul.id_penilaian_judul and nilai_siswa2.nis=data_siswa.nis and nilai_siswa2.nis=$nis and penilaian_judul.id_mapel=$id_mapel and nilai_siswa2.jenis=4 and penilaian_judul.id_semester='$id_semester' and penilaian_judul.id_tp='$id_tp' order by data_siswa.id_siswa asc"); $jm=mysql_num_rows($dren); $jumlah_us=$jm;
if($jm==0){$jumlah="1";} else {$jumlah=$jm;};
$queryj=mysql_query("SELECT sum(n_100) as rn_100 FROM nilai_siswa2,penilaian_judul where nilai_siswa2.id_penilaian_judul=penilaian_judul.id_penilaian_judul and nilai_siswa2.nis=$nis and penilaian_judul.id_mapel=$id_mapel and nilai_siswa2.jenis=4 and penilaian_judul.id_semester='$id_semester' and penilaian_judul.id_tp='$id_tp'");
while($hasil=mysql_fetch_array($queryj))
{$rn_100=substr($hasil['rn_100']/$jumlah,0,5); $RATA_US= $rn_100;}
if(!isset($RATA_TUGAS)){$RATA_TUGAS2=0;}else{$RATA_TUGAS2=$RATA_TUGAS;};
if(!isset($RATA_UH)){$RATA_UH2=0;}else{$RATA_UH2=$RATA_UH;};
if(!isset($RATA_UTS)){$RATA_UTS2=0;}else{$RATA_UTS2=$RATA_UTS;};
if(!isset($RATA_US)){$RATA_US2=0;}else{$RATA_US2=$RATA_US;};
$jumlahc=($persen['tugas']*$RATA_TUGAS2)/100+($persen['uh']*$RATA_UH2)/100+($persen['uts']*$RATA_UTS2)/100+($persen['us']*$RATA_US2)/100;
if($jumlah_tugas!=0){$k=1;} else {$k=0;};
if($jumlah_uh!=0){$l=1;} else {$l=0;};
if($jumlah_uts!=0){$m=1;} else {$m=0;};
if($jumlah_us!=0){$n=1;} else {$n=0;};
$JUMLAH_PERSEN1=($persen['tugas']*$k)+($persen['uh']*$l)+($persen['uts']*$m)+($persen['us']*$n);
if($JUMLAH_PERSEN1==0){$JUMLAH_PERSEN=1;}else{$JUMLAH_PERSEN=$JUMLAH_PERSEN1;};
$NILAI_AKHIRg=substr(($jumlahc/$JUMLAH_PERSEN)*100,0,5);
$nile= $NILAI_AKHIRg;
$arrg[] = $NILAI_AKHIRg;} ?>
<?php $p = mysql_fetch_array(mysql_query("SELECT * FROM peringkat WHERE kode_kelas='$kode_kelas' and nis='$nis'and id_tp='$id_tp'")); ?>
<text style="display:none;"> <?php $sumc = 0; $output = ""; while($element = array_shift($arrg)){$sumc += $element; $output .= $element; if(sizeof($arrg) > 0){ $output .= " + ";}} echo $output . PHP_EOL;?></text>
<td width="15%" class="ki1234" height="auto" valign="center" align="center"><?php echo $sumc;?></td>
<text style="display:none;"><?php $result=mysql_query("SELECT count(*) as total FROM mapel WHERE kode_kelas='$kode_kelas' and kel!=3"); $data=mysql_fetch_assoc($result); echo $data['total'];?></text>
<td width="15%" class="ki1234" height="auto" valign="center" align="center"><?php $avg = ($sumc)/($data['total']); echo number_format((float)$avg, 2, ',', ''); ?></td>
<td width="15%" class="ki1234" height="auto" valign="center" align="center"><?php
$array = array($avg);
$i=1;
foreach($array as $key=>$value)
{
$max = max($array);
echo "\n".$max." rank is ". $i."\n";
$keys = array_search($max, $array);
unset($array[$keys]);
if(sizeof($array) >0)
if(!in_array($max,$array))
$i++;
}
?></td>
</tr>
<?php }?>
Need Your help.. Anyone..?!
<?php $avg = ($sumc)/($data['total']); echo number_format((float)$avg, 2, ',', ''); ?>
<?php
$array = array($avg); // useless
$i=1;
foreach($array as $key=>$value) // also useless, you never use these variables, and since $array contains only one entry, there is no need to foreach it
{
$max = max($array);
echo "\n".$max." rank is ". $i."\n";
$keys = array_search($max, $array);
unset($array[$keys]);
if(sizeof($array) >0)
if(!in_array($max,$array))
$i++;
}
Let's clean that up :
<?php $avg = ($sumc)/($data['total']); echo number_format((float)$avg, 2, ',', ''); ?>
<?php
$max = $avg; // That's actually what you were doing. Cleaning the code makes it obvious, that's all
echo "\n".$max." rank is ". $i."\n";
$keys = array_search($max, $array);
unset($array[$keys]);
if(sizeof($array) >0)
if(!in_array($max,$array))
$i++;
}
The code still has errors, but the more we correct, the more its design flaws appear, and the less it's therefore correctable.
You should, in my opinion, look at that code, figure a way to put all the values you want to have in $array in it so that then you can compute the datas you need.
From what you've posted seems the problem is how to sort the average values descending.
With this in mind you know that if you're calculating the average on the go, you have to go throuh the entire array first so here's some snippet to help you:
// Random values I want to calculate the average
$values = [
1 => [15, 25, 35],
2 => [45, 55, 65],
3 => [75, 85, 95],
4 => [105, 115, 125]
];
$avgs = array();
foreach ($values as $key => $value) {
$avgs[$key] = array_sum($value) / count($value); // calculate average for each key on the array
}
print_r($avgs);
// Will print:
// Array
// (
// [1] => 25
// [2] => 55
// [3] => 85
// [4] => 115
//)
arsort($avgs); // sort the array in descending order
print_r($avgs);
// Will print:
//Array
//(
// [4] => 115
// [3] => 85
// [2] => 55
// [1] => 25
//)
So there if have your avg array sorted, therefore you have your "rank".
ps. The keys 1, 2, 3, 4 are just random values, you could have anything there.
Related
In the following codes, its coming numbers 1, 2, 3, 4, 5 .....and so on but i need that number in 5, 4, 3, 2,1 like that
<?php
//$events = $db->select("select * from main_menu where mm_menu='4' and mm_status='1' and mm_project='1' order by mm_order asc limit 0,10");
$events = $db->select("select * from main_menu where mm_menu='4' and mm_status='1' and mm_project='1' order by mm_id desc");
if (count($events) > 0) {
$i = 1;
foreach ($events as $event_info) {
$clsIn = ($i==1) ? "in" : "";
$clsOut = ($i==1) ? "" : "collapsed";
?>
<tr>
<td><?php echo $i ?></td>
<td><?php echo $event_info['mm_title'];?></td>
<td><?php echo html_entity_decode($event_info['mm_content']);?></td>
</tr>
<?php
$i++;
}
}?>
If you want to count down, set $i to the total number of events, and then decrement it:
First:
$i = count($events);
and then at the end of the loop:
$i--;
Let's say i have the following array:
$array = [
['time_spent' => 10, 'rate' => 150],
['time_spent' => 20, 'rate' => 100]
];
I want the table to look like the following:
It has 2 rows and 4 columns (Time Spent, Rate, Total, Grand Total):
The first row will be:
10 | 150 | 10 x 150 = 1500 | 3500
and the second row:
20 | 100 | 10 x 200 = 2000
(3500 is the sum of the total of both records)
Assuming you you wanted to output an HTML table, something like this would work.
$array= [
["time_spent"=> 10, "rate"=> 150],
["time_spent"=> 20, "rate"=> 100]
];
$html = [];
$sum = 0;
foreach ($array as $i => $row) {
$product = $row['rate'] * $row['time_spent'];
$line = "<tr><td>{$row['time_spent']}</td><td>{$row['rate]'}</td>";
$line .= "<td>{$row['time_spent']} x {$row['rate']} = {$product}</td>";
if ($i !== 0) {
$line .= '</tr>';
}
$html[] = $line;
$sum += $product;
}
$rowCount = count($html);
$html[0] .= "<td rowspan='{$rowCount}'>{$sum}</td></tr>";
echo '<table>' . implode('', $html) . '</table>';
EDIT: Changed code to reflect the change in input.
Note that there would be better ways to simply calculate total sum. This method generates the desired display along with the sum with a single loop through the data.
Using html template strings with s/printf() does a great job of keeping clean, readable code with separation between processing and markup.
I am purposely making this script verbose. The advantage in this script is readability and maintainability.
It is an unusual requirement to have the grand total in the right column of the table instead of the final row of the table. If you desired the total to be in the final row, then the code would be easier to write and read (for humans).
In my demo, click on the eye icon in the upper right of the output box to see the html rendered version of the result.
Code: (Demo)
$array = [
['time_spent' => 10, 'rate' => 150],
['time_spent' => 20, 'rate' => 100]
];
$tableMarkup = <<<'HTML'
<table>
%s
%s
</table>
HTML;
$headerRowMarkup = <<<'HTML'
<tr>
<th>Time Spent</th>
<th>Rate</th>
<th>Total</th>
<th>Grand Total</th>
</tr>
HTML;
$firstRowMarkup = <<<'HTML'
<tr>
<td>%1$d</td>
<td>%2$d</td>
<td>%1$d x %2$d = %3$d</td>
<td rowspan="%4$d">%5$d</td>
</tr>
HTML;
$subsequentRowMarkup = <<<'HTML'
<tr>
<td>%1$d</td>
<td>%2$d</td>
<td>%1$d x %2$d = %3$d</td>
</tr>
HTML;
$rowsMarkup = [];
$count = count($array);
if ($count) {
$grandTotal = array_reduce(
$array,
function ($carry, $row) {
$carry += $row['rate'] * $row['time_spent'];
return $carry;
},
0
);
$row = array_shift($array);
$rowsMarkup[] = sprintf(
$firstRowMarkup, // template string
$row['time_spent'], // %1$d
$row['rate'], // %2$d
$row['time_spent'] * $row['rate'], // %3$d
$count, // %4$d
$grandTotal // %5$d
);
}
foreach ($array as $row) {
$rowsMarkup[] = sprintf(
$subsequentRowMarkup,
$row['time_spent'],
$row['rate'],
$row['time_spent'] * $row['rate']
);
}
if ($rowsMarkup) {
printf(
$tableMarkup,
$headerRowMarkup,
implode("\n", $rowsMarkup)
);
}
You need to calculate the grand total before you start printing the table. Then you can show it on the first row of the table.
$grand_total = 0;
foreach ($array as $item) {
$grand_total += $item['rate'] * $item['time_spent'];
}
echo "<table>";
foreach ($array as $i => $item) {
$total = $row['time_spent'] * $row['rate'];
echo "<tr><td>{$row['time_spent']}</td><td>{$row['rate']}</td>";
if ($i == 0) { // first row, show grand total
echo "<td>$total</td><td>$grand_total</td>";
} else {
echo "<td colspan='2'>$total</td>";
}
echo "</tr>";
}
echo "</table>";
I wrote this code to sum only amount but is not working.Everything works perfectly except the sum. Can some one help me out?
Here is the code:
<tr >
<td > Total </td>
<td >
<?php $sum2=0; foreach($contributions_ as $val){
$sum = $sum2 + $contributions_[$val];
}echo array_sum($contributions_);
?>
</td>
</tr>
Here is the php:
<?php
$tempArray = array(
$rowRes['amount'],
$rowRes['remarks'],
$rowRes['ID']
);
$contributions_[] = $tempArray;
?>
I want to sum only amount(s) whiles the condition is true
This is my query which worked perfectly but cannot sum amount after displaying the various members contributions .
<?php $selectQuery = "select a.*,b.* ID, monthname(date) as month,month(date) as mnth, year(date) as yr, type,amount, recipient_initials,remarks, in_kind_items,other_type,other_payment_name,date from member_payments a inner join member b on a.memberID = b.ID";
?>
Why can't I sum amount here like SUM(amount)
<tr >
<td> Total </td>
<td>
<?php
$sum = 0;
foreach($contributions_ as $val){
$sum = $sum + $val['amount'];
}
echo $sum;
?>
</td>
</tr>
<?php
$tempArray = array(
$rowRes['amount'],
$rowRes['remarks'],
$rowRes['ID']
);
$contributions_[] = $tempArray;
$sum += $rowRes['amount'];
?>
Got it now after echoing sum
You used $sum and $sum2 in this line:
$sum = $sum2 + $contributions_[$val];
If I'm reading your code right, it should be more like this:
<?php $sum2=0; foreach($contributions_ as $val){
$sum2 = $sum2 + $contributions_[$val];
}echo $sum2;
?>
Sorry, I don't know what you mean by "I want to sum only amount(s) whiles the condition is true".
I am trying to take this style of code and generate based on how many machines are returned from the database.
I have this:
<table style="width: 100%; height:85%;table-layout:fixed;text-align:center;">
<tr>
<td><?php echo $array[0]; ?> <span class="blue">#1</span><br>
sets <span class="blue">1</span> reps <span class="blue">50</span><br>
weight <span class="blue">25</span></td>
<td><?php echo $array[1]; ?> <span class="blue">#2</span><br>
sets <span class="blue">1</span> reps <span class="blue">100</span><br>
weight <span class="blue">40</span></td>
</tr>
<tr>
<td><?php echo $array[2]; ?> <span class="blue">#3</span><br>
sets <span class="blue">2</span> reps <span class="blue">25</span><br>
weight <span class="blue">20</span></td>
<td><?php echo $array[3]; ?> <span class="blue">#4</span><br>
sets <span class="blue">3</span> reps <span class="blue">25</span><br>
weight <span class="blue">30</span></td>
</tr>
</table>
Here is the database code to retrieve the machines from the database:
$sql1 = "SELECT m1.machine_id, m2.* FROM userPlanDetail AS m1 LEFT JOIN machines AS m2 ON m1.machine_id = m2.machine_id WHERE `user_id` = '$user_id1' AND `cardio` = 0";
$retval1 = mysql_query( $sql1, $conn );
$array = array();
while ($row = mysql_fetch_array($retval1, MYSQL_ASSOC)) {
$array[] = $row['machine_name'];
}
I want the code to detect how many machines are in the database that aren't cardio machines and generate the number of table rows accordingly.
Here is a visual representation:
Since you are loading all of your results into an array you can use array_chunk to break the array into rows and then output accordingly.
$array = array_chunk($array, 4);
echo '<table style="width: 100%; height:85%;table-layout:fixed;text-align:center;">';
foreach($array as $row){
echo "<tr>";
foreach($row as $cell){
// echo out your tds for format each cell here.
}
echo "</tr>";
}
echo '</table>';
Updated for hard-coded machine attributes (not really a good idea)
$machine_atts = array(
'bicep curl' => array( 'sets' => 1, 'reps' => 50, 'weight' => 25 ),
'cable chest press' => array( 'sets' => 1, 'reps' => 100, 'weight' => 40 ),
'lat pulldown' => array( 'sets' => 2, 'reps' => 25, 'weight' => 20 ),
'tricep extension' => array( 'sets' => 3, 'reps' => 25, 'weight' => 30 ),
);
$sql1 = "SELECT m1.machine_id, m2.* FROM userPlanDetail AS m1 LEFT JOIN machines AS m2 ON m1.machine_id = m2.machine_id WHERE `user_id` = '$user_id1' AND `cardio` = 0";
$retval1 = mysql_query( $sql1, $conn );
$array = array();
$i=0;
while ($row = mysql_fetch_array($retval1, MYSQL_ASSOC)) {
$i++;
$sets = $machine_atts[$row['machine_name']]['sets'];
$reps = $machine_atts[$row['machine_name']]['reps'];
$weight = $machine_atts[$row['machine_name']]['weight'];
$array[] = '<td>'.$row['machine_name'].'> <span class="blue">'.$i.'</span><br>sets <span class="blue">'.$sets.'</span> reps <span class="blue">'.$reps.'</span><br>weight <span class="blue">'.$weight.'</span></td>';
}
echo '<table style="width: 100%; height:85%;table-layout:fixed;text-align:center;">';
foreach ( array_chunk( $array, 2 ) as $chunk )
echo '<tr>' . implode('', $chunk ) . '</tr>';
echo '</table>';
Original Answer
Assuming the number of sets, number of reps, and weight are in the results set of the query:
$sql1 = "SELECT m1.machine_id, m2.* FROM userPlanDetail AS m1 LEFT JOIN machines AS m2 ON m1.machine_id = m2.machine_id WHERE `user_id` = '$user_id1' AND `cardio` = 0";
$retval1 = mysql_query( $sql1, $conn );
$array = array();
$i=0;
while ($row = mysql_fetch_array($retval1, MYSQL_ASSOC)) {
$i++;
$array[] = '<td>'.$row['machine_name'].'> <span class="blue">'.$i.'</span><br>sets <span class="blue">'.$row['sets'].'</span> reps <span class="blue">'.$row['reps'].'</span><br>weight <span class="blue">'.$row['weight'].'</span></td>';
}
echo '<table style="width: 100%; height:85%;table-layout:fixed;text-align:center;">';
echo '<tr>' . implode('</tr><tr>', array_chunk( $array, 2 ) ) . '</tr>';
echo '</table>';
<?php
for ($x=0; $x<sizeOf($array); $x++) {
echo '<tr><td>Array Data Here</td></tr>';
}
?>
I use the following to print a 15-row, two-column table from an associative arrray in PHP:
<table id="freq-table" class="table-list" cellspacing="0" cellpadding="10">
<tbody>
<?
$rowCount = 0;
foreach($word_frequency as $word => $frequency) if ($rowCount++ < 15) {?>
<tr>
<td><? echo $word; ?></td>
<td<? echo $frequency; ?></td>
</tr>
<?}?>
</tbody>
</table>
It works fine but takes up too much vertical space on the page. How can I format this into three side-by-side tables of five rows and two columns each with the first group having array items 1-5, the second group 6-10, and the last group 11-15? (refer to following illustration):
key1 value1 | key6 value6 | key11 value11
key2 value2 | key7 value7 | key12 value12
...
...
key5 value5 | key10 value10 | key15 value15
I've tried various table, div, container, and multiple loop experiments with very mixed (and unsuitable) results. Thank you in advance.
Since you can also use multiple columns to achieve this visual effect, you're going to probably want to format the data ahead of time to make the code of generating the table a little cleaner.
<?php
// Format the array data so each row contains all of the columns needed
$rows = array();
$max_per_column = 5;
$max_words = 15;
$rows = array_pad($rows, $max_per_column, array());
$count = 0;
foreach ($word_frequency as $word => $frequency) {
if ($count >= $max_words) {
break;
}
array_push($rows[$count % $max_per_column], $word, $frequency);
$count++;
}
?>
<table id="freq-table" class="table-list" cellspacing="0" cellpadding="10">
<tbody>
<?php
foreach ($rows as $cols) {
echo '<tr><td>' . implode('</td><td>', $cols) . '</td></tr>';
}
?>
</tbody>
</table>
Try some thing like that.
<table width="100%">
<tr>
<?php
$rowCount = 0;
foreach($word_frequency as $word => $frequency) if ($rowCount < 15) { ?>
<?php if($rowCount % 5 == 0) { ?>
<td><table border=1>
<?php } ?>
<tr>
<td><?php echo $word; ?></td>
<td><?php echo $frequency; ?></td>
</tr>
<?php if($rowCount % 5 == 4) { ?>
</table></td>
<?php } ?>
<?php $rowCount++; } ?>
</tr>
</table>
The only way to do this is to use a numeric key. For example
$word_frequency[0] = array();
$word_frequency[0]['word'] = "some word";
$word_frequency[0]['frequency'] = 10;
...
$word_frequency[15] = array();
$word_frequency[15]['word'] = "some other word";
$word_frequency[15]['frequency'] = 14;
Once you have your array, you could repeat the loop as follows
<table id="freq-table" class="table-list" cellspacing="0" cellpadding="10">
<tbody>
<?
$rowCount = 0;
for($i = 0; $i <= 5; $i++) {
$word[0] = $word_frequency[$i]["word"];
$frequency[0] = $word_frequency[$i]["frequency"];
$word[1] = $word_frequency[$i + 5]["word"];
$frequency[1] = $word_frequency[$i + 5]["frequency"];
$word[2] = $word_frequency[$i + 10]["word"];
$frequency[2] = $word_frequency[$i + 10]["frequency"];
?>
<tr>
<?
for($x = 0; $x <= 2; $x++ ){
?>
<td><? echo $word[$x]; ?></td>
<td<? echo $frequency[$x]; ?></td>
<?
}
?>
</tr>
<?
}
?>
</tbody>
</table>
Every $i loop creates the row, while the $x loop creates the columns. This assumes, of course, that you have at least 15 items, that you're going to create only 5 rows and three columns of key/value pairs.
Well, if I were doing it, I'd probably use a ul with a css columns :D
http://davidwalsh.name/css-columns
However, that's not a very "programmerly" thing to do.
It might help that you don't have to go through the array in order.
If you look at what you have, there is a pattern to the indices:
i + 0x, i + 1x, i + 2x, ...
Where x is your number of items divided by the number of columns.
and you stop iterating when i = total / x
Sorry to be too tired to code this for you but the gist is this:
$myarray = [1 ... 15];
$columns = 3;
$arrayKeys = array_keys($myarray);
$total = count($array);
$x = floor $total / $column;
for ($i = 0; $i > $x; $i += $x + 1 ){
echo $arrayKeys[$i] . " - " . $myarray($arrayKeys[$i]);
echo $arrayKeys[$i + $x] . " - " . $myarray($arrayKeys[$i + $x]);
echo $arrayKeys[$i + ($x * 2) ] . " - " . $myarray($arrayKeys[$i + ($x * 2)]);
}
Or something like that... that's off the top of my head and I think it has some issues, but you should be able to beat that into something that works.