Trying to echo a variable ( $i) into another variable [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a special form I have been making that uses some cusotm post types in wordpress. At one point I need to echo a variable $i into an if statement.
There is some validation stuff at the top that will look like this and the code in the loop is below. Pretty much I have been trying to get the majorCause1Error to be majorCause $i Error if you know what I mean, so all up it will be like 1-13
Edit: Sorry If it is hard to see what I am asking, I am finding it really hard to word my problem.
So there is a loop running around the li tags and it echos $i into the name etc so it becomes majorCause1 then next one majorCause2 and the next one magjorCause3 etc etc
Under the labels there is an if statement that is like - if($majorCause1Error !='') { do something } - I want this to be like if($majorCause1Error !=''){} and then the next one be like if($majorCause2Error !=''){} and then if($majorCause3Error !=''){}
Does this make more sense?
Here is a link to the site http://www.foresightaus.com.au/form/
if(trim($_POST['majorCause1']) === '') {
$majorCause1Error = "Please enter a major cause.";
$hasError = true;
} else {
$majorCause1 = trim($_POST['majorCause1']);
}
if(trim($_POST['majorCause2']) === '') {
$majorCause2Error = "Please enter a major cause.";
$hasError = true;
} else {
$majorCause2 = trim($_POST['majorCause2']);
}
<li class="fill-in">
<label for="majorCause<?php echo($i); ?>"><?php echo($j); ?>. State one major cause:</label>
<input type="text" name="majorCause<?php echo($i); ?>" id="majorCause<?php echo($i); ?>" value=""/>
<?php if($majorCause1Error != '') { ?>
<span class="error"><?=$majorCause1Error;?></span>
<?php } ?>
</li>

You probably want to be using an array but what you are referencing is called a variable variable and is supported by PHP!
Something like this should do it
${"majorCause{$i}Error"}

Related

How to print all even numbers between two input values typed by user. please write code hint only in php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
How to print all even numbers between two input values typed by user. please write code hint only in php.
I can give you a hint. Lookup the modulo operator. With it you can do something like this:
<?php
if (($number % 2) === 1)
{
echo "$number is odd.";
}
if (($number % 2) === 0)
{
echo "$number is even." ;
}
?>
Also. Have a look at how you ask questions on stack overflow. Or they will prevent you from asking questions in the future if you don't adhere to those rules.
use this code, for user input i have created a form.
<html>
<body>
<form method="post">
<input type="number" name="num1">
<input type="number" name="num2">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
// while loop that will print number
while($num1 <= $num2){
// here is the condition to check the EVEN number
if($num1%2!==0){
echo $num1."<br>";
}
// increasing the loope counter by 1
$num1++;
}
}
?>
i hope now your problem has been solved..

