Change on every other row - php

I have problem with that the first row misses a <tr> and then there is one line and then there is a </tr> and then it works perfect with one <tr> and two lines and </tr> and so on. How to change the code to make that work?
Any ideas to use "div" instead of "table"?
<table width="300" border="0" cellspacing="2">
<tbody>
<?
$query = mysql_query("SELECT * FROM pages ORDER BY id ASC");
while($r = mysql_fetch_array($query))
{
if(++$s % 2 == 0) { echo '<tr>'."\r\n"; }
echo '<td> · '.$r['domain'].' </td>'."\r\n";
if(++$i % 2 != 0) { echo '</tr>'."\r\n"; }
}
?>
</tbody>
</table>

Use $s++ instead of ++$s, so you test the value before you increment it. And there's no need to use a separate $i variable, you can just use the same $s variable without incrementing it.
$s = 0;
while ($r = mysql_fetch_array($query)) {
if ($s++ % 2 == 0) {
echo "<tr>\r\n";
}
echo '<td> · '.$r['domain'].' </td>'."\r\n";
if ($s % 2 == 0) {
echo "</tr>\r\n";
}
}
// If we ended loop without closing the row, do it now
if ($s % 2 == 0) {
echo "</tr>\r\n";
}

Sounds like you're trying to build a 2-column table? You don't need two counters for that, one will do:
$cells = 0;
while(...) {
if (($cells % 2) == 0) {
echo '<tr>'; // start a new row for cells 0, 2, 4, etc...
}
echo '<td> ....';
if (($cells % 2) == 1) {
echo '</tr>'; // close the row after outputting cells 1,3,5,etc...
}
$cells++;
}

Related

div's with each 6 results in it PHP MYSQL

<div>
<?php if ($result->num_rows > 0) {
$i=1;
while($row = $result->fetch_assoc()) {
if( $i % 6 == 0 )
{ ?>
</div>
<div>
<?php } ?>
<h4><?php echo $row["city"] ?></h4>
<h6><?php echo $row["info"] ?></h6>
<?php $i++;
}
} else {
echo "0 results";
}
?>
</div>
Goal: div's with each 6 rows in it.
When I use $i=1, the first gets 5 results and the other ones get 6.
When I use $i=0 the first one is empty and the other ones get 6.
How to get the first div also filled with 6 results?
Try using array_chunk. That way you don't have to worry where to put your div ends and it's more readable:
$rows = [];
while ($row = $result->fetch_assoc()) {
$rows[] = $row;
}
foreach (array_chunk($rows, 6) as $subset) {
echo "<div>";
foreach ($subset as $row) {
echo "<h4>{$row["city"]}</h4>"
echo "<h6>{$row["info"]}</h6>"
}
echo "</div>";
}
Using array_chunk as proposed by #Justinas is a good way to refactor code. Yet, taking your original code, the issue is about where you check printed amount. It is wrong to first check output amount as it breaks the logic for the first iteration. Please, refer to the code below.
<div>
<?php
if ($result->num_rows > 0) {
$i = 1;
while ($row = $result->fetch_assoc()) {
# if ($i % 6 == 0) {
# echo '</div><div>';
# }
# move commented code from above to ...
echo "<h4>{$row["city"]}</h4>";
echo "<h6>{$row["info"]}</h6>";
# ... here
if ($i % 6 == 0) {
echo '</div><div>';
}
$i++;
}
} else {
echo "0 results";
}
?>
</div>
You can try setting $i = 0 then excluding it in your if statement with
if ($i > 0 && $i % 6 == 0)
since 0 % 6 == 0.

Table with random number value as first cell

I'm working through a snippet of code. The goal is to create a program where a user can enter three values (row, col, and highlight). The user then would then click a button and it would generate a table of 100 values. The first cell must be a random number, the second would be random number + 1. I'm just not sure how to incorporate that into my code. I must use $randnumber = rand(0,100).
Here is what I have so far...
<table border="1">
<?
i = 0;
while($i < 100) {
if($i % 10 == 0) {
echo "<tr>";
}
echo ("<td>".$i."</td>");
$i++;
}
?>
this should do:
<?php $randnumber = rand(0,100); ?>
<table border="1">
<tr>
<?php
$i = 0;
while($i < 100) {
if($i % 10 == 0) {
echo "</tr><tr>";
}
echo ("<td>".($randnumber+$i)."</td>");
$i++;
}
?>
</tr>
</table>

table printing one more cell instead of new line

