printing data a specific number of times with respect to a const and string length

Multi tool use
printing data a specific number of times with respect to a const and string length
Let there be a sring $str
with any number of characters and $cons=25
, also $rest=$cons-strlen( $str )
.
$str
$cons=25
$rest=$cons-strlen( $str )
Now i wanted to print the string inside boxes so i made a loop where it catches the characters inside the string and echo it inside a div with class w3-border
Css:
.w3-border
{
border: 1px solid #f1e1d4 !important;
width:16px !important;
height:25px;
border-collapse: collapse;
float: left;
}
Php:
echo'<div class="w3-container" style="margin-bottom:5px !important">';
$str = $row['address'];
$strlen = strlen( $str );
$rest=$cons-$strlen;
echo'<div class="w3-left" style="width:150px">Address</div>';
for( $i = 0; $i <= $strlen; $i++ ) {
$char = substr( $str, $i, 1 );
echo '<div class="w3-border w3-center">'.strtoupper($char).'</div>';
}
if($rest>0)
{
for($i=0;$i<$rest;$i++)
{
echo '<div class="w3-border w3-center"> </div>';
}
}
echo'</div>';
Now the above work fine if the number of character in the string is less that $cons
, but there are more than that, i wanted to do that, it will print another '<div class="w3-border w3-center"> </div>'
until $second>0 ($second=$cons-$rest), $third>0 ($third=$cons-$second) and so on.
$cons
'<div class="w3-border w3-center"> </div>'
I am having hard time figuring it out how to achieve so ? Any help/hint will appreciated.
A view of my problem -
i want the data of string to be printed inside boxes , in 1 line max 25 boxes if the data is smaller than 25 boxes then print the data and rest make it blank boxes to make upto 25 boxes like this
but if str is bigger than 25 then it goes to next line like this
so i want to do something that could make the second line complete upto 25 boxes, or if the data is upto third line then make boxes upto last so it should look clean , but i am no having any idea how to perfom it
$rest=$cons-$str
sorry it should be $rest=$cons-strlen($str); i will update question
– Adern Nerk
Jul 1 at 8:22
i don't understand what your code does or should do. Can you inlude abn input and what you expect.
– Andreas
Jul 1 at 8:25
@andreas i have updated the question
– Adern Nerk
Jul 1 at 8:40
1 Answer
1
The modulo operator might be helpful here. It calculates (in this case) the remainder of strlen() / 25
. Can you work it out from there?
strlen() / 25
Something along these lines may work, although I am not sure this is the exact solution -- didn't try it:
$rest = $cons - ($strlen % $cons);
EDIT:
After running the following code, I am convinced that the above solution should work. Or I may be misunderstanding your question...
<?php
$cons = 25;
$strlen = 24;
echo $cons - ($strlen % $cons). "<br>";
//Output: 1
$strlen = 26;
echo $cons - ($strlen % $cons);
// Output: 24
?>
well this is a nice idea, i had already tried it but somehow it reurns values thans is 1 less ( for example to make it proper i have to use
$ rest = $cons - ($strlen % $cons)+1;
, thats why i didnt used it , maybe though someone would figure out a different approach of doing this : ), thanks for help– Adern Nerk
Jul 1 at 9:07
$ rest = $cons - ($strlen % $cons)+1;
I've edited my answer. I don't really see why simply changing the declaration of
$rest
wouln't fix your issue.– Niellles
Jul 1 at 9:15
$rest
You may have to change
for($i=0;$i<$rest;$i++)
to for($i=0;$i<=$rest;$i++)
, that's the proper solution (instead of $rest = $rest + 1
) to make sure you're getting enough blank boxes.– Niellles
Jul 1 at 9:17
for($i=0;$i<$rest;$i++)
for($i=0;$i<=$rest;$i++)
$rest = $rest + 1
well your answer make me realize and i figured it out : ), i also made a loop inside the str echo'ing loop when the count completes to every 25 units it displays a
<br>
making it proper :)– Adern Nerk
Jul 1 at 9:18
<br>
yes i changed the for loop ;), that was what i was takign about , ... ty for help
– Adern Nerk
Jul 1 at 9:19
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
If $cons is 25 and $str is a string, what does this do:
$rest=$cons-$str
?– Andreas
Jul 1 at 8:21