Okay, what I want is to display retrieved data in a table form database, I want my table to be limit in 5 data per table. I need the tables to be display horizontally.
Like this:
while($row1 = sqlFetchArray($row))
{ $ctr = 0;
$ctr+1;
if($ctr=1 || $ctr<=5)
{
$html .='<table width="100px" style="float:left;"><tr><td>'.$row1['id'].'</td></tr></table>';
}
if($ctr=6 || $ctr<=10)
{
$html .='<table width="110px" style="float:left;"><tr><td>'.$row1['id'].'</td></tr></table>';
}
if($ctr=11 || $ctr<=15)
{
$html .='<table width="120px" style="float:left;"><tr><td>'.$row1['id'].'</td></tr></table>';
}
}
Output:
1 6
2 7
3 8
4 9
5 10
This should work for you:
(But the function sqlFetchArray as to work like mysql_fetch_array)
<?php
$limit = 15;
for($numberCounter = 0; $numberCounter < $numberCount = mysql_num_rows($row); $numberCounter++) {
$count = 0;
if($numberCounter >= mysql_num_rows($row))
break;
if ($count == 0 || $count % $limit == 0)
echo "<table width='100px' style='float:left;'>";
while ($row1 = sqlFetchArray($row) && $count < $limit) {
if($numberCounter >= mysql_num_rows($row))
break;
echo "<tr><td>" . $row1[$numberCounter] . "</td></tr>";
$count++;
$numberCounter++;
}
if($count == 0 || $count % $limit == 0)
echo "</table>";
}
?>
As an example:
<?php
$test = range(1, 43);
$limit = 15;
for($numberCounter = 0; $numberCounter < $numberCount = count($test); $numberCounter++) {
$count = 0;
if($numberCounter >= count($test))
break;
if ($count == 0 || $count % $limit == 0)
echo "<table width='100px' style='float:left;'>";
while ($count < $limit) {
if($numberCounter >= count($test))
break;
echo "<tr><td>" . $test[$numberCounter] . "</td></tr>";
$count++;
$numberCounter++;
}
if($count == 0 || $count % $limit == 0)
echo "</table>";
}
?>
Related
hii i'm trying to improve logic in php where i'm trying to print star pattern in A shape,B shape and K shape but it isnt working can anyone help me with my logic my code is as follows
k.php
<?php
$j = 5;
$i = 0;
for ($row=0; $row<=7; $row++)
{
for ($column=0; $column<=7; $column++)
{
if ($column == 1 or (($row == $column + 1) and $column != 0))
echo "*";
else if ($row == $i and $column == $j)
{
echo "*";
$i=$i+1;
$j=$j-1;
}
else
echo " ";
}
echo "<br>";
}
?>
B.php
<?php
for ($row=0; $row<7; $row++)
{
for ($column=0; $column<=7; $column++)
{
if ($column == 1 or (($row == 0 or $row == 3 or $row == 6) and ($column < 5 and $column > 2)) or ($column == 5 and ($row != 0 and $row != 3 and $row != 6)))
echo "*";
else
echo " ";
}
echo "<br>";
}
?>
A.php
<?php
for ($row=0; $row<=7; $row++)
{
for ($column=0; $column<=7; $column++)
{
if ((($column == 1 or $column == 5) and $row != 0) or (($row == 0 or $row == 3) and ($column > 1 and $column < 5)))
echo "*";
else
echo " ";
}
echo "<br>";
}
?>
my code isnt working as expected please anyone help
You can try this code:
<pre><?php echo getK();?></pre>
<pre><?php echo getA();?></pre>
<pre><?php echo getB();?></pre>
<?php
function getK()
{
$j = 5;
$i = 0;
for ($row=0; $row<=7; $row++)
{
for ($column=0; $column<=7; $column++)
{
if ($column == 1 or (($row == $column + 1) and $column != 0))
echo "*";
else if ($row == $i and $column == $j)
{
echo "*";
$i=$i+1;
$j=$j-1;
}
else
echo " ";
}
echo "\n";
}
}
function getA()
{
for ($row=0; $row<=7; $row++)
{
for ($column=0; $column<=7; $column++)
{
if ((($column == 1 or $column == 5) and $row != 0) or (($row == 0 or $row == 3) and ($column > 1 and $column < 5)))
echo "*";
else
echo " ";
}
echo "\n";
}
}
function getB()
{
for ($row=0; $row<7; $row++)
{
for ($column=0; $column<=7; $column++)
{
if ($column == 1 or (($row == 0 or $row == 3 or $row == 6) and ($column < 5 and $column > 2)) or ($column == 5 and ($row != 0 and $row != 3 and $row != 6)))
echo "*";
else
echo " ";
}
echo "<br>";
}
}
One more answer:
legend:
n = "new line (\n)"
* = letter (you can use, bettor for you)
space = space)))
for example: "a" => " *** n
* *n
* *n
*****n
* *n
* *n
* *",
Code:
<?php
$letters = [
"a" => " *** n* *n* *n*****n* *n* *n* *",
"b" => "**** n* *n* *n****n* *n* *n**** ",
"k" => "* *n* *n* *n**n* *n* *n* *",
];
function printLetter($pattern)
{
$len = strlen($pattern);
for ($i = 0; $i < $len; $i++) {
if ($pattern[$i] == 'n') {
echo "\n";
continue;
}
echo $pattern[$i];
}
}
?>
<pre><?php printLetter($letters['a']); ?></pre>
<pre><?php printLetter($letters['b']); ?></pre>
<pre><?php printLetter($letters['k']); ?></pre>
I am having difficulties with my MYSQL / PHP dashboard. - Currently i am having 50 pages, but currently they are all showing on the same page.
http://imgur.com/wDfTWUa - as you can see in the attached file. - I only want 10 pages to be shown, and be able to click through the rest of the pages without seeing 4 rows of pages.
Exsampel <- 2 3 4 5 6 7 8 9 10 -> when you are on ?page=1, if you are on page ?page=10 <- 11 12 13 14 15 16 17 18 19->
Hope you can help me.
Code:
<?php
include 'config.php';
$sidenr = $_GET['page'];
$sidenr2 = ($sidenr -1) * 10;
echo $sidenr2;
echo "<br><br>";
$query100 = mysqli_query($conn, "SELECT * FROM `test` LIMIT $sidenr2,10") or die(mysqli_error($conn));
while($row = mysqli_fetch_array($query100))
{
echo $row['id']."<br>";
}
$result = mysqli_query($conn, "SELECT * FROM test");
$num_rows = mysqli_num_rows($result);
$sideantal = $num_rows / 10;
echo "Der skal være antal sider: ". $sideantal;
echo "<br><br>antal rækker ". $num_rows . "<br><br>";
?>
<br><br>
<?php
for ($number = 1; $number <= $sideantal; $number++) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a></li>";
}
?>
function getPageRange($current, $max, $total_pages = 10) {
$desired_pages = $max < $total_pages ? $max : $total_pages;
$middle = ceil($desired_pages/2);
if ($current <= $middle){
return [1, $desired_pages];
}
if ($current > $middle && $current <= ($max - $middle)) {
return [
$current - $middle,
$current + $middle
];
}
if ($current <= $max ) {
return [
$current - ($desired_pages - 1),
$max
];
}
}
list($min,$max) = getPageRange($sidenr, $sideantal);
foreach (range($min, $max) as $number) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a></li>";
}
try to change this:
for ($number = 1; $number <= $sideantal; $number++) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a>
</li>";
}
to this:
for ($number = 1; $number <= $sideantal; $number++) {
if (($number > $_GET['page']) && ($number <= $_GET['page'] + 10)) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a>
</li>";
}
}
You can try the following code:
for ($number = 1; $number <= $sideantal; $number++) {
/** If the loop count is greater than the current page but less than current page plus 10 */
if ( ($number > $_GET['page'] && ($number < ($_GET['page'] + 10)))) $is_valid = true;
/** If the loop count is less than the current page but greater than current page -10 and the current page is the last page */
if ($number < $_GET['page'] && $_GET['page'] == $sideantal && $number > ($_GET['page'] - 10)) $is_valid = true;
else $is_valid = false;
if ($is_valid) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a></li>";
}
}
I am showing images which are stored in my DB.
See code below
$num_rows = mysql_num_rows($result41);
for ($i = 1; $i <= mysql_num_rows($result41); $i++)
{
$row = mysql_fetch_array($result41);
$upload_id = $row ['upload_id'];
$file = $row ['FILE_NAME'];
echo"
<td>
<a href='image.php?id=$upload_id&gallery=$id'><center>
<img src='uploads/$file' alt='$name Gallery' title='$name Album' class='resize'>
</td>";
}
if ($i % 0 == 4) {
echo '</tr>'; // it's time to move to next row
}
My question is, after showing 4 columns how do I move onto another row? (only 4 images per row)
I have if ($i % 0 == 4) in my script but doesn't seem to be working?
Thanks
Like this :
for ($i = 1; $i <= mysql_num_rows($result41); $i++)
{
$row = mysql_fetch_array($result41);
$upload_id = $row ['upload_id'];
$file = $row ['FILE_NAME'];
if($i % 4 == 0) echo "<tr>";
echo"
<td>
<a href='image.php?id=$upload_id&gallery=$id'><center>
<img src='uploads/$file' alt='$name Gallery' title='$name Album' class='resize'>
</td>";
if($i % 4 == 3) echo "</tr>";
}
And look at PHP PDO for your Mysql access into Google ;)
And start you loop (for) at 0 ?
This is how I would do it:
$num_rows = mysql_num_rows($result41);
$k = 1;
for ($i = 1; $i <= mysql_num_rows($result41); $i++){
$row = mysql_fetch_array($result41);
$upload_id = $row ['upload_id'];
$file = $row ['FILE_NAME'];
echo"
<td>
<a href='image.php?id=$upload_id&gallery=$id'><center>
<img src='uploads/$file' alt='$name Gallery' title='$name Album' class='resize'>
</td>";
if($k == 4){
$k = 1
echo "</tr><tr>";
} else {
$k++;
}
}
The $k variable counts to 4 and resets every time.
While it resets, it also ends and creates a new row in the table.
I'm new to PHP and trying to create the following whilst minimizing the amount of code needed. PHP should show a list of 100 then display if the number is / by 3, 5 or 3 and 5. If not by any then show nothing.
This is what I've got so far, but any help would be great since not sure about the / by 3 and 5 bit as you can see below.
<?php $var = range(0, 100); ?>
<table>
<?php foreach ($var as &$number) {
echo " <tr>
<td>$number</td>
<td>";
if($number % 3 == 0) {
echo "BY3";
} elseif ($number % 5 == 0) {
echo "BY5";
} elseif ($number % 3 and 5 == 0) {
echo "BY3 AND 5";
}
echo "</td></tr>";
}
?>
</table>
Thanks
Nope... you should check first if it's divisble for 15 (3x5) (or 3 and 5) and after you can do other checks:
if($number % 15 == 0) {
echo "BY3 AND 5";
} elseif ($number % 5 == 0) {
echo "BY5";
} elseif ($number % 3 == 0) {
echo "BY3";
}
echo "</td></tr>";
?>
Because every number divisble for 15 is also divisble for 3 and 5. So your last check could never hit
if I'm reading your question correct then you are looking for :
if ($number % 3 == 0 && $number %5 == 0) {
echo "BY3 AND 5";
} elseif ($number % 3 == 0) {
echo "BY3";
} elseif ($number % 5 == 0) {
echo "BY5";
}
Alternative version :
echo ($number % 3 ? ($number % 5 ? "BY3 and 5" : "BY 3") : ($number % 5 ? "BY 5" : ""));
$num_count = 100;
$div_3 = "Divisible by 3";
$div_5 = "Divisible by 5";
$div_both = "Divisible by 3 and 5";
$not_div = "Not Divisible by 3 or 5";
for($i=0;$i<=$num_count;$i++)
{
switch($i)
{
case ($i%15==0):
echo $i." (".$div_both.")</br>";
break;
case ($i%3==0):
echo $i." (".$div_3.")</br>";
break;
case ($i%5==0):
echo $i." (".$div_5.")</br>";
break;
default:
echo $i."</br>";
break;
}
}
No need to do three if statements:
echo "<table border='1'>";
for ($i = 1; $i <= 100; $i++) {
echo "<tr><td>{$i}</td><td>";
if ($i % 3 == 0) echo "BY3 ";
if ($i % 5 == 0) echo "BY5";
echo "</td></tr>\n";
}
echo "</table>";
Update the code as given below
<?php $var = range(0, 100); ?>
<table>
<?php foreach ($var as &$number)
{
echo " <tr>
<td>$number</td>
<td>";
if($number % 3 == 0 && $number % 5 == 0)
{
echo "BY3 AND 5";
}
elseif ($number % 5 == 0)
{
echo "BY5";
}
elseif ($number % 3 == 0)
{
echo "BY3";
}
echo "</td></tr>";
}
?>
<?php
if($number % 5 == 0 && $number % 3 == 0) {
echo "BY3 AND 5";
} elseif ($number % 5 == 0) {
echo "BY5";
} elseif ($number % 3 == 0) {
echo "BY3";
} else{
echo "NOT BY3 OR 5";
}
?>
if($number % 15 == 0)
{
echo "Divisible by 3 and 5";
}
elseif ($number % 5 == 0)
{
echo "Divisible by 5";
}
elseif ($number % 3 == 0)
{
echo "Divisible by 3";
}
This is neater and completed to be run:
<?php
for ($i = 1; $i <= 100; $i++) {
if ($i % 15 == 0)
{
echo"Divisible by 3 and 5</br>";
}
elseif ($i%3==0)
{
echo"Divisible by 3</br>";
}
elseif ($i%5==0)
{
echo"Divisible by 5</br>";
}
else
{
echo $i,"</br>";
}
}
?>
<?php
for ($i = 1; $i <= 100; $i++) {
if ($i % 15 == 0) echo "This Number is Divisible by 3 and 5<br>";
else if ($i % 3 == 0) echo "This Number is Divisible by 3 only<br>";
else if ($i % 5 == 0) echo "This number is Divisible by 5 only<br>";
else{
echo "$i<br>";
}
}
?>
Pretty much I have a code like below that I'm trying to add </tr><tr> to after every 6 results.
echo "<table><tr>";
$query="SELECT * WHERE id='$id' ORDER BY date ASC";
$result=mysql_query($query);
if (mysql_num_rows($result) > 0) {
while($rts = mysql_fetch_array($result)){
$cdata1 = $rts['cdata1'];
$cdata2 = $rts['cdata2'];
echo "<td>$cdata1 and $cdata2</td>";
}
}else{
echo "<td>no results</td>";
}
echo "</tr></table>";
echo "<table><tr>";
$query="SELECT * WHERE id='$id' ORDER BY date ASC";
$result=mysql_query($query);
$i = 0;
if (mysql_num_rows($result) > 0) {
while($rts = mysql_fetch_array($result)){
$cdata1 = $rts['cdata1'];
$cdata2 = $rts['cdata2'];
echo "<td>$cdata1 and $cdata2</td>";
if(++$i % 6 == 0) {
echo '</tr><tr>';
}
}
}else{
echo "<td>no results</td>";
}
echo "</tr></table>";
UPD:
Whats means if(++$i % 6 == 0) code:
++$i equals $i = $i + 1;
$i % 6 means $i modulo 6
If $i modulo 6 equals 0 then echo </tr><tr>
So we can write it as:
$i = $i + 1;
if($i % 6 == 0) {
echo '</tr><tr>';
}
http://php.net/manual/en/internals2.opcodes.mod.php
http://php.net/manual/en/language.operators.increment.php