IF within a WHILE (USE OF UNDEFINED CONSTANT) - php

I have a suspended boolean for each member of staff. when displaying staff members in a table i want to show either the text "SUSPENDED" or "NOT SUSPENDED" rather than 1 or 0.
I keep receiving the error,
Notice: Use of undefined constant Staff_Suspension - assumed 'Staff_Suspension'
Im sure this is simple im fairly new to php, just stuck and dont want to waste anymore time trying to work this out and not get anywhere. help appreciated
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "\t<tr>\n";
echo "\t\t<td> $row[Staff_ID] </td>\n";
echo "\t\t<td> $row[Staff_Forename] </td>\n";
echo "\t\t<td> $row[Staff_Surname] </td>\n";
echo "\t\t<td> $row[Staff_Email] </td>\n";
echo "\t\t<td>";
if ($row[Staff_Suspension] == 1){
echo 'Suspended';
} else if ($row[Staff_Suspension] == 0){
echo 'Not Suspended';
}
echo "</td>\n";
echo "\t\t<td> $row[Staff_Delete_Permissions] </td>\n";
echo "\t</tr>\n";
}

You must quote the indexes. Otherwise, PHP assumes you've defined a constant named Staff_Suspension. If no such constant exists, it then assumes you meant to specify a string literal. Quoting takes away any guess work (and, hence, any notice):
if ($row['Staff_Suspension'] == 1) {
echo 'Suspended';
} elseif ($row['Staff_Suspension'] == 0) {
echo 'Not Suspended';
}
or, simplified:
echo $row['Staff_Suspension'] ? 'Suspended' : 'Not Suspended';

Answer:
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "\t<tr>\n";
echo "\t\t<td>". $row['Staff_ID'] ".</td>\n";
echo "\t\t<td>". $row['Staff_Forename'] ."</td>\n";
echo "\t\t<td>". $row['Staff_Surname']". </td>\n";
echo "\t\t<td>". $row['Staff_Email'] ".</td>\n";
echo "\t\t<td>";
if ($row['Staff_Suspension'] == 1){
echo 'Suspended';
} else if ($row['Staff_Suspension'] == 0){
echo 'Not Suspended';
}
echo "</td>\n";
echo "\t\t<td>". $row['Staff_Delete_Permissions'] ".</td>\n";
echo "\t</tr>\n";
}
Roughly Explained
The use of constants comes mainly from define(); which you do not require a variable for constant values
the reason you are being presented with this error comes from the lines:
if ($row[Staff_Suspension] == 1){
echo 'Suspended';
} else if ($row[Staff_Suspension] == 0){
echo 'Not Suspended';
}
Because you are specifying a key of an array by the name. You should wrap this in quotes, double or single is up to you.
Example of a constant:
define ('Name', 'ConstantValue');
echo Name;
This will output : ConstantValue
now, from reading the above:
http://php.net/manual/en/function.define.php
the link is a rough explination on constant values.
Now for your specific question.
$row[Staff_Suspension] You have defined a constant value, since this is a column name, this should be wrapped in quotes.
If you was specifying from the key number: $row[0]; this is a different story which is irrelevant to your question.

Related

Utilizing PHP Explode is not dumping out parsed string

I have a database where I am storing a long string in one column. I am using two different delimiters. I am using $$ to separate the 4 inputs user can input. I am using ** to separate between each time the user enters the inputs (keep up with historical). I am pulling the data and trying to display it within my php file. Here is a portion of my code. My code is working when no ** delimiter is found (only $$ is found). However, when I enter the if statement due to finding **, my code quits printing data out. I get no errors just a blank section on my page. Do I have something set wrong in the while ($lUB < $bSize){} portion?
if ($userData[$index] != "")
{
if (strpos($userData[$index], '**') != false)
{
echo "here 0";
$userDataB = explode("**", $userData[$index]);
$lUB = 0;
$bSize = count($userDataB);
while ($lUB < $bSize)
{
$dataparse = explode("$$", $userDataB[$lUB]);
echo ("<tr>");
echo("<td>");
echo $dataparse[1];
echo("</td>");
echo("<td>");
echo $dataparse[2];
echo("</td>");
echo("<td>");
echo $dataparse[0];
echo("</td>");
echo("</tr>");
unset($dataparse);
$lUB = $lUB+1;
}
}
else
{
echo "here 1";
$dataparse = explode("$$", $userData[$index]);
echo ("<tr>");
echo("<td>");
echo $dataparse[1];
echo("</td>");
echo("<td>");
echo $dataparse[2];
echo("</td>");
echo("<td>");
echo $dataparse[0];
echo("</td>");
echo("</tr>");
}
}
else
{
echo "here 2";
}
If string is at the start of another string, strpos will return 0, which is equal to false in an if block, either you should use === operator or try to use some other function.

How To Parts Of Speech Only One Time In PHP

