output numbers bold except some - php

How to produce the following output? All numbers should be bold except 10, 20, 30 and 40.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
My current code is:
<?php
$i = 1;
while($i <= 40) {
$m = ($i % 1);
if($m == 0) {
echo '<b><u>' . $i . '</b></u>';
}
$i++;
}
?>

Simple one:
<?php
for ($i=1;$i<=40;$i++){
if ($i % 10 == 0){
$result .= $i;
}
else{
$result .= "<b>".$i."</b>";
}
}
echo $result;
?>
Update 1:
If your logic is to be corrected then,
<?php
$i = 1;
while($i <= 40) {
$m = ($i % 10); // have to replace 1 by 10
if($m == 0) {
echo $i;
}
else{
echo '<b><u>' . $i . '</b></u>';
}
$i++;
}
?>
You can merge the if ($i%10 == 0) into single statement as well.
<?php
$i=1;
while($i<=40)
{
if ($i%10 == 0){
echo $i;
}
else{
echo '<b><u>'.$i.'</b></u>';
}
$i++;
}
?>

one small Correction From 1st answer #Fakhruddin Ujjainwala
Undefined variable: result
<?php
$result = "";
for ($i=1;$i<=40;$i++){
if ($i % 10 == 0){
$result .= $i;
}
else{
$result .= "<b>".$i."</b>";
}
}
echo $result;
?>

Related

Limit pages numbers on PHP pagination

I have a pagination script with PHP. When page records are some hundreds, the pagination result is too big. How I can limit the page numbers/links?
Example: < 1 | 2 ... 37 | 38 | 39 | 40 | 41 | 42 ... 82 | 83 >
This is my PHP script
<?php
$ppp = 10;
$rows = mysql_num_rows($query);
$nmpages = ceil($rows/$ppp);
// if current page is not 1, draw PREVIOUS link
if ($pg > 1 && $nmpages != 0) {
echo "< ";
}
For($i = 1 ; $i <= $nmpages ; $i++) {
If($i == $pg) {
echo "<b>".$i."</b> ";
} else {
echo "".$i." ";
}
}
// if current page less than max pages, draw NEXT link
if ($pg < $nmpages && $nmpages != 0) {
echo ">";
}
?>
Do you have an ideas how I can do this with the specific PHP script that I have?
Try this :
<?php
$link = "";
$page = $_GET['pg']; // your current page
// $pages=20; // Total number of pages
$limit=5 ; // May be what you are looking for
if ($pages >=1 && $page <= $pages)
{
$counter = 1;
$link = "";
if ($page > ($limit/2))
{ $link .= "1 ... ";}
for ($x=$page; $x<=$pages;$x++)
{
if($counter < $limit)
$link .= "".$x." ";
$counter++;
}
if ($page < $pages - ($limit/2))
{ $link .= "... " . "".$pages." "; }
}
echo $link;
?>
OUTPUT :
//At page=1
1 2 3 4 ... 20
//At page=12
1 ... 12 13 14 15 ... 20
//At page=18
1 ... 18 19 20
An improvement or rather re-write based on #Makesh's code.
function get_pagination_links($current_page, $total_pages, $url)
{
$links = "";
if ($total_pages >= 1 && $current_page <= $total_pages) {
$links .= "1";
$i = max(2, $current_page - 5);
if ($i > 2)
$links .= " ... ";
for (; $i < min($current_page + 6, $total_pages); $i++) {
$links .= "{$i}";
}
if ($i != $total_pages)
$links .= " ... ";
$links .= "{$total_pages}";
}
return $links;
}
OUTPUT:
page = 1
1 2 3 4 5 6 ... 20
page = 10
1 ... 5 6 7 8 9 10 11 12 13 14 15 ... 20
page = 19
1 ... 14 15 16 17 18 19 20
The answer for this question was basically to visit a page about Digg style pagination which includes code samples.
So that's the answer, but this question is basically a duplicate.
Try to make a page bracket for example 10 less and 10 more than the actual page, change for example the for statement for this:
For($i = $pg-10 ; $i <= $pg+10 ; $i++)
I wanted to get an array of numbers by the current page and total page. So this is what I came up with. I hope this helps others -
Caution - this work if total page is more than 10. I felt if the total
page is less than 10 then just show it in a single for loop.
function getSmartPageNumbers($currentPage, $totalPage)
{
$pageNumbers = [];
$diff = 2;
$firstChunk = [1, 2, 3];
$lastChunk = [$totalPage - 2, $totalPage - 1, $totalPage];
if ($currentPage < $totalPage) {
$loopStartAt = $currentPage - $diff;
if ($loopStartAt < 1) {
$loopStartAt = 1;
}
$loopEndAt = $loopStartAt + ($diff * 2);
if ($loopEndAt > $totalPage) {
$loopEndAt = $totalPage;
$loopStartAt = $loopEndAt - ($diff * 2);
}
if (!in_array($loopStartAt, $firstChunk)) {
foreach ($firstChunk as $i) {
$pageNumbers[] = $i;
}
$pageNumbers[] = '.';
}
for ($i = $loopStartAt; $i <= $loopEndAt; $i++) {
$pageNumbers[] = $i;
}
if (!in_array($loopEndAt, $lastChunk)) {
$pageNumbers[] = '.';
foreach ($lastChunk as $i) {
$pageNumbers[] = $i;
}
}
}
return $pageNumbers;
}
Test:
getSmartPageNumbers(8, 20);
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => .
[4] => 6
[5] => 7
[6] => 8
[7] => 9
[8] => 10
[9] => .
[10] => 18
[11] => 19
[12] => 20
)
getSmartPageNumbers(1, 20);
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => .
[6] => 18
[7] => 19
[8] => 20
)

