I am currently studying a beginning PHP programming class and I need some assistance with one assignment I'm trying to solve. The assignment is to create a form where the user can enter a positive integer. Then, use a “for” loop to display that amount of horizontal lines created by the "hr" tag [Hint: <hr size=1 width=50% color='black'>]. Finally, use an if statement to perform “modulus” calculation. When the counter in the “for” loop is an even number set the width of the horizontal line to 50%; otherwise, set the width of the horizontal line to 100%.
Here's the code I have come up with thus far:
<?php
if ($_POST) { // if the form is filled out
$integer = $_POST["pi"];
$i = $integer;
for ($i = 1; $i <= $integer; $i++) {
if ($i % 2) { // modulus operator
echo "<hr size=1 width=50% color='black'>";
} else {
echo "<hr size=1 width=100% color='red'>";
}
}
}
else { // otherwise display the form
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Enter a <i>Positive Integer</i>:
<input type="text" name="pi" size=5>
<input type="submit" value="Check"></form></p>
<?php
}
?>
I can't post an image yet, but the sample output should be a 50% black horizontal rule, followed by a 100% red horizontal rule, until the integer entered is reached. In between each hr seems to have some spacing.
This line:
$i = $integer;
...is redundant, as soon as you say for($i = ..., $i will be overwritten. In your case, so it should be. Take that line out to start with.
Second, I think the problem you're having is that your lines aren't showing as black or red. Reason is that color is a font attribute and you should look at this post to find out how to change your color:
Changing the color of an hr element
I suggest using class='black' and class='red' in your PHP and setting classes up in your CSS.
It isn't clear what the problem is. If the issue is that your HR elements have spaces between them, then removing the default margin will help (at least in Firefox, I'm not sure if all browsers use the same rendering rules on HR).
<hr size="1" width=50% color='black' style="margin:0;" />
<hr size="1" width=100% color='red' style="margin:0;" />
The issue is that you are assigning $i equal to the variable $integer, therefore they are the same value.
<?php
if ($_POST)
{ // if the form is filled out
$integer = $_POST["pi"];
for ($i = 1; $i <= $integer; $i++)
{
if ($i % 2 ===0)
{ // modulus operator
echo "<hr size=1 width=50% color='black'>";
}
else
{
echo "<hr size=1 width=100% color='red'>";
}
}
}
else
{ // otherwise display the form
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Enter a <i>Positive Integer</i>:
<input type="text" name="pi" size=5>
<input type="submit" value="Check"></form></p>
<?php
}
?>
use this CSS code to formatting all hr elements
<style type="text/css">
hr {margin: 0px auto 0px auto;}
</style>
Related
I am trying to nest a php loop into another php loop according to an php array. First loop will continue the loop according how many elements the array has and I want the second loop will continue according to the element. If the first element is 2 the user input repeat 2 times after that the second element is 3 the user input repeat 3 times. when all the elements are gone the loop stopped. but I don't know how to do it.
<body style="margin: 0 auto; width: 50%;">
<div class="input-field col s12">
<?php
$dolils = array("2","3","4");
$total_dolils = count($dolils);
for ($x = 1; $x <= $total_dolils; $x++) {
?>
<p class="blue-text" style="color: blue; font-weight: bold;"><?php echo $x; ?> নং দিনে দলিলের হিসাবঃ </p>
<?php
$z = 0;
for($y = 0; $y <= $dolils[$z]; $y++) {
?>
<p class="red-text" style="color: red; font-weight: bold;">দলিলের নাম্বার</p>
<input id="input_number" type="number" name="dolils_no[]" data-length="10">
<p>মোট পৃষ্ঠাঃ </p>
<input id="input_number" type="number" name="page[]" data-length="10">
<p>মোট লাইনঃ </p>
<input id="input_number" type="number" name="line[]" data-length="10">
<?php
$z++;
echo "<br/>";
echo $z;
echo "<br/>";
if($z >= end($dolils)){
die;
}
}
}
?>
</div>
</body>
Basic logic:
<?php
$content = '<div>';
$dolils = array (2, 3, 4);
foreach ($dolils as $dolil) {
for ($i = 1; $i <= $dolil; $i++) {
$content .= "<p>iteration {$dolil}/{$i} </p>";
}
}
$content .= '</div>';
// after all
echo $content;
bonus tip:
Stop mixing PHP and HTML like this is not comfortable, error pron and just ugly. Instead build HTML in one piece of PHP code or better get familiar with templating engines and/or MVC patterns.
I am trying to apply if else condition in bootstrap class with php variable but its not working. My variable is getting result.
Below is my tbody where i am applying php code:
<div class="<?php '#count_rows#' <= '2')
{
echo 'middle_menu';
}
else
{
echo 'middle_menu1';
} ?>">
<table class="table_4">
<thead>
<tr>
<th class="th_11">Quantity</th>
</tr>
</thead>
<tbody>
#TABLE_BODY#
</tbody>
</table>
</div>
Below is my 2 css classes:
.middle_menu
{
margin-top: 40px;
padding-bottom: 200px;
}
.middle_menu1
{
margin-top: 40px;
}
I am fetching my variable from another page where i set this type of opening and closing variable with #.
Below is my code for your reference but i dont think that is an issue for me because i check this #count_rows# variable with print_r in my current page and it showing me correct result.
foreach ($form_field as $key => $value){
$htmlContent = str_replace('#'.$key.'#', $value, $htmlContent);
}
<?php $className = $count_rows <= 2 ? 'middle_menu' : 'middle_menu1'; ?>
<div class="<?php echo $className; ?>" />
You can create a test.php then run php test.php and you should see the result
<?php
// $count_rows = 1;
$count_rows = 3;
$className = $count_rows <= 2 ? 'middle_menu' : 'middle_menu1';
echo "<div class=\"$className\"/>\n";
<div class="<?php '#count_rows#' <= '2')
You are missing a keyword here and also some brackets (which should cause a fatal syntax error), Also you are comparing two different literal strings - surely one of the terms should be a PHP variable?
You can't read back stuff you've previously written to the output stream - you are confusing what is happening in HTML, CSS and PHP.
I think you mean...
<div class="<?php if ($form_fields['count_rows'] <= '2')
Comparing a number by casting it to a string is rather dangerous.
Personally I would do this:
<div class="<?php echo (2 <= $form_fields['count_rows'])
? 'middle_menu' : 'middle_menu1'; ?>">
Since you didn't mention that the script blew up in your face, I suspect there may be a lot of other issues.
foreach ($form_field as $key => $value){
$htmlContent = str_replace('#'.$key.'#', $value, $htmlContent);
}
This is very innefficient. Consider:
$find=array_keys($form_field);
foreach ($find as $index=>$term) {
$find[$index]='#' . $term . '#';
}
$htmlContent = str_replace($find, $form_feld, $htmlContent);
I am trying to echo pagenumbers in html. Below is the code. It does echo,
but numbers are always on left side of the page. Then I created a div to
center it, it did, but then all numbers are div. When user clicks on
numbers, basically, div gets clicked. I want user to be able to click on
each page number with page numbers at the center of the page.
<html>
<body>
<?php
for ($i = 1; $i <= $total_pages; $i++) { // print links for all pages
echo '<a href="getjobs.php?function_Options[0]='.$q.'& function_Options[1]='.
$q1.'&state_Options[0]='.$qq.'&state_Options[1]='.
$qq1.'&state_Options[2]='.$qq2.'&state_Options[3]='.
$qq3.'&state_Options[4]='.$qq4.'&state_Options[5]='.
$qq5.'&page='.$i.'"';
if ($i==$page) echo " class='curPage'";
echo " style='color: white; font-size: 20pt;'>".$i."</a> ";
};
?>
</body>
</html>
<div class="pageNr">
<?php
for ($i=1; $i<=$total_pages; $i++) { // print links for all pages
echo('<a href="getjobs.php?
function_Options[0]='.$q.'&
function_Options[1]='.$q1.'&
state_Options[0]='.$qq.'&
state_Options[1]='.$qq1.'&
state_Options[2]='.$qq2.'&
state_Options[3]='.$qq3.'&
state_Options[4]='.$qq4.'&
state_Options[5]='.$qq5.'&
page='.$i.'"');
if ($i==$page) echo " class='curPage'";
echo " style='color: white; font-size: 20pt;'>".$i."</a> ";
};
?>
</div>
So you just need to make the width of your div like a couple of pixels and let it float right... this should work.
And just a little tip: best practice is that you don't use inline css...
I have a problem with not working "first-child". I want to make top of christmas tree as red so first div.main have to be div with red background.
And another problem is that if I set amount of the height of this tree over 70 the next char is in next line but want to stay this div in line over and scroll it in this case.
Sorry for not English name of Variables
<html>
<head>
<style>
div.center {
border:2px solid black;
display:block;
}
div.main{
display:inline-block;
color:green;
}
div.main:first-child {
background-color:red
}
</style>
</head>
<body>
<form action="choinka.php" method="GET">
wysokość: <input type="text" name="tak">
<input type="submit" value="dawaj">
</form>
<?php
$tak = $_GET['tak'];
echo " wysokosc to: $tak";
echo '<div class="center>";
for($wysokosc = $_GET['tak']; $wysokosc >= 0;$wysokosc--){
echo "<br>";
for($szerokosc = 0; $szerokosc <= $wysokosc ; $szerokosc++){
echo " "; //spacja szerokosc 1em
if($szerokosc == $wysokosc)
for($znaki = $_GET['tak']; $znaki >= $wysokosc ; $znaki--){
echo '<div class="main">  ⊗</div>';
}
}
};
echo "</div><br><br>";
?>
Please try div.main:first-of-type instead of div.main:first-child as you can see loop append a <br></br> before main and :first-child get <br> as the first child.
This will do the trick Check Image
<!DOCTYPE html>
<html lang="en">
<head>
<h1>Table Generator</h1>
</head>
<body>
<center>Refresh</center>
<?php
$rows = (isset($_POST['rows']) ? $_POST['rows'] : null);
$cols = (isset($_POST['cols']) ? $_POST['cols'] : null);
$highlight = (isset($_POST['highlight']) ? $_POST['highlight'] : null);
if ($rows == "")
{
$rows = 10;
}
if ($cols == "")
{
$cols = 10;
}
if ($highlight == "")
{
$highlight = 5;
}
?>
<form method="post">
ROWS <input type="text" name="rows" value = "<?php echo $rows;?>" />
COLUMNS <input type="text" name="cols" value = "<?php echo $cols;?>" />
HIGHLIGHT <input type = "text" name = "highlight" value = "<?php echo $highlight;?>" /><br>
<input type="submit" value="Generate">
</form>
<?php
if(isset($_POST['rows']))
{
$randnumber = rand(0,100);
$rows = $_POST['rows'];
$cols = $_POST['cols'];
$highlight = $_POST['highlight'];
echo '<table border="1" align = "center">';
if (is_numeric($rows) and is_numeric($cols) and is_numeric($highlight))
{
if ($randnumber % 2 == 0)
{
echo '<center>The first number is <div class = "red">even</div></center>';
}
else
{
echo '<center>The first number is <div class = "green">odd</div></center>';
}
for($row = 1; $row <= $rows; $row++)
{
echo '<tr style = "background-color:green">';
for($col = 1; $col <= $cols; $col++)
{
if ($randnumber % $highlight == 0)
{
echo '<td style = "background-color: red">';
echo $randnumber;
$randnumber++;
echo '</td>';
}
else
{
echo '<td>';
echo $randnumber;
$randnumber++;
echo '</td>';
}
}
echo '</tr>';
}
echo '</table>';
}
else
{
echo "<center>Rows / Columns / Highlight must ALL be INTEGER values. Re-enter correct value(s).</center>";
}
echo '<pre><center>';
print_r($_POST);
echo '</center></pre>';
}
?>
<style type ="text/css">
h1 {
color: grey;
text-align:center;
}
form {
text-align: center;
padding-bottom: 20px;
}
a:link {
text-decoration: none;
}
.red {
color: red;
}
.green {
color: green;
}
</style>
</body>
</html>
So. I have this PHP code to generate a table based off the user's input and I recently ran into a problem I cant figure out how to fix.
It was working perfectly fine but now whenever I use the Refresh link it resets the entire page to default (i.e. default textbox values instead of keeping the current ones, removing the table).
So, I have 2 questions. How would I keep the data on refresh (with $_POST being used) and how to display the table with the default values when the page first loads.
Refresh
Clicking it will trigger browser's reload mechanism and you'll be asked to resubmit the form action, it will allow you to keep POST data.
You need to re-create the post if you want to keep the parameters. Can be done pretty eaily by looping thru the array.
<form method='POST' id='refresh' action='<?php echo $_SERVER['PHP_SELF']; ?>'>
<?php foreach($_POST as $k=>$v): ?>
<input type='hidden' name='<?php echo $k; ?>' value='<?php echo $v; ?>' />
<?php endforeach; ?>
<a href='#' onclick='document.getElementById("refresh").submit(); return false;'>refresh</a>
</form>
Note: This is a little longer than the other answer, but will not prompt to resend post data.