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.
Related
I am trying to get a PHP file to read a specific line from a text file and then use that line in a string comparison for an if statement.
The second line in the textfield will always have one of two different values. Either &activeGame=0 or &activeGame=1.
The textfile:
boardarray=["NV", "SB", "VB", "NV"]
&activeGame=1
&activePlayer=V
The PHP-file:
$theFile = "test.txt";
$line = file($theFile);
echo $line[1]; //This will output "&activeGame=1" without quotation marks
if ($line[1] == "&activeGame=1") {
echo "The game is active";
} else {
echo "The game is not active";
}
Since echo $line[1] outputs &activeGame=1 I know that the PHP script can read the data from the text file.
The problem is that the if function will echo "The game is not active" and I cant figure out why.
EDIT
SOLUTION:
$theFile = "test.txt";
$line = file($theFile);
echo $line[1]; //This will output "&activeGame=1" without quotation marks
if (trim($line[1]) == "&activeGame=1") {
echo "The game is active";
} else {
echo "The game is not active";
}
The trim function on row 5 is what was missing.
Your problem is that every line of file ends with \n.
You can see it if you var_dump($line[1]) instead of echo it.
So real value for &activeGame=1 is &activeGame=1\n.
That's definitely not equals &activeGame=1.
So, before comparing - use trim function:
$theFile = "test.txt";
$line = file($theFile);
echo $line[1]; //This will output "&activeGame=1" without quotation marks
$line_one = trim($line[1]);
if ($line_one == "&activeGame=1") {
echo "The game is active";
} else {
echo "The game is not active";
}
First Problem was if you echo $line[1] then its value is "&activeGame=1 " ( Notice the whitespace at end . and the optimal Solution Code that will give you desired output is as below
<?php
$theFile = "test.txt";
$line = file($theFile);
echo trim($line[1]); //This will output "&activeGame=1" without quotation marks
$a=trim($line[1]);
$truestr="&activeGame=1";
if ($a == $truestr) {
echo "The game is active";
} else {
echo "The game is not active";
}
?>
OUTPUT
'&activeGame=1'The game is active
I would use parse_str this way even if there are more variables on that line you can always get that value. http://php.net/manual/en/function.parse-str.php
$theFile = "test.txt";
$line = file($theFile);
parse_str($line[1],$output);
if ($output['activeGame'] == 1) {
echo "The game is active";
} else {
echo "The game is not active";
}
Is it possible to have one code block execute when the loop runs throught once, then every time after it run a different code block?
for example, I have:
while ($blank > 0) {
echo "<td></td>";
$blank--;
$day_count++;
}
I would like to change it to this for the first time it runs:
echo "<td class = 'left'></td>";
Then after the first loop through, go to this:
echo "<td></td>";
Is that possible at all?
$firstrow = true;
while ($blank > 0) {
if ($firstrow) {
echo "<td class = 'left'></td>";
$firstrow = false;
} else {
echo "<td></td>";
}
$blank--;
$day_count++;
}
It's not exactly clear from your question what "first time" means for you. But in principle, you should be able to create a variable that indicates whether this is the first time. For example, you can use isset:
while($blank>0) {
if !isset($notTheFirstTime){
...
}
else {
}
$notTheFirstTime = 1
}
The first time you encounter this loop, the variable $notTheFirstTime does not yet exist, and the first block of code is run; once you've been through the loop once, the variable exists. That means this will only run once. On the other hand, if you want to do something "on the first pass through the loop, every time I encounter the loop" then I would recommend just putting the relevant code outside of the loop, rather than inside.
This should work for you:
<?php
//$blank = 3;
//$day_count = 0;
for($i = $blank; $blank > 0; $blank--) {
if($i == $blank)
echo "<td class = 'left'></td>";
else
echo "<td></td>";
$day_count++;
}
?>
Output:
<td class = 'left'></td><td></td><td></td>
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.
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.
I have a small PHP function that is called from one of my pages.
function ratingDetails($uid, $align, $width) {
$queryFull = "SELECT * FROM rating WHERE uid = $uid";
$resultFull = mysql_query($queryFull);
//START DISPLAY TABLE IF RESULTS
if(mysql_num_rows($resultFull) > 0) {
echo "<table class=\"ratingTable\" align=\"center\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\" width=\"550\">\n";
echo "<tr bgcolor=\"#6699cc\"><th>STARS</th><th>DATE RATED</th><th>COMMENT</th><th>RATED BY</th></tr>\n";
while($rowFull = mysql_fetch_array($resultFull)) {
$rating = $rowFull['rating'];
$comment = $rowFull['comment'];
$datePosted = date("M j, Y", $rowFull['date']);
$rid = $rowFull['raterID'];
$rater = getUsername($rid);
//SHOW STARS
if($rating == 0) { $stars = "notYet.jpg"; }
if($rating == 1) { $stars = "starOne.jpg"; }
if($rating == 1.5) { $stars = "starOneHalf.jpg"; }
if($rating == 2) { $stars = "starTwo.jpg"; }
if($rating == 2.5) { $stars = "starTwoHalf.jpg"; }
if($rating == 3) { $stars = "starThree.jpg"; }
if($rating == 3.5) { $stars = "starThreeHalf.jpg"; }
if($rating == 4) { $stars = "starFour.jpg"; }
if($rating == 4.5) { $stars = "starFourHalf.jpg"; }
if($rating == 5) { $stars = "starFive.jpg"; }
//DISPLAY IT ALL
echo "<tr><td width=\"10\"><img src=\"/images/rating/$stars\" width=\"105\" height=\"20\" /></td>";
echo "<td width=\"75\" align=\"center\">$datePosted</td><td>$comment</td><td width=\"85\">$rater</td></tr>\n";
}//END WHILE
echo "</table>\n";
} //END IF
else {
echo "<table class=\"ratingTable\" align=\"center\" border=\"1\" cellspacing=\"0\" cellpadding=\"8\" width=\"550\">\n";
echo "<tr><td align=\"center\"><span class=\"blue\">NO REVIEWS OR RATINGS FOR THIS DISTRIBUTOR</span></td></tr></table>\n";
} //END IF ELSE
}
But when it runs in IE (7 or 8), it throws this error:
A script on this page is causing Internet Explorer to run slowly. Bla bla bla...
I call this function from two pages and both cause the same error. If I remove the call from the page, the page loads fine.
There is no javascript involved with the pages in question...
Help, help, help... I don't have much hair left...
Rick
What's happening is that it's taking a long time to execute your script. IE is waiting for output from the server but it's taking a long time.
You may want to try either a call to flush() every row (or maybe every 25 rows using the modulus operator) or using ob_implicit_flush() to turn on implicit flushing. That way you have data continually coming back to the browser (I assume you have a huge amount of data). You probably will also need to call set_timeout(0) to disable the 30 second time limit on your script.
The first thing I would do is get rid of all those IF statements for the stars, either using an array for them or a switch statement, either of which would reduce the processing required for your script. An array would be my approach. Your code would look something like the following:
function ratingDetails($uid, $align, $width) {
$queryFull = "SELECT * FROM rating WHERE uid = $uid";
$resultFull = mysql_query($queryFull);
// Declare the array for the stars.
$astars = array(
0=> "notYet.jpg",
1=> "starOne.jpg",
1.5=> "starOneHalf.jpg",
2=> "starTwo.jpg",
2.5=> "starTwoHalf.jpg",
3=> "starThree.jpg",
3.5=> "starThreeHalf.jpg",
4=> "starFour.jpg",
4.5=> "starFourHalf.jpg",
5=> "starFive.jpg"
);
//START DISPLAY TABLE IF RESULTS
if(mysql_num_rows($resultFull) > 0) {
echo "<table class=\"ratingTable\" align=\"center\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\" width=\"550\">\n";
echo "<tr bgcolor=\"#6699cc\"><th>STARS</th><th>DATE RATED</th><th>COMMENT</th><th>RATED BY</th></tr>\n";
while($rowFull = mysql_fetch_array($resultFull)) {
$rating = $rowFull['rating'];
$comment = $rowFull['comment'];
$datePosted = date("M j, Y", $rowFull['date']);
$rid = $rowFull['raterID'];
$rater = getUsername($rid);
$stars = $astars[$rating];
//DISPLAY IT ALL
echo "<tr><td width=\"10\"><img src=\"/images/rating/$stars\" width=\"105\" height=\"20\" /></td>";
echo "<td width=\"75\" align=\"center\">$datePosted</td><td>$comment</td><td width=\"85\">$rater</td></tr>\n";
}//END WHILE
echo "</table>\n";
} //END IF
else {
echo "<table class=\"ratingTable\" align=\"center\" border=\"1\" cellspacing=\"0\" cellpadding=\"8\" width=\"550\">\n";
echo "<tr><td align=\"center\"><span class=\"blue\">NO REVIEWS OR RATINGS FOR THIS DISTRIBUTOR</span></td></tr></table>\n";
} //END IF ELSE
The decimals may cause a problem in the array; if so, you can enclose the keys in quotes; e.g., "1.5"=>"starOneHalf.jpg"
The other thing you could do is create a variable for the HTML output instead of echoing the output every line, and then just echo the output at the end.
I found my problem...
The table within my function was assigned a css class. This particular css class contained a behavior, which called PIE.htc. Apparently, this caused some sort of IE melt down whenever it was called. I removed PIE (bummer, no more rounded corners or shadows in IE) and my problem is solved!!!
Thanks to everyone who provided assistance along the way!!!