Insert tr after every third loop - php

I'm making a forum in PHP. I have to display all forum categories in a table, and to do so, I have used a while loop. However, I want to have only 3 td's in every table row. To loop through the categories, I'm using a while loop with the query, so I don't think I can use modulus here.

Why can't you use modulus? Just add a counter somewhere, and if it hits % 3 == 0 reset the counter and do your stuff.
You might need to do some extra if's for first and last and stuff like that, but there is no reason not to use a modulo with a while.
$i=0;
while(guard()){
if($i % 3 == 0){
//ploing
}
$i++
}

This code will close any extra rows:
<table>
<?php
$columns = 3;
$i = 0;
while($row = mysql_fetch_array($result)){
$i++;
//if this is first value in row, create new row
if ($i % $columns == 1) {
echo "<tr>";
}
echo "<td>".$row[0]."</td>";
//if this is last value in row, end row
if ($i % $columns == 0) {
echo "</tr>";
}
}
//if the counter is not divisible by the number of columns, we have an open row
$spacercells = $columns - ($i % $columns);
if ($spacercells < $columns) {
for ($j=1; $j<=$spacercells; $j++) {
echo "<td></td>";
}
echo "</tr>";
}
?>
</table>

I haven't tested the code, but the logic should work:
<Table>
<?php
$i = 0;
while($row = mysql_fetch_array($result)){
if($i == 0){
echo"<TR>";
}
echo"<td>".$row[0]."<TD>";
$i++;
if($i == 3)
{
$i = 0;
echo"</tr>"
}
}
if($i ==1){
echo "<td></td><td></td></tr>";
}
if($i ==2)
{
echo "<td></td></tr>";
}
?>
<table>

Related

Second row from MySql Database

I am pulling rows from a MySql DB, and I want to enter a line break on the 2nd row.
for ($i = 1; $i <= mysql_num_rows($result); $i++)
{
$row = mysql_fetch_array($result);
$status = $row ['status'];
echo "$status";
}
if ($i % 4 == 0) {
echo '';
}
Is there a way of knowing if it is the 2nd row? Im guessing its something to do with $i?
Sorry if it is a silly question!
You're right. It has something to do with the $i variable.
If you want to enter it the 2nd row only you can evaluate $i==2. Also you could add it every 2nd row by evaluating $i % 2 == 0
This results in the following code:
for ($i = 1; $i <= mysql_num_rows($result); $i++)
{
$row = mysql_fetch_array($result);
$status = $row ['status'];
echo "$status";
if ($i == 2){ // Or replace the evaluation with $i % 2 == 0
echo '<br />'; //HTML linebreak
//echo '\n'; //This is for a newline character.
}
}
if ($i % 4 == 0) {
echo '';
}
This should do it:
for ($i = 1; $i <= mysql_num_rows($result); $i++) {
$row = mysql_fetch_array($result);
$status = $row['status'];
echo "$status";
if ($i == 2) {
echo "\n"; //line break
}
}
if ($i % 4 == 0) {
echo '';
}
If you want an HTML line break, use:
echo "<br/>";
Your indenting might lead you to think the $i mod 4 is inside of the for loop when in fact it is not.
No questions are silly :)

Foreach multiple result set print in HTML table