I am trying to limit the printing of table cells to 3 per row. This worked in one example, but clearly not working when I tried to use the same code somewhere else in the site. This is the code:
$n=3;
echo "<table cellpadding='10' cellspacing='10' style='margin-right:-70px;'><tr>";
$users_count = count($users);
for($i=0; $i<$users_count;$i++)
{
$temp = array();
$temp = $users[$i];
echo "<td>";
echo "<div id='kitchen_box'>";
echo "<div id='kitchen_box_details'>";
echo "<h4>".$temp->fullname . "</h4><br>";
if(strcmp($temp->address, '') == 0)
echo $temp->city;
else
echo $temp->address.", ".$temp->city;
echo "</div>";
echo "<div id='kitchen_box_pic'><img id='kitchen_image' src='".$temp->profilepic."' /></div>";
echo "</div>";
echo "</td>";
if($i != 0){
if($i % $n == 0 && $i != $users_count-1){
echo "</tr><tr>";
}
else{
echo ""; //if it is the last in the loop - do not echo
}
}
}
echo "</table>";
I can't see why this wouldn't work! I would really appreciate support on the matter :)
There are several issues with your code. But your main issue is that you're using a zero-based increment, but doing a 1-based check. So, a table of your the results of an $i!=0 && $i%$n==0 goes like this:
$i $result
0 false
1 false
2 false
3 true
So, you see, the result closes the row after the fourth, not the third cell. To fix this, change the line to:
if($i % $n == $n-1 && $i != $users_count-1){
You should also include a closing </tr> tag with your closing </table> tag.
Incidentally, you shouldn't give the same ID to multiple elements on a page. Each of your kitchen_box and kitchen_box_div DIV tags will have the same ID. If you want this for CSS, use classes. Otherwise, you might try adding the value of $i to each ID.
Nitpicking on request:
The line $temp = array(); seems a little pointless, especially since you don't want $temp to be an array, but an object.
The else{ echo ""; } lines are also redundant.
You don't need the if($i != 0) check now because that case will not pass the next test anymore.
Otherwise the code seems fine to me.
I think the problem is that your $i starts at zero.
Let's look at your conditions before creating a new row :
if($i != 0){
if($i % $n == 0 && $i != $users_count-1)
If $i = 0, the first condition isn't matched. Then the second isn't for $i = 1 and $i = 2. And then your script creates another ... in your first row before matching your if conditions for the first time.
I guess you could move this part of the code right after the beginning of the for instructions :
for($i=0; $i<$users_count;$i++)
{
if($i != 0)
{
if($i % $n == 0 && $i != $users_count-1){
echo "</tr><tr>";
}
else{
echo ""; //if it is the last in the loop - do not echo
}
}
// Echo your <td> ... </td>
}
echo "</tr></table>";

Mysql fetch array, table results

i'm pretty new to this and not sure how should i do it,
I've got a database with a column called "names"
how can i make it display in so?
<tr>
<td width="270px">Henry</td>
<td width="270px">Jeffrey</td>
<td width="270px">Hansel</td>
</tr>
<tr>
<td width="270px">Michelle</td>
<td width="270px">Jackson</td>
<td width="270px">Ivan</td>
</tr>
I only know how i should do it if the records goes one after another vertically.
$result = mysql_query('SELECT * FROM member');
while($row = mysql_fetch_array($result))
{
echo'
<tr>
<td width="270px">'.$row['names'].'</td>
<td width="270px">Jeffrey</td>
<td width="270px">Hansel</td>
</tr>';
Kinda stucked here...
Sorry i forgot to put this.
what i want is that it should loop the tags along. So i can have a 3 column table with infinite rows.
what bout this one? What if i want this to loop instead?
<tr>
<td><img src="images/ava/A.png" /></td>
<td>A</td>
<td width="2px" rowspan="3"></td>
<td><img src="images/ava/B.png" /></td>
<td>B</td>
</tr>
<tr>
<td>A</td>
<td>B</td>
</tr>
$row will update on each iteration of the loop:
$result = mysql_query('SELECT * FROM member');
echo '<tr>';
for($i = 0; $row = mysql_fetch_array($result); $i = ($i+1)%3){
echo '<td width="270px">'.$row['names'].'</td>';
if($i == 2)
echo '</tr><tr>';
}
echo '</tr>';
$result = mysql_query('SELECT * FROM member');
echo'<tr>'
while($row = mysql_fetch_array($result))
{
echo '<td width="270px">'.$row['names'].'</td>';
}
echo '</tr>';
$result = mysql_query('SELECT * FROM member');
echo '<tr>;
while($row = mysql_fetch_array($result))
{
echo '<td width="270px">'.$row['names'].'</td>';
}
echo '</tr>';
Lets say you have an array of the names called $names, then you could do what you wanted with code that looks something like this:
<tr>
<?php
foreach($names as $name) {
print "<td>$name</td>";
}
?>
</tr>
That would put all the names on the same row.
In order to start a new row say, every 3 names, you could put an if statement in the for loop like this:
// assume we have these variables available.
$row_number;
$max_cols = 3;
// this goes at the top of the foreach
if($row_number % $max_cols == 0) {
print '</tr><tr>';
}
You know accomplish this by assigning the row count before you start the variable and few flags to know initialized loop or started/ended loop.
$i = 1;
$initFlag = false;
$flag = "closed";
while($row = mysql_fetch_array($result)) {
if($i%3 == 0) $initFlag = false;
if($flag == "closed" && ($i == 1 || $i % 3 == 1)) {
echo "<tr>"; $flag = "started"; $initFlag = true;
}
echo '<td width="270px">'.$row['names'].'</td>';
if(!initFlag && $flag == "started" && $i % 3 ==0) {
echo "</tr>"; $flag = "closed";
}
$i++;
}
So most of these answers are going for the kludge. First, if all you want is the name in your resultset, then only ask for it. So rather than going with:
$result = mysql_query('SELECT * FROM member');
Instead go with:
$result = mysql_query('SELECT names FROM member');
Also, everyone seems to be ignoring the three column request, and that can be satisfied using a modulo to tell when to break the rows. We'll do a little magic to make sure you always close the row tag.
$row_count = 0;
while($row = mysql_fetch_array($result))
{
if( $row_count % 3 == 0 )
{
echo '<tr>';
}
echo '<td width="270px">'.$row['names'].'</td>';
if( $row_count % 3 == 0 )
{
echo '</tr>';
}
$row_count++;
}
I don't want to get too picky about your schema, but if you have a choice, it's better to name the table a plural like members rather than member, since you have a collection of rows, each one representing one 'member'. And unless your members have multiple names that column could probably best be called 'name' instead.

Merging Code with Display on Loop

Trying to create DIV containers out of a mysql results which each hold chunks of 10.
$balloon_count = the amount of records each div should hold.
$ui = the loop counter.
Functionality is simple, don't want to a template engine.
Trying to use the MODULUS operator to simplify the div cuts.
Doesn't work. Any direction is greatly appreciated.
Sample Code
$ui=1;
$balloon_holds = 10;
while($row = mysql_fetch_array($result))
{
if($ui==1||$ui%$balloon_holds != 0)
{
echo '<div><table style="width:400px;border:2px solid gray;border-style:dashed;"><tr>';
echo "<td style=\"font-size:small;vertical-align:text-top;\">";
}
echo '<input disabled type="checkbox" value="$row[id]"'; $this->ischecked($uid,$row[id]); echo "/>".$row['name'].'<br>'."\r\n";
if($ui==10||$ui%$balloon_holds != 0){
echo '</td></tr></table></div>';
}
$ui++;
}
Sample Expected "HTML" Output
<div><table style="width:400px;border:2px solid gray;border-style:dashed;"><tr>
<td style="font-size:small;vertical-align:text-top;">
Record1
Record2
Record3
Record4
Record5
Record6
Record7
Record8
Record9
Record10
</td></tr></table></div>
<div><table style="width:400px;border:2px solid gray;border-style:dashed;"><tr>
<td style="font-size:small;vertical-align:text-top;">
Record11
Record12
Record13
Record14
Record15
Record16
Record17
Record18
Record19
Record20
</td></tr></table></div>
If I understand the question correctly, you want the <div> opener on $ui = 1,11,21,31,... and the </div> closer on 10,20,30,40,...
If this is correct, your modulus operators should be changed as such:
if($ui%$balloon_holds == 1)
{
...
}
...
if($ui%$balloon_holds == 0)
{
...
}
if($ui==1||$ui%$balloon_holds != 0)
This will always be true except for when $ui is a multiple of 10, so you'd be outputting your header/footer blocks for ALL rows except 0, 10, 20, etc... and the one case where $ui is 1.
Most like likely this would be easier to understand:
$row_cnt = 0;
$max_rows = 10;
while($row = ...) {
if ($row_cnt == 0) {
// output header
}
// output row data
if ($row_cnt == 9) {
// output row footer
}
$row_cnt++;
$row_cnt %= $max_rows; // ($row_cnt resets to 0 when it reaches 10)
}
Here is my suggestion, it's also a little more readable / maintainable.
$ui=0;
$balloon_holds = 10;
while ($row = mysql_fetch_array($result))
{
if ($ui % $balloon_holds)
{
if ($ui >= 10)
{
echo '</td></tr></table></div>';
}
echo '<div><table style="width:400px;border:2px solid gray;border-style:dashed;"><tr>';
echo "<td style=\"font-size:small;vertical-align:text-top;\">";
}
echo '<input disabled type="checkbox" value="$row[id]"';
$this->ischecked($uid,$row[id]);
echo "/>".$row['name'].'<br>'."\r\n";
$ui++;
}
if ($ui > 0)
{
echo '</td></tr></table></div>';
}
$ui=0;
$balloon_holds = 10;
while($row = mysql_fetch_array($result))
{
$exit = 0;
if($ui==1||$ui%$balloon_holds != 0)
{
echo '<div><table style="width:400px;border:2px solid gray;border-style:dashed;"><tr>';
echo "<td style=\"font-size:small;vertical-align:text-top;\">";
}
echo '<input disabled type="checkbox" value="$row[id]"'; $this->ischecked($uid,$row[id]); echo "/>".$row['name'].'<br>'."\r\n";
$ui++;
if($ui%$balloon_holds == 0){
echo '</td></tr></table></div>';
$exit = 1;
}
}
if($exit == false){
echo '</td></tr></table></div>';
}

Categories