To print 1 to 25

Expected output:
1 2 3 4 5
10 9 8 7 6
11 12 13 14 15
20 19 18 17 16
21 22 23 24 25
Tried following code:
for($i=1;$i<=25;$i++)
{
if($i%5 ==0)
{
echo $i;
echo "<br>";
for($j=($i+5);$j>$i;$j--)
{
echo $j;
}
echo "<br>";
}
else if ($i%5!=0)
{
echo $i;
}
}
for($i=1;$i<=25;$i++)
{
if($i%5 ==0)
{
echo $i;
echo "<br>";
for($j=($i+5);$j>$i;$j--)
{
echo $j;
}
$i = $i+5;
echo "<br>";
}
else if ($i%5!=0)
{
echo $i;
}
}

Matrix formation, there is something wrong in the loops

I am trying to get this kind of matrix:
1 0 0 0 5
0 2 0 4 0
0 0 3 0 0
0 2 0 4 0
1 0 0 0 5
and here is the code:
$n = 5;
$flag = 0;
for($i=1; $i<=$n; $i++){
for($j=1; $j<=$n; $j++){
if($i == $j){
echo "$i ";
}else{
echo "0 ";
}
if($j == $n - $flag){
echo $n - $flag." ";
$flag++;
}
}
echo "</br>";
}
the output is:
1 0 0 0 0 5
0 2 0 0 4 0
0 0 3 3 0 0
0 0 2 0 4 0
0 1 0 0 0 5
there is something wrong in the middle. I think it is because two for loops overlap there.
How to fix thiis?
Try this code to output the array you want:
$n = 6;
for($i=1; $i<$n; $i++){
for($j=1; $j<$n; $j++){
if($i == $j){
echo "$i ";
} else if ($j == $n - $i) {
echo $n - $i ." ";
} else {
echo "0 ";
}
}
echo "</br>";
}
The reason isn't an overlap in the loops. Its an overlap of the two if statements.
In the inner loop, you always print 6 things.
for($j=1; $j<=$n; $j++){
if($i == $j){ // <--- This will always print in each iteraton (i.e 5 times)
echo "$i ";
}else{
echo "0 ";
}
if($j == $n - $flag){ // <--- This will print once in the loop (the 6th extra)
echo $n - $flag." ";
$flag++;
}
}
Since $j is equal to $flag, you don't need to keep track of it.
Change it to this (Check it on ideone):
for($j=1; $j<=$n; $j++){
if($i == $j){
echo "$i ";
} elseif($j == $n - $i + 1){
echo $n - ($i-1)." ";
} else {
echo "0 ";
}
}

If condition from database

