I have a variable that is posted through a html form:
$_POST['ref']
And a variable that is pulled from a table in a database:
$row['ref']
i have a basic comparison script to check if they are both the same:
$ref = $_POST['ref'];
$result = mysql_query("SELECT * FROM logbook.job");
if (!$result) {
die("Query to show fields from table failed");
}
$row = mysql_fetch_array($result);
$refdb = $row['ref'];
$refform = $_POST['ref'];
echo $_POST['ref'] ."<br>". $row['ref'] . "<br><br>";
if ($refdb == $refform) {
echo "Yes they are<br><br>";
}
else {
echo "No they are not<br><br>";
}
if (is_string($_POST['ref']))
{
echo "Yes";
} else {
echo "No";
}
echo "<br>";
if (is_string($row['ref']))
{
echo "Yes";
} else {
echo "No";
}
Which outputs:
G2mtxW
G2mtxW
No they are not
Yes
Yes
I echo them both out. Than i ask if they are the same. Then i check whether each is a string.
How come they are not the same? How can i get them to match
Any help would be appreciated
Try using the binary-safe comparison for String:
result = strcmp($str1, $str2);
If the result is 0, then both are the same. Otherwise, they aren't.
One of your strings (probably the one from the DB) might be null-terminated. I've tested the following
$foo = "abc\0";
$bar = "abc";
echo "$foo\n$bar\n";
if($foo == $bar)
echo "Equal.";
else
echo "Not equal."
Output is
abc
abc
Not equal.
Try var_dump-ing both values, check their lengths and inspect them using view-source. They are different in someway.
echo $status_message;
echo "Accepted";
if(strcmp($status_message,"Accepted")==0)
{
echo "equal";
}
else
{
echo "not equal";
}
?>
$row['status'] is a field from table
Related
So I am trying to echo the value of a key if the key is present in the array. I've go this code at the moment:
<?php
$lingos = array(
"en"=>"en_US",
"en-gb"=>"en_GB",
"nl"=>"nl_NL",
"de"=>"de",
"da"=>"da",
"es"=>"es",
"ca"=>"es_MX",
"fr"=>"fr",
"it"=>"it",
"pt-pt"=>"pt_PT",
"no"=>"no",
"sv"=>"sv",
"fi"=>"fi",
"et"=>"en_GB",
"is"=>"en_GB",
"cs"=>"cs",
"pl"=>"pl",
"lv"=>"en_GB",
"lt"=>"lt",
"hu"=>"hu",
"ro"=>"ro",
"sr"=>"en_GB",
"hr"=>"en_GB",
"bg"=>"bg",
"el"=>"el",
"uk"=>"en_GB",
"ru"=>"ru",
"tr"=>"tr",
"ar"=>"ar",
"zh-hans"=>"zh_CN",
"zh-hant"=>"zh_TW",
"ja"=>"ja",
"ko"=>"ko",
"id"=>"in",
"ms"=>"ms",
"th"=>"th",
"vi"=>"vi",
"pt-br"=>"pt_PT"
);
// foreach($lingos as $lingo => $x_lingo) {
// echo "Key=" . $lingo . ", Value=" . $x_lingo;
// echo "<br>";
// }
$wmpl_langcode = ICL_LANGUAGE_CODE;
echo $wmpl_langcode;
if (array_key_exists($wmpl_langcode, $lingos)) {
echo $lingos[1];
} else {
echo "not found";
}
?>
Thing is, $lingos[1] is not returning anything. What am I doing wrong?
Presumably $wmpl_langcode is something like en as you are checking for it with array_key_exists so use that as the index:
$wmpl_langcode = ICL_LANGUAGE_CODE;
echo $wmpl_langcode;
if (array_key_exists($wmpl_langcode, $lingos)) {
echo $lingos[$wmpl_langcode];
} else {
echo "not found";
}
Or simpler:
echo isset($lingos[$wmpl_langcode]) ? $lingos[$wmpl_langcode] : "not found";
//PHP 7+
echo $lingos[$wmpl_langcode] ?? "not found";
I was wondering if it's possible to have an if statement within an echo.
I have if statement which works fine when echoing results through the a while loop... This is the statement:
<div><?php if ($row['image'] == '') {}
else {echo "<img src='data:image/jpeg;base64,".base64_encode($row['image'])."'>";} ?>
<?php if ($row['video'] == '') {}
else {echo "<iframe src={$row['video']}></iframe>";} ?></div>`
So basically it's either a video or an image which works fine but then I implemented an infinite scroll to my blog which echoes the data from the database through and if statement like so:
if ($results) {
while($obj = $results->fetch_object())
{
echo '
<div><h3>'.$obj->headline.'</h3> </div>
<div><img src='data:image/jpeg;base64,".base64_encode('.$obj->image.')."'></div>'
So I wondering if anyone knows if it's possible to transfer that if statement within this echo so that it display an image firstly and then knows whether one is present or when a video is present within the database.
Thanks in advance for any help.
PS: I'm very new to coding/php!
Of course. Just split up the echo into multiple statements:
while($row = $results->fetch_object()) {
echo '<div>';
if ($row['image'] == '') {
} else {
echo "<img src='data:image/jpeg;base64,".base64_encode($row['image'])."'>";
}
if ($row['video'] == '') {
} else {
echo "<iframe src={$row['video']}></iframe>";
}
echo '</div>';
}
Try this one.
//first initialize a variable as a string
$result="";
while($obj = $results->fetch_object()) {
$result.="<div>";
if (!empty($obj['image'])){
$result.="<img src='data:image/jpeg;base64,".base64_encode($obj['image'])."'>";
}
elseif (!empty($obj['video'])){
$result.="<iframe src={$obj['video']}></iframe>";
}else{
//show some notification or leave it
//echo 'not Found';
}
$result.="</div>";
}
//finally you need to print the result variable.
echo $result;
I am new to PHP and learning. I'm trying to pass a value through a url link but it doesn't seem to work.
The link value I am passing is http://www.mysite.com/index.php?id=f
I want to run a js script if ID not F seen below but right now when I run it. It doesn't do anything:
<?php
$ShowDeskTop = $_GET['id'];
if (isset($ShowDeskTop)){
echo $ShowDeskTop;
if ($ShowDeskTop != "f"){
echo "ShowDeskTop Value is not F";
echo "<script type=\"text/javascript\">";
echo "if (screen.width<800)";
echo "{";
echo "window.location=\"../mobile/index.php\"";
echo "}";
echo "</script>";
};
};
?>
I know this is easy PHP 101 but I can't figure it out. I have tried everything from w3schools to other sites on Google for the answer and having no luck. Could someone please tell me what I am doing wrong?
Thank you!
$ShowDeskTop is not the same as $ShowDesktop variables names are case sensitive!
This is never gonna work since you set the variable AFTER checking if it exist..
The most easy way:
<?php
if (isset($_GET['id'])) {
echo $_GET['id'];
if ($_GET['id'] != 'f') {
?>
<script type="text/javascript">
if (screen.width < 800) {
window.location = "../mobile/index.php";
}
</script>
<?php
}
}
?>
I don't think <> is valid in PHP (it is in VB.NET ..) the is not operator is != or !== (strict/loose comparison).
Also you don't have to close if statements with a ;
This:
if (expr) {
}
Is valid and not this:
if (expr) {
};
I thought about writing != instead of <>.
You have a number of problems including bad variable case (i.e. variables not matching), checking for variables before they exist, etc. You can simply do something like this:
if (!empty($_GET['id'])) { // note I check for $_GET['id'] value here not $ShowDeskTop
$ShowDeskTop = $_GET['id'];
echo $ShowDeskTop; // note I change case here
if ($ShowDeskTop !== "f"){ // note the use of strict comparison operator here
echo "YES, the id doesn't = f";
echo "<script type=\"text/javascript\">";
echo "if (screen.width<800)";
echo "{";
echo "window.location=\"../mobile/index.php\"";
echo "}";
echo "</script>";
} // note the removal of semicolon here it is not needed and is bad coding practice in PHP - this is basically just an empty line of code
} // removed semicolon here as well
Fist thing, you need ; at the end of echo $ShowDesktop
And, what does f mean in if ($ShowDeskTop <> "f"){
use strcmp() instead of <> operator.
Try
if(!strcmp($ShowDeskTop, "f")){
echo "YES, the id doesn't = f";
}
<?php
$ShowDeskTop = $_GET['id']; // assign before checking
if (isset($ShowDeskTop)){
//echo $ShowDeskTop;
if ($ShowDeskTop !== "f"){
echo "YES, the id doesn't = f";
echo "<script type='text/javascript'>";
echo "if (screen.width<800)";
echo "{";
echo "window.location.replace('../mobile/index.php');"; // assuming your path is correct
echo "}";
echo "</script>";
}
}
?>
for($c=1;$c<=$num;$c++)
{
$row=mysql_fetch_array(mysql_query("SELECT * FROM `$quiztitle` WHERE id=$c"));
if($row['answer']==$_POST['answer'][$c]) // NOT WORKING
{
echo "correct";
echo "<br>";
}
else
{
echo "incorrect";
echo "<br>";
}
}
on the line where it says "NOT WORKING",
the index [$c] does not get the value from the loop.
but when i specify it and change it to $_POST['answer1'], it is working.
what is the correct syntax for this?
Try this.
for($c=1;$c<=$num;$c++)
{
$row=mysql_fetch_array(mysql_query("SELECT * FROM `$quiztitle` WHERE id=$c"));
if($row['answer']==$_POST['answer'.$c]) // NOT WORKING
{
echo "correct";
echo "<br>";
}
else
{
echo "incorrect";
echo "<br>";
}
}
You're treating 'answer' as an array here, looking for an index within.
You want to concatenate the value.
if($row['answer']==$_POST["answer{$c}'])
Based on your note, it looks like you want:
$_POST["answer$c"]
Correct is:
if ($row['answer'] == $_POST['answer' . $c]) {
....
}
I cannot get my PHP script to echo the second else statement if the first result empty.
The way my script currently works is "Print Addresses (from another array) > List Comments", however even if a comment is empty for an address is will either print the comments or nothing, I cannot make the script echo the word "No comment".
if(!empty($row['id']))
{
echo "$row[comment]<br/>";
}
else
{
echo "no comment<br/>";
}
Any help appreciated. Thanks.
Assuming this comes from a database and each row has an id, this will always be true:
if(!empty($row['id']))
Try:
if(!empty($row['comment']))
If id is something else, the same logic applies: check the value that you intend to print:
if (!empty($row['id']) && !empty($row['comment']))
{
echo $row['comment'].'<br/>';
}
else
{
echo "no comment<br/>";
}
EDIT: If this code is looping through all comments attached to a post or something, there will never be any output if there are no comments to loop through. In that case try something like this:
if (count($comments) === 0)
{
echo "no comments<br />";
}
else
{
foreach ($comments as $row)
{
if (!empty($row['comment']))
{
echo $row['comment'].'<br />';
}
else
{
echo "no comment<br />";
}
}
}
OR:
$comment_count = 0;
foreach ($comments as $row)
{
if (!empty($row['comment']))
{
echo $row['comment'].'<br />';
$comment_count++; // We have at least one comment
}
else
{
echo "no comment<br />";
}
}
if ($comment_count === 0) echo 'no comments<br />';