I want to show only one time the heading of parts of speeches for example when user enter the word "Spell" all the parts of speeches and its meaning come every time .
The headings are repeating multiple times . i want to print only one time
There should be a heading Of Noun,Verb,Adjective etc..
like this
Noun.
Verb.
adjective.
this is my code
while($row=mysql_fetch_array($sql)){
echo "<div id='wordid' style='display:none'>".$row["wordid"]."</div>";
echo $count++." : ".$row['definition']. " ";
if($row['pos']=="n"){
echo "(Noun) <br/>";
}
if($row['pos']=="s"){
echo "(Subject) <br/>";
}
if($row['pos']=="p"){
echo "(Proverb) <br/>";
}
if($row['pos']=="a"){
echo "(Adjective) <br/>";
}
if($row['pos']=="v"){
echo "(Verb) <br/>";
}
}
You can keep track of what the last wordid was and only echo the header if it changed.
// initialize to avoid notices
$lastWordId = '';
while($row=mysql_fetch_array($sql)){
if ($lastWordId != $row['wordid']) {
echo "<div id='wordid' style='display:none'>".
$row["wordid"].
"</div>";
$lastWordId = $row['wordid'];
}
echo $count++." : ".$row['definition']. " ";
if($row['pos']=="n"){
echo "(Noun) <br/>";
}
if($row['pos']=="s"){
echo "(Subject) <br/>";
}
if($row['pos']=="p"){
echo "(Proverb) <br/>";
}
if($row['pos']=="a"){
echo "(Adjective) <br/>";
}
if($row['pos']=="v"){
echo "(Verb) <br/>";
}
}

Need help showing code results in a table

I'm trying to get this
for ($r = 1; $r <= 10; $r++) {
if ($shirt_info[$r][0] != "Select One")
{
echo "<div class='shirtinfo'>Order $r: </div>";
echo "<div class='shirtinfo'>Size: ".$shirt_info[$r][0]."</div>";
echo "<div class='shirtinfo'>Price: ".$shirt_info[$r][1]."</div>";
echo "<div class='shirtinfo'>Qty: ".$shirt_info[$r][2]."</div>";
echo "<div class='shirtinfo'>SPE: ".$shirt_info[$r][5]."</div>";
echo "<div class='shirtinfo'>LIA: ".$shirt_info[$r][6]."</div>";
echo "<div class='shirtinfo'>Vendor: ".$shirt_info[$r][7]."</div>";
echo "<div class='shirtinfo'>Style: ".$shirt_info[$r][8]."</div>";
echo "<div class='shirtinfo'>Color: ".$shirt_info[$r][9]."</div>";
echo "<div style='clear:both;'></div>";
}
}
to show in a table and I'm not really sure how. I keep getting a syntax error.
The code you are showing us has an unterminated for loop. Maybe this is the syntax error.
Your for does not end with }
I would also suggest that you use table and not div if you want to display a table.

PHP & MySQL - Compare IPs that are stored

I am trying to create a system to store the last logged on IP, and to compare it to the current IP, then do some functions later on down the road, but currently, I cannot compare them without going to the last else statement. Here's the code.
<?php
$userToPull = $session->userinfo['username'];
$query = "SELECT * FROM users WHERE username='$userToPull'";
$result = mysql_query($query);
while($row = mysql_fetch_row($result)){
$userToShow = $row[25];
$IPtoVerify = $row[26];
}
$lastActivity = RelativeTime($userToShow);
$currIP = $_SERVER['REMOTE_ADDR'];
/*
Shows Partner Stuff
}elseif(!$session->isAdmin()){
echo "<div style='text-align:right;' id='homebox'";
echo "<b>Partner Total:</b> ".$database->getNumMembers()."<br>";
echo $database->num_active_users." partners logged in and ";
echo $database->num_active_guests." guests viewing the site.<br><br>";
echo "</div>";
*/
if(!$IPtoVerify == $currIP){
echo "<div style='text-align:right; background-color: #FAAAB3' id='homebox_partner'";
echo "<b>You are logged on from an unrecognized location.</b><br>";
echo "You will be sent a notification for security purposes.";
echo "<br>This location will automatically be remembered.";
echo "</div><br>";
}elseif($IPtoVerify == $currIP){
echo "<div style='text-align:right;' id='homebox_partner'";
echo "<b>You are logged on from a recognized location.</b><br>";
echo "Your IP is ".$_SERVER['REMOTE_ADDR'];
echo "<br>Your last login was approximately ".$lastActivity;
echo "</div><br>";
}else{
echo "<div style='text-align:right;' id='homebox_partner'";
echo "<b>An error has occurred.</b><br>";
echo "</div><br>";
}
?>
The only thing not working is the if(!$IPtoVerify == $currIP){ if statement.
The IP is stored in the normal fashion, and echo's like: 100.100.100.100. (normal fashion)
Maybe I am not comparing them right, but it has worked for me in the past.
This code doesn't do what you think:
if (!$IPtoVerify == $currIP) {
PHP interprets it as:
if ((!$IPtoVerify) == $currIP) {
You will want to write it as:
if ($IPtoVerify != $currIP) {
Try ($IPtoVerify !== $currIP) instead of (!$IPtoVerify == $currIP)
!$IPtoVerify == $currIP
means
0==$currIP,
because it first validates
`!$IPtoVerify`
which always returns 0 unless $IPtoVerify is 1.
Add additional brackets like
if(!($IPtoVerify == $currIP))...
to solve the issue.

Comparing 2 exact same strings returns false

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

Categories