I visit:
http://www.mydomain.com/test.php?loc=1
This should set $location to be 'VC' and print it out to the screen.
But when I do this, I get a zero '0' instead.
What am I doing wrong, why can't I set a string?
<?php
$loc = $_REQUEST['loc'] ;
if($loc == 1){
$location = 'VC';
echo 'yes';
}
else {
echo "ERROR - Wrong Location code presented";
}
echo 'Location: ' + $location;
?>
I started with:
<?php
$loc = $_REQUEST['loc'] ;
echo 'Location: ' + $loc;
?>
Where I would visit:
http://www.mydomain.com/test.php?loc=VC
and it would do what I want, but that didn't work.
I must be missing something obvious, but I can't see what. help!
You have to change it this:
<?php
$loc = $_REQUEST['loc'] ;
echo 'Location: ' + $loc;
?>
to this:
<?php
$loc = $_REQUEST['loc'] ;
echo 'Location: ' . $loc;
?>
PHP's string concatenation operator is the . dot instead of a + as used in JavaScript, etc.
Use $_GET['loc'] instead of $_REQUEST['loc'] and concetenate with a . instead of a + as Mattedgod already stated.
Why $_GET instead of $_REQUEST?? See: Among $_REQUEST, $_GET and $_POST which one is the fastest?
Related
I'm learning PHP and i'm trying to show an " €" when and only when $autocollant_total_ht_custom isset.
This is what i wrote :
$euro = " €";
if (isset($autocollant_total_ht_custom)) {
$autocollant_total_ht_custom = $autocollant_total_ht_custom . $euro;
} else echo " ";
However my " €" is always showing even when $autocollant_total_ht_custom is not set.
I spent 75 minutes on it, trying and failing again and again despite researching.
I also tried with !is_null, !is_empty with the same result.
I'm fairly certain that my logic isn't wrong but the way to do it is.
Anyone to the rescue?
Have a nice Saturday everyone !
Mike.
Edit 1:
A little visual aid image
My goal was to only show the content of a cell if there was indeed something in it. By default i could see 0 in the empty cells.
if (!$autocollant_total_ht_lot10) {
$autocollant_total_ht_lot10 = " ";
} else echo "error ";
if (!$autocollant_total_ht_lot20) {
$autocollant_total_ht_lot20 = " ";
} else echo " ";
if (!$autocollant_total_ht_lot50) {
$autocollant_total_ht_lot50 = " ";
} else echo " ";
if (!$autocollant_total_ht_custom) {
$autocollant_total_ht_custom = " ";
} else echo " ";
I know my code must look primitive but it works and i don't see it making a conflict with what we are trying to achieve in the initial question.
Then, as asked, this is what i'm writing in the table row and table data :
<tr>
<td class=table_align_left>A partir de 100</td>
<td><?php echo $autocollant_prix ?></td>
<td><?php echo $autocollant_custom?></td>
<td><?php echo $autocollant_total_ht_custom?> </td>
</tr>
So in short, i'm trying to not show anything if there's no value to be shown (which is currently working) and then adding a " €" after the variable is there's something to be shown.
Edit 2 :
My primitive code : my_code
Edit 3 :
The $autocollant_total_ht_custom is already conditioned to be shown earlier in this statement :
} elseif($autocollant_quantité >= 90 && $autocollant_quantité <= 99){
$autocollant_quantité_lot50 = 2;
} elseif($autocollant_quantité >= 100 && $autocollant_quantité <= 1000){
$autocollant_custom = $autocollant_quantité;
} else echo "entrée invalide";
$autocollant_total_ht_custom = $autocollant_prix * $autocollant_custom;
$autocollant_total_ht_lot10 = $autocollant_prix_lot10 * $autocollant_quantité_lot10;
$autocollant_total_ht_lot20 = $autocollant_prix_lot20 * $autocollant_quantité_lot20;
$autocollant_total_ht_lot50 = $autocollant_prix_lot50 * $autocollant_quantité_lot50;
$pointeuse_total_ht = $pointeuse_prix * $pointeuse_quantité;
$pointeuse_autocollant_offert = $pointeuse_quantité * 10;
$pointeuse_autocollant_offert_total_ht = $pointeuse_autocollant_offert * $autocollant_prix;
$pointeuse_autocollant_offert_total_ht = $pointeuse_autocollant_offert * $autocollant_prix;
I posted my code if that can help.
Mike.
//$autocollant_total_ht_custom = null;
$autocollant_total_ht_custom = "something that isnt null";
//if you switch the variable assignment above you will see it behaves as expected.
$euro = "€";
if (isset($autocollant_total_ht_custom))
{
echo $autocollant_total_ht_custom = $autocollant_total_ht_custom . " " .$euro;
}
else
{
//$autocollant_total_ht_custom wouldn't be set at all if we reach this point, this is why im un-sure what your requirements are. Nothing would be echoed.
echo $autocollant_total_ht_custom;
}
Something like this maybe? It's hard to understand your exact requirements.
IsSet checks if a variable is set to something if its not null then it passes the test, and if you're manipulating strings at this variable then it will never be null, meaning the euro sign will always show up.
If the variable IS null then you fail the conditional test, hit else and echo nothing a null string.
If you can update your answer with what you would expect "$autocollant_total_ht_custom" to be set to, I can help better.
EDIT:
Seems to me you can simplify what you what, basically we are only concerned with echoing a string at all if there is something set, otherwise there's no point doing anything, so your checks could be as simple as
$autocollant_total_ht_lot10 = null;
$autocollant_total_ht_lot11 = "";
$autocollant_total_ht_custom = "1,000";
$euro = "€";
if (isset($autocollant_total_ht_custom))
{
echo 'ht custom';
echo TDFromString($autocollant_total_ht_custom, $euro);
}
//notice this doesnt output anything because it isnt set
if (isset($autocollant_total_ht_lot10, $euro))
{
echo 'lot 10';
echo TDFromString($autocollant_total_ht_lot10, $euro);
}
//notice this does because the string, while empty is something that isnt null
if (isset($autocollant_total_ht_lot11))
{
echo 'lot 11';
echo TDFromString($autocollant_total_ht_lot11, $euro);
}
//lets set it to null and see what happens
$autocollant_total_ht_lot11 = null;
if (isset($autocollant_total_ht_lot11))
{
echo 'lot 11 AGAIN';
echo TDFromString($autocollant_total_ht_lot11, $euro);
}
//it doesnt get printed!
//create a function that takes the string in question,
//and for the sake of your use case also the currency to output,
//that way you could change 'euro' to 'currency'
//and have the sign change based on what the value of the $currency
//string is, eg $currency = "£"
function TDFromString($string, $currency)
{
return '<td>' . $string . ' ' .$currency . '</td>';
}
Live example : https://3v4l.org/r5pKt
A more explicit example : https://3v4l.org/JtnoF
I added an extra echo to indicate (and newlines) which variable is being printed out you dont need it of course!
I'll just note the function name is a good example of a bad function name, as it not only returns a td around the string but also inserts the currency, you may want to name it a little better :)
EDIT EDIT:
A final edit outside the scope of your question, you should look into keeping your data in arrays and working on them instead.
Using the previous example we can reduce the code to just this !
$autocollant_total_ht_lot10 = null;
$autocollant_total_ht_lot11 = "";
$autocollant_total_ht_lot12 = "2,0000000";
$autocollant_total_ht_custom = "1,000";
$euro = "€";
//create an array, and stick all our strings in it, from now, if we need to do something to one of the strings(or all!), we do it through the array
$arrayofLots = array($autocollant_total_ht_lot10, $autocollant_total_ht_lot11, $autocollant_total_ht_lot12, $autocollant_total_ht_custom);
//go over each array 'entry' so the first time is '$autocollant_total_ht_lot10', then '$autocollant_total_ht_lot11' etc
foreach ($arrayofLots as $lot)
{
//and we've been over this bit :)
//$lot is a variable we set so we have something to refer to for the individual array entry we are on, we could just as easily name it anything else
if (isset($lot))
{
echo TDFromString($lot, $euro);
}
}
function TDFromString($string, $currency)
{
return '<td>' . $string . ' ' .$currency . '</td>';
}
Good day. It looks like you are missing the end brace
if (isset($autocollant_total_ht_custom)) {
$autocollant_total_ht_custom = $autocollant_total_ht_custom . $euro;
} else {
echo " ";
}
I have a condition within echo statement like this, how to adjust it to make it working:
echo "<option value="http://localhost/myproject/index.php?if(empty($_GET['view'])){echo "view=main-content";}else{$view=basename($_GET['view']);echo "view=".$view;}"></option>";
Many thanks
I noticed that you are using two times view, threfore if you condition enters the else your final url would be something like: ?view=whateverview=whatever2 which is clearly wrong.
$view = (empty($_GET['view']) ? 'main-content' : basename($_GET['view']));
echo "<option value='http://localhost/myproject/index.php?view=" . $view . "'></option>";
If you would like to add more parameters to the URL you'll need to use &.
is this your expected output?
$d = "<option value='http://localhost/myproject/index.php?";
if(empty($_GET['view']))
{
$d .= "view=main-content";
}
else
{
$view=basename($_GET['view']);
$d .="view=".$view;
};
$d .="'>Testing</option>";
echo "<select>". $d."</select>";
You could pass it to a variable, and concatenate it with the rest of your echo statement.
if(empty($_GET['view'])){
$res = 'main-content';
} else {
$res = basename($_GET['view']);
}
echo '<option value="http://localhost/myproject/index.php?view=' . $res . '"></option>';
And since no one else posted it:
echo '<option value="http://localhost/myproject/index.php?view='
. (empty($_GET['view']) ? 'main-content' : basename($_GET['view'])
. '"></option>';
I've got a problem. I'm sure I'm being really stupid but I can't seem to be able to rertive a $_SESSION variable. I run throught the code with a variable called $setup which I post each time as reset. Each time I run through the code I increment $setup so it starts off with no value then has the value 1 and then then value 2. When it's one, I set a SESSION to a posted value. The next time when it's two, the SESSION doesn't seem to have a value.
This is the code when the page is loaded:
<?php
session_start();
$setup=$_POST['reset'];
if ($setup==NULL)
{
$setup=0;
}
elseif ($setup==1)
{
$_SESSION['value1']=$_POST['value1'];
$value1=$_SESSION['value1'];
}
elseif ($setup==2)
{
$value1=$_SESSION['value1'];
$_SESSION['value2']=$_POST['value2'];
$value2=$_SESSION['value2'];
}
?>
When setup is one I can print out value1 however when setup is two is use this code
echo $value2 . " " . $value1 . ".";
All I get is value2 followed by a dot. Am I doing something wrong here?
In this part of your code :
elseif ($setup==2)
{
$value1=$_SESSION['value1'];//HERE
$_SESSION['value2']=$_POST['value2'];
$value2=$_SESSION['value2'];
}
$_SESSION['value1'] is empty so $value1 will be empty too , instead of this i suggest this code:
elseif ($setup==2)
{
if(isset($_SESSION['value1']) $value1=$_SESSION['value1'];
else $value1='Some value for test';
$value2=$_SESSION['value2'];
}
ALSO:
echo $value2 . " " . $value1 ".";
Should be :
echo $value2 . " " . $value1 . ".";//if you want dot in the end
or :
echo $value2 . " " . $value1 ;//without dot int end of line
<?php
session_start();
if (!isset($_POST['reset'])) {
// do nothing or something more useful
}
else {
if ($_POST['reset'] == 1) {
$value1 = $_SESSION['value1'] = $_POST['value1'];
}
elseif ($_POST['reset'] == 2) {
$value1 = $_POST['value1']; // $_POST !!! Not $_SESSION['value1'], which is not set here!
$value2 = $_SESSION['value2'] = $_POST['value2'];
}
}
?>
$value1=$_SESSION['value1']; // you can't do this here ( == 2), because you did not set $_SESSION['value1'] to anything before
It depends on what you want to echo.
Example:
$value1 = "hello";
$value2 = "Richard";
echo $value1." ".$value2;
// will output "hello Richard" (Without the quotes)
//using your code (and syntax corrected)
echo $value1." ".$value2.".";
// will output "hello Richard." (Without the quotes)
If you want the dot to be echoed, you need to surround it with quotes "", and to break in and out of PHP vars/text etc in an echo as above.
You forgot a concatenating dot before "." string
Should be:
echo $value2 . " " . $value1 . ".";
I've fixed it. I has a reset button at the bottom to destroy the session and it had PHP in it. Maybe this question will help anyone else who make this mistake. I can happen.
i simply pass 3 values to the URL and while testing i was trying to echo them back to the screen but it will only echo each value once even though i have set it to echo at various points. once i escape the value it wont let me echo it. Why is this?
<?php
session_start();
if (isset($_SESSION['SESSION_C']) && ($_SESSION['SESSION_C']==true))
{
$getyear = $_GET["Year"];
echo $getyear; (IT WILL ECHO AT THIS POINT)
$getyear = mysql_real_escape_string($getyear);
echo $getyear; (BUT WONT ECHO HERE)
$getsite = $_GET["Site"];
echo $getsite;
$getsite = mysql_real_escape_string($getsite);
echo $getsite;
$getsite = str_replace(' ', '', $getsite);
echo $getsite;
$getdoc = $_GET["Doc"];
echo $getdoc;
$getdoc = mysql_real_escape_string($getdoc);
echo $getdoc;
}
else
{
echo "sessionerror";
}
?>
mysql_real_escape_string() requires a open connection to mysql. Otherwise it will return false. I guess var_dump($getdoc); will give you boolean(false).
You'll have to call mysql_connect() before that code.
I'm trying to echo regular text along with arrays. I'm basing the below code off of the answer found here, but it's not working: Echo arrays with regular text
<?php
$val1 = Yes;
if (($row->relation) == ($val1)) {
echo "<p><b>Applicant\'s Name:</b> {$row['relation_name']} | <b>Business:</b> {$row['relation_business']}</p>";
}
?>
You are doing so many thing wrong here
Example
V--------------------- Row Seems to be Object here
if (($row->relation) == ($val1)) {
echo "<p><b>Applicant\'s Name:</b> {$row['relation_name']}
^---------------------- Calling it as array here
After you clarify the above you can just use printf instead
If its array :
printf("<p><b>Applicant\'s Name:</b> %s|<b>Business:</b>%s</p>",$row['relation_name'],$row['relation_business']);
If its Object
printf("<p><b>Applicant\'s Name:</b>%s|<b>Business:</b>%s</p>",$row->relation_name,$row->relation_business);
You can use . notation which is clearly used for concatenation in php:
<?php
$val1 = Yes;
if (($row->relation) == ($val1)) {
echo "<p><b>Applicant\'s Name:</b>" . $row['relation_name'] . | . "<b>Business:</b>" . $row['relation_business'] . "</p>";
}
?>
or you can seperate HTML and PHP like this:
<?php
$val1 = Yes;
if (($row->relation) == ($val1)) {
?>
<p><b>Applicant\'s Name:</b><?php echo $row['relation_name'] ?>|<b>Business:</b><?php echo $row['relation_business'] ?></p>
<?
}
?>