I'm developing a webpage that has many forms with some text close to form. This form and text are generated by a function, but I get this
As you can see the forms and the text are not aligned with forms and text below. I would like to have this:
(this work because I put spaces manually). This is how I create forms.
function add_form($product,$name,$productp)
{
$productpt = "X";
echo $name;
echo '<input type="text" name="'. $product. '" id="'. $product. '" style="font-size:12pt;height:30px"/> x'.$productp. '€
price:'.$productpt.'€<br>';
}
echo '<form method="post" name="form1" id="form1" autocomplete="off">'; //start form
echo '<div class="leftpane">'; //start leftpane
echo '<h1>';
echo '<font size="6" color="red"> ALIMENTS </font><br>';
//ADD ELEMENTS IN MY MENU
//The first argument is the form name and id, the second is the name that will be printed(to the left of the form), the third argument is the price.
$menu[] = new Menu("pasta", "Pasta", 2);
$menu[] = new Menu("spaghetti", "Spaghetti", 1.50);
$menu[] = new Menu("pizza", "Pizza", 5);
$menu[] = new Menu("chicken_wings_x4", "Chicken Wings X4", 4.50);
$menu[] = new Menu("cheeseburger", "Cheeseburger", 6);
$menu[] = new Menu("Sandwich", "Sandwich", 2);
$menu[] = new Menu("hamburger", "Hamburger", 4.50);
$menu[] = new Menu("stuff", "stuff", 15.50);
//FOR EACH ELEMENT CREATE A FORM
for($i=0; $i<count($menu); $i++){
add_form($menu[$i]->form_name, $menu[$i]->name, $menu[$i]->price);
}
echo '</h1>';
echo '</div>'; //Close leftpane
...Do Others stuff in middlepane and in rightpane
To be clearer in html, for example the first form will be:
<input type="text" name="pasta" id="pasta" style="font-size:12pt;height:30px"/> x2€ price:X€<br>
I thought to divide in 3 parts my leftpane and to place $name to the left, the form to the middle and the price to the right. But the problem is that i create form in function, so I do not know how to do it.
This is my css:
<style>
input[type=text] {
width: 10%;
margin: 7.5px 0;
box-sizing: border-box;
}
input[type=submit].class1 {
width: 50%;
padding: 12px 20px;
margin: 8px 0;
box-sizing: border-box;
}
input[type=submit].class2 {
width: 50%;
padding: 12px 20px;
margin: 8px 0;
box-sizing: border-box;
}
body, html {
width: 100%;
height: 100%;
margin: 0;
}
.container {
width: 100%;
height: 100%;
}
.leftpane {
width: 33%;
height: 100%;
float: left;
border-collapse: collapse;
}
.middlepane {
width: 33%;
height: 100%;
float: left;
border-collapse: collapse;
}
.rightpane {
width: 33%;
height: 100%;
position: relative;
float: right;
border-collapse: collapse;
}
form {
display: inline-block;
}
h1 {
font-size: 19.5px;
}
</style>
I would suggest you to use a table. https://www.w3schools.com/html/html_tables.asp
Or you could wrap every section into a div-container and define its width.
I think your output code would really benefit from some additional HTML structure with classes that allow you to control their layout in a consistent way with CSS. I would update the add_form() function:
function add_form($product,$name,$productp) {
$productpt = "X";
echo '<div class="form-wrap">';
echo '<span class="form-name">' . $name . '</span>';
echo '<input type="text" name="'. $product. '" id="'. $product. '"/> <span class="form-label">x'.$productp. '€</span> <span class="form-price">price:'.$productpt.'€</span>';
echo '</div>';
}
Then with the output having more targetable HTML structure you can do this with CSS:
.form-wrap input {
font-size: 12pt;
height: 30px
}
.form-name {
display: inline-block;
width: 100px;
margin-right: 20px;
}
.form-label {
display: inline-block;
margin-left: 5px;
margin-right: 20px;
}
<div class="form-wrap">
<span class="form-name">Something</span>
<input type="text" name="something" id="somethingId" />
<span class="form-label">x1€</span>
<span class="form-price">price:1€</span>
</div>
<div class="form-wrap">
<span class="form-name">Another one</span>
<input type="text" name="anotherone" id="somethingId2" />
<span class="form-label">x2€</span>
<span class="form-price">price:2€</span>
</div>
<div class="form-wrap">
<span class="form-name">Number Three</span>
<input type="text" name="thirdone" id="somethingId3" />
<span class="form-label">x3€</span>
<span class="form-price">price:3€</span>
</div>
Try this.
function add_form($product,$name,$productp)
{
$productpt = "X";
echo "<table><tr>";
echo "<td width='20%'>" . $name . "</td>";
echo "<td width='20%'><input type='text' name='". $product . "' id='" . $product . "' style='font-size:12pt;height:30px;' /></td>";
echo "<td width='20%'>x" . $productp . "€</td>";
echo "<td width='20%'>price:" . $productpt . "€</td>";
echo "</tr></table>";
}
Related
I'm trying to create a square, created of smaller red and blue squares. The problem is, when i want to use something wierd happens, every new line is off by 20 px.
<style>
.box{
background-color: red;
position: relative;
width: 20px;
height: 20px;
margin-left: 3px;
margin-top: 3px;
float:left;
}
.box1{
background-color: blue;
position: relative;
width: 20px;
height: 20px;
margin-left: 3px;
margin-top: 3px;
float:left;
}
</style>
<?php
for($a=0;$a<4;$a++){
for($i = 0; $i < 4; $i++)
{
echo "<div class= box> </div>";
echo "<div class= box1> </div>";
}
echo "<br />";
for($i = 0; $i < 4; $i++)
{
echo "<div class= box> </div>";
echo "<div class= box1> </div>";
}
echo "<br />";
}
?>
How it looks like
Using relative position is the problem. Use display:inline-block instead.
Also you can accomplish this only using 2 for loops.
<style>
.box{
background-color: red;
width: 20px;
height: 20px;
margin-left: 3px;
margin-top: 3px;
display: inline-block;
}
.box1{
background-color: blue;
width: 20px;
height: 20px;
margin-left: 3px;
margin-top: 3px;
display: inline-block;
}
</style>
<?php
for($a=0;$a<8;$a++){
for($i = 0; $i < 4; $i++)
{
echo "<div class= box> </div>";
echo "<div class= box1> </div>";
}
echo "<br />";
}
?>
The proper way to break float is to use clear style attribute. For example add this to your styles:
br{
clear:both;
}
<br /> will now reset the float of the divs after it, so the next ones will be in new line.
Example in here.
Please note that class attribute should be in quotes, your code is not a valid HTML. To make it valid use one of these:
echo "<div class='box'> </div>";
or
echo '<div class="box"> </div>';
From my point of view you have to add one main div as like below code and have to add float left and width to 100%;
<style>
.box{
background-color: red;
position: relative;
width: 20px;
height: 20px;
margin-left: 3px;
margin-top: 3px;
float:left;
}
.box1{
background-color: blue;
position: relative;
width: 20px;
height: 20px;
margin-left: 3px;
margin-top: 3px;
float:left;
}
.main {
float:left;
width:100%;
}
</style>
<?php
for($a=0;$a<4;$a++){
echo "<div class='main'>";
for($i = 0; $i < 4; $i++)
{
echo "<div class= box> </div>";
echo "<div class= box1> </div>";
}
echo "</div>";
echo "<br />";
echo "<div class='main'>";
for($i = 0; $i < 4; $i++)
{
echo "<div class= box> </div>";
echo "<div class= box1> </div>";
}
echo "</div>";
echo "<br />";
}
?>
As you can see in the picture, there is a box to the right with some text and a lot of white space.
My goal is to have the text under the pictures while I have 3 pictures in a row that are nicely aligned.
When I try this either the pictures aren't aligned anymore or the pictures aren't cropped anymore.
I wanted every picture with the same size and still sharp but, then this issue came up.
#content {
width: 100%;
max-width: 100%;
margin: 20px auto;
}
form {
width: 50%;
margin: 20px auto;
}
form div {
margin-top: 5px;
}
#img_div {
width: 30%;
margin-left: 2%;
margin-right: 1%;
margin-bottom: 3%;
border: 2px solid #d8680c;
float: left;
}
#img_div:after {
content: "";
display: block;
clear: both;
}
img {
float: left;
object-fit: cover;
width: 250px;
height: 250px;
}
#pictext {
word-wrap: break-word;
}
<div id="content">
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<div id='img_div'>";
echo "<img src='fotopage\images/" . $row['image'] . "' >";
echo "<p id='pictext'>" . $row['image_text'] . "</p>";
echo "</div>";
}
?>
</div>
use class instead of using ID
ID must be unique and when you do the loop there will be some other divs with same ID
add it like this
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<div class='img_div'>";
echo "<img src='fotopage\images/" . $row['image'] . "' >";
echo "<p class='pictext'>" . $row['image_text'] . "</p>";
echo "</div>";
}
?>
make sure to reflect them via JS or JQuery depends on what your are using
So I am doing the same grocery list I did before in JScript for an assignment but now in PHP. When I try to calculate the total it gives me a strange number.
$result is a calculation from product price multiplied by the quantity (waarde & aantal, its Dutch).
so when I do $total += $result;
And then I echo that, I get a very strange result.
To me it looks like it does not go by all the results. I tried using the $i index for it. But that does not work. What am I missing?
(Very new to PHP just learned a bit of the basic of JavaScript)
The expected outcome is all the totals shown in the table added together to create the total: 15.76
The result now is 31.984.85.98, what even is that magical number? Might be that I do something wrong with the number format, looking into that now too. (Also is it normal to share the code as I did? Apparently it's not really for PHP, I guess because it being a server-side thing) I have a CodePen of the original JavaScript version of it : https://codepen.io/3lly/pen/oNxaPKg maybe for clear view, you can see what I mean. The totals of all the total column cells.
table {
margin-top: 20px;
display: inline-block;
}
th, td , input {
border: 2px solid #FFB000;
padding: 2px;
color: black;
}
body {
text-align: center;
}
h1 {
color: #FFB000;
font-family: impact;
text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
}
p {
color: white;
}
div {
display: inline-block;
}
#add {
display: inline-block;
padding: 10px 20px;
background: orange;
box-shadow: -3px 3px black, -2px 2px black, -1px 1px black;
border: 1px solid orange;
}
#add:hover {
background: green;
color: white;
}
input[type="text"], input[type="number"] {
background-color: skyblue;
}
input[type="number"]:hover {
background-color: black;
}
#totaal {
border: 2px solid #FFB000;
background-color: #282828;
padding: 5px;
color: skyblue;
}
#totaal:hover{
background-color: green;
color: white;
}
<head>
<link rel="stylesheet" href="style.css">
<title>Boodschappenlijst</title>
</head>
<body>
<h1> Boodschappenlijst </h1>
<div id="container"></div>
<table>
<tr>
<?php
$headerTexts = ['Name', 'Prijs', 'Aantal', 'Totaal'];
for($i=0;$i<count($headerTexts);$i++) {
echo "<th>" . $headerTexts[$i] . "</th>";
}
?>
</tr>
<?php
for($i=0;$i<count($products);$i++) {
//result calculations
$total = 0;
$result = number_format($products[$i]['waarde'] * $products[$i]['aantal'],2);
$total += $result;
echo $total;
//echo "<pre>" . is_int($products['waarde']) . "</pre>";
//Table Rows
echo "<tr>";
echo "<td>" . $products[$i]['omschrijving'] . "</td>" .
"<td>" . $products[$i]['waarde'] . "</td>" .
"<td>" . $products[$i]['aantal'] . "</td>" .
"<td>" . $result . "</td>";
echo "</tr>";
}
?>
</tr>
</table>
<p>Naam</p>
<form>
<input type="text" name="item" id="naam" /><br />
<p>Aantal</p>
<input type="text" name="quantity" id="qty" /><br />
<p>Prijs</p>
<input type="text" name="price" id="prijs" /><br/><br />
<input type="button" value="Add Product" onclick="updateTable()" id="add"><br /><br />
</form>
<div id="totaal"></div>
<!-- <script src="script.js"></script>-->
</body>
</html>
So I pushed the results of the total calculation of each products to the array which I did not do before like so :
$products[$i]['total'] = $products[$i]['waarde'] * $products[$i]['aantal'];
Next I did the calculation like so:
$total += $products[$i]['total'];
Within the loop. And that fixed it :D It was very simple apperently !
i am begineer on PHP but it can be so easy for experience people, but i am tried to learn. i just want to add button side by side, but according to my codes, which is one under the other.
while ($row_all = mysqli_fetch_assoc($result))
{
echo '<form method="post">';
echo '<u>'.$row_all["product_name "].'</u>';
echo ' <button name="add_to_cart" class="btn3"
value='.$row_all['urun_adi'].'
type="submit">'.$row_all["urun_adi"].'</button>';
echo '</form>';
}
CSS code
.btn3{
position: relative;
top: 150px;
left: 1280px;
width: 150px;
height: 100px;
border:5px solid white;
color:yellow;
font-weight:bold;
font-family:Verdana;
font-size:14px;
background-color: green;
}
Try this ! Create the form outside the while
echo '<form method="post">';
while ($row_all = mysqli_fetch_assoc($result))
{
echo '<u>'.$row_all["product_name "].'</u>';
echo ' <button name="add_to_cart" class="btn3"
value='.$row_all['urun_adi'].'
type="submit">'.$row_all["urun_adi"].'</button>';
}
echo '</form>';
My code displays this:
but I am not sure if I implemented the div and input form mixing properly.
Could you please comment if I am doing this in the correct way?
CSS:
.newstuff
{
width: 80%;
height: 100px;
}
.newstuff.left
{
float: left;
padding-left: 5px;
text-align: right;
width: 15%;
vertical-align: middle;
background: red;
}
.newstuff.center
{
float: left;
padding-left: 5px;
text-align: left;
width: 35%;
vertical-align: middle;
background: green;
}
.newstuff.right
{
float: left;
padding-left: 5px;
text-align: left;
width: 35%;
vertical-align: middle;
background: yellow;
}
.buttontest
{
float: left;
width: 35%;
}
PHP:
<?php
echo "<head>";
echo ' <link rel="stylesheet" href="test.css">';
echo "</head>";
echo '<body>';
echo '<div class="newstuff">';
echo ' <form action="'.htmlspecialchars($_SERVER["PHP_SELF"]).'" method="post" name="auth_form">';
echo '<div class="newstuff left">';
echo ' <input type="text" name="data1" placeholder="This is left">';
echo '</div>';
echo '<div class="newstuff center">';
echo '<textarea name="centertext" rows="5" cols="50" maxlength="255" placeholder="This is center"></textarea>';
echo '</div>';
echo '<div class="newstuff right">';
echo ' <input type="text" name="data3" placeholder="This is right">';
echo '</div>';
echo '</div>';
echo '<div class="buttontest">';
echo ' <input type="submit" name="test" value="Testing">';
echo " </form>";
echo '</div>';
echo "</body>";
?>
It's definitely not the proper way of mixing.
Try using template engines for PHP, here are the possible options for you to start:
Twig
Smarty
Others
As the compromise, which I'd not recommend to use in long run, is to use Alternative syntax for control structures in PHP and short tags, which I found useful exactly for the trivial templates, when there's need to involve template engines.