Getting data from hundreds of fields [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
This is more of a hypothetical, since I just want to use the most efficient way to go about this.
Say you have a page with over 100 textboxes, and a submit button that bring you to a next page and displays what you've written.
How would I go about only printing the textboxes that have been filled in?
I would know how to do this with a lot of if statements, or a very long switch statement, but perhaps there's a simpler way?
Thank you!
yes, there is.
simply call them numbering them.
for example you'll have textbox1, textbox2, textbox3...
in this way, on the next page you just have to build a for loop.
for ($index = 1; $index < 100; $index++) {
if (!empty($_REQUEST["textbox" . $index]))
{
echo "textbox number {$index} isn't empty!";
}
}
in this way you will get each box is full or not.
Something like
<?php
for($i=0; $i<100; $i++){
if(!empty($_POST['textbox'.$i]))
echo $_POST['textbox'.$i].'<br>';
}
?>
should work I guess.
I did not test it
if the name itself is not important i would give them the name txtbox[] (name="txtbox[]")
This way you can choose one name for all the boxes that belong together, have a specific array to loop through $_POST['txtbox'] . Then just echo them all
foreach($_POST['txtbox'] as $key => $tb) {
echo "<br>Box " . $key . ": " . $tb;
}
Give them names like txtbox_1 to txtbox_100
foreach ($_POST as $key => $value) {
if (is_integer(strpos($key, 'txtbox_')) && $value != '') {
echo "<br />$key = $value ";
}
}

PHP implode function in if and foreach condition [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
This is my HTML Code
<input type="checkbox" value="fotoğraf" name="materyal[]"> Fotoğraf
<input type="checkbox" value="resim" name="materyal[]">Resim
<input type="checkbox" value="çizelge" name="materyal[]">Çizelge
<input type="checkbox" value="katlanabilir harita" name="materyal[]"> Katlanabilir Harita
There are checkbox and i'll take values with $_POST['materyal'] and then i want to write screen values with comma if values more than one. if checkbox values empty i don't write anything screen.
if (isset($_POST['materyal']) && !empty($_POST['materyal'])) {
$materyal = $_POST['materyal'];
echo "İçerdiği extra materyaller ; ";
foreach ($materyal as $materyallist) {
foreach ($materyallist as $yenimateryal){
array_push($sonmateryal, $yenimateryal);
}
}
echo implode(", ", $sonmateryal);
}
This is my code. when i want to use implode in if contiditon, I take mistake.
How can i do
It seems like you just want to show the contents?
$sonmateryal = array();
if (isset($_POST['materyal']) && !empty($_POST['materyal'])) {
echo "İçerdiği extra materyaller ; ";
foreach ($_POST['materyal'] as $materyallist) {
foreach ($materyallist as $yenimateryal) {
$sonmateryal[] = $yenimateryal;
}
}
}
echo implode(", ", $sonmateryal);
update according to given HTML
if (!empty($_POST['materyal'])) {
echo "İçerdiği extra materyaller ; ".htmlentities(implode(',', $_POST['materyal']));
}
Since you're posting the inputs as simple array's this will implode the results with a comma if the post-ed value isn't empty..

Integrating Clean code when mixing PHP and HTML [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm finding an organisation a major problem with mixing PHP and HTML, it just looks horrible, so i'm wondering if it's a viable option to create a set of object oriented methods such as this:
class MainOO {
public $Database;
public function __construct($Server,$User, $Password, $DB){
if ($this->Database = new mysqli($Server,$User,$Password,$DB)){
return true;
}
return false;
}
public function User_Login(){
$Get_Usr_Info = $this->Database->prepare("SELECT ID, Password, Salt FROM Users WHERE Username=?");
$Get_Usr_Info->bind_param('s',$_POST['username']);
$Get_Usr_Info->execute();
$Get_Usr_Info->store_result();
$User_Number = $Get_Usr_Info->num_rows;
$Get_Usr_Info->bind_result($UserID, $Stored_Password, $Stored_Salt);
$Get_Usr_Info->fetch();
$Get_Usr_Info->close();
if ($User_Number !== 1){
$Error = "Wrong Username Specified Or Password Is Incorrect";
header ("Location: index.php?Errors=".urlencode($Error));
exit;
}
// Continue with login script
}
public function Logout(){
if (session_status() !== PHP_SESSION_DISABLED){
session_destroy();
header ("Location: LoggedOut.php");
exit;
}
}
}
Then HTML side:
<?php
include "MainOO.php";
$MainOO = new MainOO("host","user","password","database");
?>
<div class="example">
<div class="example left">
<?php
$MainOO->User_Login();
?>
</div>
</div>
It's still mixing PHP & HTML, but it's making look a hell of a lot neater than having heaps of PHP in the middle of HTML.
I'm fully aware I could migrate over to a MVC Framework (which this topic is looking like) already setup, or even use a template engine such as smarty, but I want to avoid this as much as possible.. So is this a viable option to have neater PHP code within html?
You will probably want to use an isset() in the code too
e.g
<?= (isset($variable)) ? $variable : ''; ?>
e.g if variable isset then display it otherwise display nothing

My code is not responding as i want it to be? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
My php is not responding correctly here is my HTML code
<html>
<body>
<form name="myform" method="post" action="lol.php">
<input type="text" name="man" value="">
<input type="submit" name="submit" value= "post">
</form>
</body>
</html>
Here is my PHP code
<?php
if ($_POST['man']= null )
{print ('has no value');}
else {print ($_POST['man']);
}
?>
use == operator for comparison instead of = because the second one is for assignment.
if ($_POST['man']== null )
{
print ('has no value');}
else {
print ($_POST['man']);
}
See PHP Comparison Operators
Change
if ($_POST['man']= null )
to
if ($_POST['man'] == null )
You are receiving any error?
Try:
if (isset($_POST['man'])) {
echo $_POST['man'];
}
else {
echo 'Nothing';
}
And to confirm everything is working use this:
var_dump($_POST);
The you will see the values on $_POST
At First, you should use isset instead of =, second and very important: = is an assignment, but what you want is an comparison, so use == and compare against an empty string

Categories