Hi I have a special problem ... I have three values from database.
Value1 is 1 or 0
Value2 is again 1 or 0
Value3 is remaining time like (24 hours left, 23, ...)
There values are saved in variables:
$value1
$value2
$value3
These values change from DB on every load from webpage. I have these values also inside echo""; The php function is already like:
echo"text text text .................
"Value 1 is:" . $value1 . " and value 2 is:" . $value2 . ""
..................;
I need a function that says different things like
if (value1=0)
echo "Only text"
else
echo "Value 1 is:" . $value1 . " and value 2 is:" . $value2 . "";
this to be in another echo function from first example so in my way it looks like this:
*Some function*
echo"if (value1=0)
echo "Only text"
else
echo "Value 1 is:" . $value1 . " and value 2 is:" . $value2 . """; // two echo closing
But it does not work. Any help will be appreciated.How to solve this? thank you.
Echo is saying to output whatever you type. Saying to output "echo" actually means to display the word "echo". There's no reason to output an output... you already output it!
I think what you want is something more like:
if($value1 == 0){
echo "Only text";
} else {
echo "Value 1 is:" . $value1 . " and value 2 is:" . $value2;
}
Your second piece of code looks fine. Having multiple echo statements is not a problem, and using if...else to choose one is perfectly reasonable.
If you have to use just one for some reason, you can use the (...) ? ... : ... ternary operator.
echo (value1 == 0) ? 'Only text' : "Value 1 is:" . $value1 . " and value 2 is:" . $value2 . """;
function chVal($value1, $value2) {
if ($value1 == 0) {
echo "Only text";
} else {
echo "Value 1 is:" . $value1 . " and Value 2 is:" . $value2;
}
}
Is that what you are looking for?
EDIT: I think I get what you mean.
function chVal ($val) {
if ($val == 0) {
return true;
} else {
return false;
}
}
if ($value1) {
echo "Value 1 is:" . $value1 . "\n";
} else {
echo "Only Text\n";
}
if ($value2) {
echo "Value 2 is:" . $value2 . "\n";
} else {
echo "Only Text\n";
}
Please use value1==0. Basically = means you assign the value 0 into the variable value1.
Why not use something like this:
echo "Value 1 : ".func1($value1)." - Value 2 : ".func2($value2)." - Value 3 : ".func3($value3);
then you may have 3 functions or 1 depending on how complex is your logic.
function func1($value){
if ($value == 0) return " zero ";
else return " else ";
}
Probably in this way,
echo 'if (value1=0)
echo "Only text"
else
echo "Value 1 is:"' . $value1 . " and value 2 is:" . $value2 ;
How about something like:
<?php
$value1=0;
$value2=1;
echo ($value1==0) ? "value 1 is :".$value1." and value 2 is ".$value2 : "only text";
?>
Is this what you're looking for?
I think you're looking to display the PHP code itself: http://se.php.net/manual/en/function.highlight-string.php
<?php
highlight_string('
function chkValue($value1,$value2) {
if($value1 == 0) {
echo "Only Text<br />";
} else {
echo "Value 1 is: ".$value1." and Value 2 is: ".$value2."<br />";
}
}
');
?>
It's very hard to tell what you're trying to achieve, but based on some of your other comments I'm beginning to suspect you want to echo PHP code:
$value = ($value == 0 ? 'Only Text' : "Value 1 is:$value1 and value2 is: $value2");
echo "echo \"$value\";";
update:
Yes thats it! Working but I need different colors for the two texts :-) How to add there?
I'm assuming you're outputting the text to a browser, so something like this will work:
$value = '<span style="color:#'
. ($value == 0 ? 'f00">Only Text' : "0f0\">Value 1 is: $value1 and value2 is: $value2")
. '</span>';
echo "echo \"$value\";";
echo ($value1 == 0)?'Yes':'No';
Related
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.
The code in question :
<?php /*tests added by jason*/
echo "<br />";
echo "count = " . $this->countModules('showcase');
echo "<br />";
echo "hidebyview = " . $hideByView;
echo "<br />";
if($hidebyview == true) {
echo "T";
}
else {
echo "F";
}
echo "<br />";
if ($this->countModules('showcase') && $hideByView == false) {
echo "pass";
}
else {
echo "fail";
}
echo "<br />";
?>
Site 1 output Apache/2.2.22 (Ubuntu) PHP Version 5.3.10-1ubuntu3.7 (where everything works fine):
count = 1
hidebyview =
F
pass
Site 2 output Apache/2.2.13 (Win32) PHP/5.3.26 (where the thing is broken) :
count = 1
hidebyview = 1
F
fail
I guess it boils down to how can the part that evaluates to "fail" evaluate to different answers?
$hideByView == false is not (always) equal to !($hidebyview == true) because of casting and other possible automatic conversions. So your debug info is not really showing you what your expression $hideByView == false evaluates to.
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>
<?
}
?>
I have the following code with the if...else statement within a while loop.
$colour = array("50305A", "E33600", "DBDD02", "73BE58");
$nextcolr = next($colour);
if ($nextcolr == FALSE)
{
reset($colour);
echo current($colour);
}
else
{
echo next($colour);
}
I can't work out why what ever is in the else statement isn't being executed, even if I switch the two statements and reverse the operator. Could anyone help me?
The entire while loop:
while($row = mysql_fetch_array($result))
{
echo "by <a href='/neuro/profile.php?userid=$row[MemberID]'>" . $row['FirstName'] . " " . $row['LastName'] . "</a> on " . $row['Timestamp'] . " | " . $row['NumberOfComments'] . " comments.";
echo "<div id='blog' style='background-color:#";
if ($nextcolr == FALSE)
{
reset($colour);
echo current($colour);
}
else
{
echo next($colour);
}
echo "'><a href='blog.php?threadid=" . $row['tID'] . "'>" . $row['Title'] . "</a></div>";
}
$colour = array("50305A", "E33600", "DBDD02", "73BE58");
while ... {
$nextcolr = next($colour);
if ($nextcolr === FALSE)
{
reset($colour);
}
echo current($colour);
}
is how your while loop should look like. If I am right, you are also defining $colour in the while loop, which might cause problems.
If all this is in the while loop, then you are re-declaring the array on each iteration, thus returning the array internal pointer to the beginning with each iteration.
If you want to iterate this array multiple times, you could do it this way:
$colour = array("50305A", "E33600", "DBDD02", "73BE58");
$i = 0;
while ... {
...
echo $colour[$i++ % count($colour)];
...
}
So you don't need this if-else block.
The problem with your original while loop is that you never change the value of $nextcolr.
Thus, it always remains FALSE and the else part never gets executed.
I am trying to process a form that is dynamically created and therefore varies in length. The while loop seems to work fine. However, the 'if' statement is not; it should only print the startId$i and corId$i if and only if the form's particular text field was filled in. The code is printing a line for every text field on the form, regardless of if it was left empty or not.
$i = 0;
while(!is_null($_POST["startId$i"])){
if(($_POST["startId$i"]) != ""){
echo "startId: " . $_POST["startId$i"] . " ---<br>";
echo "corId: " . $_POST["corId$i"] . " ---<br>";
}
$i++;
}
$i = 0;
while(isset($_POST["startId$i"])){
if( !empty($_POST["startId$i"]) ){
echo "startId: " . $_POST["startId$i"] . " ---<br>";
echo "corId: " . $_POST["corId$i"] . " ---<br>";
}
$i++;
}
Can you manage with fields names ?
If yes, better way is to name inputs with name="startId[0]" and name="corId[0]" and so on...
Then in PHP you just do:
$startIds = $_POST['startId'];
$corIds = $_POST['corId'];
foreach ( $startIds as $k => $startId ) {
if ( !empty($startId) ) {
$corId = $corIds[$k];
echo "startId: " . $startId . " ---<br>";
echo "corId: " . $corId . " ---<br>";
}
}
You should use empty() in this case:
if(!empty($_POST["startId$i"])) {
...
}
I suggest to check the real content of $_POST. You can do that via var_dump($_POST);
You may find out, for example, that the empty fields contain whitespaces. In that case the trim() function may help.
For example:
while(isset($_POST["startId$i"])){
if(trim($_POST["startId$i"])){
echo "startId: " . $_POST["startId$i"] . " ---<br>";
echo "corId: " . $_POST["corId$i"] . " ---<br>";
}
$i++;
}