I am getting $final value from database and displaying in a table according to user. Now I have a value i.e., this is I was showing in the frontend
Userid Total Letter
36 45
5 67
78 90
42 82
12 57
Back End Code
$sql = mysql_query('SELECT userid, total FROM history');
echo '<table><th>Userid</th>
<th>Total</th>
<th>Grade</th>
while($row = mysql_fetch_array($sql))
{
echo '<tr><td>'.$row['userid'].'</td>
<td>'.$row['$total'].'</td>
$letter = mysql_query('SELECT score,letter FROM letter');
while($row = mysql_fetch_array($letter))
{
$score= $row['score'];
$letterp= $row['letter'];
switch($row['$total'])
{
case $row['$total'] == $score;
echo '<td>'.$letterp.'</td>';
break;
case $row['$total'] >= $score;
echo '<td>'.$letterp.'</td>';
break;
}
</tr>
}
I have a letter table
ID SCORE letter
1 100 A+
2 90 A-
3 80 B+
4 73 B-
5 65 C
6 55 D
7 45 E
8 0 F
Switch case is a sample I wrote here. I need to give condition like if userid scores 45 then I need to show letter E and if userid scores 67 then I need to show B-(b'coz the scorefield defines 65 to 54 is B-) . This is the exact requirement.
Something like this should do:
SELECT
CASE score
WHEN = 100 THEN 'A+'
WHEN >= 90 and < 100 THEN 'A-'
WHEN >= 80 and < 89 THEN 'B+'
WHEN >= 73 and < 80 THEN 'B-'
// and so one...
ELSE 'F'
END as myGrade,
studentID
from
table1
Edit: Sorry, I thought you were trying to do it in the database (where I would have probably done it if I just wanted the grade, not the score. Here is the PHP code anyhow:
$myScore=90; // for example
function getMyScore($myScore)
{
if($myScore==100)
{
return 'A+';
}
elseif ($myScore >= 90 && $myScore <100)
{
return 'A-';
}
elseif ($myScore >= 80 && $myScore <90)
{
return 'B+';
}
// .....
else
{
return 'F';
}
}
$myGrade=getMyGrade($myScore);
echo $myGrade; // output: A-
Based on the function I wrote:
function getMyScore($myScore)
{
if($myScore==100)
{
return 'A+';
}
elseif ($myScore >= 90 && $myScore <100)
{
return 'A-';
}
elseif ($myScore >= 80 && $myScore <90)
{
return 'B+';
}
// .....
else
{
return 'F';
}
}
while($row = mysql_fetch_array($sql))
{
echo '<tr><td>'.$row['userid'].'</td><td>'.$row['$total'].'</td>';
// In your code:
$letter = mysql_query('SELECT score,letter FROM letter');
while($row = mysql_fetch_array($letter))
{
$score= $row['score'];
echo '<td>'.getMyGrade($row['letter']).'</td>';
}
echo'</tr>';
}

Need a little help for algorithm

Right now I have a set of if and elseif to set the height of the div
<?php
if($num == 0){echo 'height:0px;';}
elseif($num > 0 && $num < 10) {echo 'height:3px;';}
elseif($num >= 10 && $num < 22) {echo 'height:7px;';}
elseif($num >= 22 && $num < 45) {echo 'height:16px;';}
elseif($num >= 45 && $num < 50) {echo 'height:33px;';}
elseif($num >= 50 && $num < 67) {echo 'height:36px;';}
elseif($num >= 67 && $num < 79) {echo 'height:48px;';}
elseif($num >= 79 && $num < 88) {echo 'height:56px;';}
elseif($num >= 88) {echo 'height:72px;';}
?>
The problem is that this is copyed 5 times for 5 different divs and think there is better way to do it
Like so :
<?php
function divHeight($maxNum,$number)
{
if($number == 0)
{
echo 'height:0px;' ;
}
elseif($number >= $maxNum)
{
echo 'height:72px;' ;
}
else
{
//here were the algorithm have to be
}
}
?>
I will call it like <?php divHeight(88,$number);?>
The max height of div is 72, now how to calculate the height?
// Edit : This is so simple :X :X but is too late and i havent sleept so
$newHeight = floor($number * 72 / 100);
echo $newHeight;
function mapNumToHeight($num) {
// Max $num => height for that num
$heightMap = array(
0 => 0,
9 => 3,
21 => 7,
44 => 16,
49 => 33,
66 => 36,
78 => 48,
87 => 56,
88 => 72
);
// Store the keys into an array that we can search
$keys = array_keys($heightMap);
rsort($keys);
// We want to find the smallest key that is greater than or equal to $num.
$best_match = $keys[0];
foreach($keys as $key) {
if($key >= $num) {
$best_match = $key;
}
}
return 'height:' . $heightMap[$best_match] . 'px;';
}
mapNumToHeight(3); // height:3px;
mapNumToHeight(33); // height:16px;
mapNumToHeight(87); // height:56px;
mapNumToHeight(88); // height:72px;
mapNumToHeight(1000); // height:72px;

Categories