Second row from MySql Database - php

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

Related

Is there any idea on how to print a pattern only if the user inputted the number of row and column?

I don't have any idea if how I am going to print a pattern, only if I input the number of row and column, help me guys, send your code
<?php
if (isset($_GET['row']) && $_GET['column']) {
$row = $_GET['row'];
$column = $_GET['column'];
$output ="*";
for ($i=$_GET['row']; $i <=1 ; $i++) {
for ($j=$i; $j <=$_GET['column']; $j++) {
$count = $i;
if ($i == $j) {
echo $count. " ";
}else{
echo $count +=4;
} echo "<br>";
}
}
}
?>
here is what I want to have
Enter Row : 5
Enter Column : 4
Output:
*****
*****
*****
*****
You can try the following way with the help of array_fill() and implode(). The array_fill() function fills an array with values of * here and implode() join the stars to a single string.
// $_GET = ['row' => 5, 'column' => 4];
if (isset($_GET['row'], $_GET['column']) && $_GET['row'] > 0 && $_GET['column'] > 0) {
for($i = 0; $i < $_GET['row']; $i++) {
echo implode('', array_fill(0, $_GET['column'], '*')) . PHP_EOL;
}
}
Working demo.

create the letter T with PHP code

I need to create the Letter T with PHP code:
This is what I have so far but can't seem to figure how to just have the asterisks on the top two lines in order to extend the top of the T:
<?php
echo "<pre>";
for ($row > 2; $row < 15; $row++) {
for ($column = 2; $column < 12; $column++) {
if (($row < 2 || $row < 2) || ($column < 2 || $column >= 6)) {
echo "*";
}
else echo " ";
}
echo "\n";
}
echo "</pre>";
?>
You have several bugs in your code.
for ($row > 2; $row < 15; $row++) {
for ($column = 2; $column < 12; $column++) {
why do you use $row > 2 and $column = 2 ? Just start from zero.
if (($row < 2 || $row < 2) || ($column < 2 || $column >= 6)) {
Why do you check if $row < 2 is true or $row < 2 is true if they are the same?
Here is an example:
echo "<pre>";
for($i=0; $i <= 10; $i++){
for($j = 0; $j < 10; $j++){
if($i > 2 && ($j < 3 || $j > 6)){
echo " ";
}else{
echo "*";
}
}
echo "\n";
}
for ($row > 2; $row < 15; $row++) {
This condition is wrong, and should be:
for ($row = 0; $row < 15; $row++) {
And:
if (($row < 2 || $row < 2)
is wrong and doesn't do what you probably think it does.
The code in the thread j08691 linked you to, contains the correct solution and you could use that:
<?php
echo "<pre>";
for ($row = 0; $row < 15; $row++) {
for ($column = 0; $column <10; $column++) {
if (($row < 1 || $row > 15) ||( $column == 4)) {
echo "*";
}
else echo " ";
}
echo "\n";
}
echo "</pre>";
?>
See the live demo.
start your for loop with $row = 0
for ($row = 0; $row < 15; $row++) {
you were ignoring the first 2 lines and only drawing the vertical line
Also ($row < 2 || $row < 2) is the same as $row < 2
#j08691 found your exact question if you want more info

How do I simplify this basic loop?

I have code which pretty much does this.....
//get the row info
$Row1 = $FullTable->find('div[class=ismPitchRow1]',0);
$Row2 = $FullTable->find('div[class=ismPitchRow2]',0);
$Row3 = $FullTable->find('div[class=ismPitchRow3]',0);
$Row4 = $FullTable->find('div[class=ismPitchRow4]',0);
$Row5 = $FullTable->find('div[class=ismPitchRow5]',0);
//Loop 5 times. One for each row on the pitch.
for ($i=1; $i<=5; $i++)
{
if ($i = 1) { echo $Row1; }
if ($i = 2) { echo $Row2; }
if ($i = 3) { echo $Row3; }
if ($i = 4) { echo $Row4; }
if ($i = 5) { echo $Row5; }
}
It works, but as you can see it's not very efficient and badly designed. How would I simplify this? I know there are much smaller ways that these kind of loops can be done.
Thanks.
use the great invention of arrays:
//get the row info
$Row[1] = $FullTable->find('div[class=ismPitchRow1]',0);
$Row[2] = $FullTable->find('div[class=ismPitchRow2]',0);
$Row[3] = $FullTable->find('div[class=ismPitchRow3]',0);
or, even more clever...
for ($i = 1; $i <= 5; $i++) {
$find = "div[class=ismPitchRow$i]";
$Row[$i] = $FullTable->find($find,0);
}
do the same for echoing:
for ($i = 1; $i <= 5; $i++) {
echo $Row[$i];
}
but why not do everything in 1 loop?
for ($i = 1; $i <= 5; $i++) {
$find = "div[class=ismPitchRow$i]";
echo $FullTable->find($find,0);
}
I would store $Row1 through $Row5 in an array and iterate through it with a foreach loop.
<?php
$YourArray = array();
array_push($YourArray,$FullTable->find('div[class=ismPitchRow1]',0),$FullTable->find('div[class=ismPitchRow2]',0),$FullTable->find('div[class=ismPitchRow3]',0),$FullTable->find('div[class=ismPitchRow4]',0),$FullTable->find('div[class=ismPitchRow5]',0));
foreach($YourArray as $row){
echo $row;
}
?>

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>'; }
}

Insert tr after every third loop

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>

Categories