How to use array variables as defined variables in PHP? [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I declared an array
$CLISTS=array("Add_Product"=>"products.php","Payment_Type"=>"payment.php","Shipping"=>"shipping.php");
and I defined variables
<?php
define("Add_Product",TRUE);
define("Payment_Type",FALSE);
define("Shipping",FALSE);
foreach($CLISTS as $lists=>$page)
{
if($lists==TRUE)
{
?>
<div class='alert' style="text-decoration:line-through;"><?php echo str_replace("_"," ",$lists);?></div>
<?php }
else
{
?>
<div class='alert'><?php echo str_replace("_"," ",$lists);?></div>
<?php }
}
?>
Its not working. All the div is strikes. What I did mistake?

DEFINE does not do what you think it does. Define creates a named constant.
And you cannot change your array variables with it.
Simply do:
$CLISTS['Add_Product'] = true;
$CLISTS['Payment_Type'] = false;
$CLISTS['Shipping'] = false;
To change your array variables.

You can write the logic like
foreach($CLISTS as $lists=>$page)
{
if($lists == 'Add_Product')
{
?>
Or even you can use === like
foreach($CLISTS as $lists=>$page)
{
if($lists === TRUE)
{
?>

Related

Extra "?>" symbol in PHP file (top left corner) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have an issue with a PHP page which displays a "?>" symbol in the top left corner. It looks like this:
<?php
include_once("DBconnect.php");
$getuser = $_POST["RegUsername"];
$getpass = $_POST["Pass"];
$getrepass = $_POST["RePass"];
$getemail = $_POST["Email"];
if($getuser){
if($getpass){
if($getrepass){
if($getpass == $getrepass){
if($getemail){
$code = rand();
if(mysqli_query($link, "INSERT INTO users VALUES ('', '$getuser', '$getpass', '$getemail', '0', '$code')")){
echo "INFO1";
}
else{
echo "ERROR6";
}
}
else{
echo "ERROR5";
}
}
else{
echo "ERROR4";
}
}
else{
echo "ERROR3";
}
}
else{
echo "ERROR2";
}
}
else{
echo "ERROR1";
}
?>
And I use this jQuery function to display the PHP returned value in my HTML page:
$("#RegSubmit").click(function(){
$.post( $("#RegForm").attr("action"),
$("#RegForm :input").serializeArray(),
function(info){
$("#RegErrorLog").empty();
$("#RegErrorLog").html(info);
});
$("#RegForm").submit(function(){
return false;
});
});
I always get the "?>" in front of the PHP "ERROR" returned value.
How can I get rid of that? Or how can I return a value from the PHP file using a variable instead of echo
I guess there's a problem in your DBconnect.php file.
Apart from that... you should really think about validating values taken from Http POSTs in your PHP script, before using them in db queries.
Check if you are not printing that symbol on the included file "DBconnect.php".

File name as variable+.txt [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 8 years ago.
Improve this question
session_start();
$user = $_SESSION['username'];
if( isset($_POST['subm_btn']) ) {
incrementClickCount();
}
function getClickCount()
{
return (int)file_get_contents($user.".txt");
}
function incrementClickCount()
{ $count = getClickCount() + 1;
file_put_contents($user.".txt", $count);
}
User register on my site, then he click on button (name="subm_btn"). I want count clicks and add number of clicks in file with name "username.txt"
I guess you are looking for something like this:
$file=$user.'.txt';
incrementClickCount($file);
function incrementClickCount($file){
$count = getClickCount($file) + 1;
file_put_contents($file, $count);
}
function getClickCount($file) {
return (int)file_get_contents($file);
}
If you want the variable to be available inside a function you either make it global or pass it as an argument (which is better).
You define $user, and then access $user1. That would be my guess as to why it doesn't work. Also, using $file might be a better idea anyway.

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

What is wrong with this javascript script with a php declaration [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
I would like to set a javascript variable 'logged_in' to either 1 or 0, depending on whether a user is logged in Wordpress. This is what I have:
<script type="text/javascript">
var logged_in = <?php if ( is_user_logged_in() ) { echo '1';} else {echo '0';} ?>;
</script>
But it's not working. What's wrong with it?
I just checked your coding. Your code will define the variable you declared in your script as 1 if true and 0 if false. Keep in mind that you are only defining a variable and it will not do anything as it is. Here is what I have. Just take a look your source code.
<?php
function is_user_logged_in() {
return true;
}
?>
<script type="text/javascript">
var logged_in = <?php if ( is_user_logged_in() ) { echo '1';} else {echo '0';} ?>;
</script>
If it is not setting the variable make sure that the function is_user_logged_in() exist and is available on the script.

Trying to echo a variable ( $i) into another variable [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 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"}

Categories