I am trying to pull two results from 2 sql statements and print into HTML table.
but some how results are not coming correct.please hel me to resolve.
Below is code 2nd result is not printing correct results - duplicated results.
<?php
global $wpdb;
$result1=$wpdb->get_results("select post_name,id,cat from wp_posts where post_name like '%java%');
$result2=$wpdb->get_results("select post_name,id,cat from wp_posts where post_name like '%oracle%');
?>
<table id="table_id">
<thead>
<tr>
<th>JAVA</th>
<th>ORACLE</th>
</tr>
</thead>
<tbody>
<?php foreach($result1 as $rows1) { ?>
<?php foreach($result2 as $rows2) { ?>
<tr>
<td> <?php echo $rows1->post_name ; ?> </td>
<td> <?php echo $rows2->post_name ; ?> </td>
</tr>
<?php
}
?>
<?php
}
?>
global $wpdb;
$result1=$wpdb->get_results("SELECT post_name,id,cat FROM wp_posts WHERE post_name LIKE '%java%'");
$result2=$wpdb->get_results("SELECT post_name,id,cat FROM wp_posts WHERE post_name LIKE '%oracle%'");
After getting to objects , we put the required values in two arrays so that we can create a multi-dimensional array.
$result_one = array();
$result_two = array();
//putting values in array
foreach ($result1 as $result) {
$result_one[] = $result->post_name;
}
foreach ($result2 as $result) {
$result_two[] = $result->post_name;
}
//Creating a multi-dimensional array so that we can print two columns easily
$results = array(
$result_one,
$result_two,
);
//Initialising the variables to be used as counters and array indexes.
$i = 0; $j = 0; $k = 0; $p = 1;
//getting length of array
$array_length = count($result_one);
Now start two loops to fetch data from multi-dimensional array created.
In this case: To create the table properly we need values in this order:
$results[0][0], $results[1][0], $results[0][1], $results[1][1],
$results[0][2], $results[1][2] and so on.
So we have to create loops accordingly.
while ($i < 2) {
while ($j < $array_length) {
if (fmod($p,2)) echo '<tr>'; // So that <tr> will be printed only after two <td>
echo "<td>". $results[$i][$j]. "</td>";
if($i == 0) { $i = 1; } else { $i =0; } // Toggling values of $i from 0 to 1.
$k++;
if(fmod($k, 2) == 0){ $j ++; } // Increasing $j after 2 steps.
$p++;
if (fmod($p,2)) echo '</tr>';
}
$i ++;
}
If you need three columns:
Repeat steps one two and three so that you can create an array like this:
$results = array(
$result_one,
$result_two,
$result_three,
);
Change first while loop to
while ($i < 3)
And change the following statements as:
if (fmod($p,3)) echo '<tr>';
if($i == 0) { $i = 1; } elseif ($i == 1) {$i=2} else { $i = 0; }
if(fmod($k, 3) == 0){ $j ++; }
if (fmod($p,3)) echo '</tr>';
I hope you got better understanding now and can make the required changes yourself.
This seems complicated. But this should work. Please try.
Anyone please feel free to edit the while loop to make it simpler.
something i missed out for 3rd result array.please correct me.
$results = array(
$result_one,
$result_two,
$result_three,
);
$i = 0; $j = 0; $m = 0; $k = 0; $p = 1;
$array_length = count($result_one);
?>
<table id="table_id">
<thead>
<tr>
<th>ORACLE</th>
<th>JAVA</th>
<th>SAP</th>
</tr>
</thead>
<tbody>
<?php while ($i < 3) {
while ($j < $array_length) {
if (fmod($p,3)) echo '<tr>';
echo "<td>" . $results[$i][$j] . "</td>";
if($i == 0) { $i = 1; } elseif ($i == 1) {$i=2} else { $i = 0; }
$k++;
if(fmod($k, 3) == 0){ $j ++; }
$p++;
if (fmod($p,3)) echo '</tr>';
}
$i ++;
}
?>

for loop keeps timing out, but it looks fine

Morning, I have a script tha is calling entries from a database, their history actually, and then displays the entries in a table, right now in order to create the table im using a pretty big for loop to make comparisons and add the results in. However for some reason it is timing out after the 30 seconds(the loop doesnt make that many cycles) i have dtermined that the cause is an issue with the first inner loop due to the fact that the echo statement in there just repeats for the duration of the loop, it never leaves that. any ideas?
for($i = 1; $i <= $FirstCount; $i++)
{
$HistoryTable .= "<tr>";
if($i = 1)
{
for($j = 0; $j < $ThirdCount; $j++)
{
if($EntryTwo[0][$j+1] == $EntryOne[$j])
{
$HistoryTable .= "<td></td>";
}
else
{
$HistoryTable .= "<td>".$EntryTwo[0][$j+1]."</td>";
}
echo $EntryTwo[0][$j+1].' == '.$EntryOne[$j];
}
}
else
{
$first = 0;
$second = 1;
for($k = 1; $k <= $SecondCount; $k++)
{
if($EntryTwo[$first][$k] == $EntryTwo[$second][$k])
{
$HistoryTable .= "<td>".$EntryTwo[$second][$k]."</td>";
}
else
{
$HistoryTable .= "<td></td>";
}
$first++;
$second++;
}
unset($k);
unset($first);
unset($second);
}
$HistoryTable .= "</tr>";
}
variables:
$FirstCount = 4;
$SecondCount = 18
$ThirdCount = 17
if($i = 1) is setting $i to 1 every time, so it's an infinite loop.
What you want is if ($i == 1).

PHP - Dynamic 3 column result table

I have this script which produces a 3 column table of results:
$cols = 3;
$row = 0;
$column = 0;
$count = 0;
echo '<table>';
while($row = mysql_fetch_assoc($result)) {
$count++;
if ($column == 0) {
echo '<tr>';
}
echo "<td>$row['dbField']</td>";
$column++;
if ($column >= $cols) {
$row++;
echo '</tr>';
$column = 0;
}
}
echo '</table>';
This works okay, except if there's only 1 result, it only prints one cell. I would like it to finish off the row of 3, so in this case, two cells would be empty.
I have the total number of records stored in a session variable $_SESSION['r_count'] and thought it would be fairly simple to add the following snippet after the $column++ part of the above:
if ($count == $_SESSION['r_count'] && $column < $cols) {
echo '<td></td>';
$column++;
}
I thought wrong. Could anyone advice me on how to modify this correctly?
If you know the number of rows (i.e. mysql_num_rows()):
for ($i = 0, $n = ceil($nr_of_rows / $cols); $i != $n; ++$i) {
$row = mysql_fetch_assoc($result);
if ($i % $cols == 0) { echo '<tr>'; }
echo '<td>', $row ? htmlspecialchars($row['dbField']) : '', '</td>';
if (($i + 1) % $cols == 0) { echo '</tr>'; }
}

How can I display 4 TDs in my table then create a new table row using PHP?

I am running a for loop grabbing data from a database and I want to create a new row every time I create 4 table datas, how could I go by doing this?
example:
<table>
<tr><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td></tr>
</table>
Use Modular arithmetic: http://snook.ca/archives/php/the_modulo_oper
(not my site, I googled it for this gentleman.)
Something like that should do the trick.
$i = 0;
echo "<table><tr>";
while ($i < 50) {
echo "<td></td>";
if ($i % 4 == 0) {
echo "</tr><tr>";
}
$i++;
}
echo "</tr></table>";
I let you handle as a homework the case where you end width a $i%4 == 0 so you end the code with </tr><tr></tr></table>
<table>
<tr>
<?php
$i = 0;
foreach ($data as $item)
{
echo "<td>$item</td>";
$i++;
if ($i % 4 == 0)
{
echo "</tr>\n<tr>";
}
}
// if total count of $data is not divisible with 4,
// we have to complete the last row with empty cells
for ($j = 0; $j < (4 - ($i % 4)); $j++)
{
echo "<td> </td>";
}
?>
</tr>
</table>

Categories