I'm trying to add 'NEW' to unread messages in the inbox. I don't think I can use an if statement within an echo and I have tried and failed to understand the proper use of ternary operators. I would appreciate any help. Thanks a lot.
If read_yet = 'no' I want to add this code as another echo statement:
echo '<td>NEW</td>' ;
Here is my current code for the inbox.
$sql = mysqli_query($con, "select * from messages
WHERE deleted = 'no' AND to_user = '" . $_GET['username'] . "'
ORDER BY id DESC");
while ($row = mysqli_fetch_array($sql)){
echo '<tr>';
echo '<td><a href="profile.php?username=' ;
echo $row['from_user'] ;
echo '">';
echo '<img id="usersimage" src="images/profileimg/thumbnail_'.$row['from_user'];
echo '.jpg"</a></td>' ;
echo '<td><a href="users.php?username=' ;
echo $row['from_user'] ;
echo '">';
echo $row['from_user'];
echo "</a></td><td><a href='messageid.php?id=".$row['id']."'><font color='#b2ccdb'>" .$row['subject'] ;
echo '</a></font></td>';
echo '</tr>';
}
If you are adding another column, I would not suggest using the ternary operator and instead simply using if:
if ($row['read_yet' == 'no') {
echo '<td>NEW</td>';
}
However, if you always output this column and need to change the display, you could use the ternary operator.
echo '<td>' . ($row['read_yet' == 'no' ? 'NEW' : 'READ') . '</td>';
Note: The ternary can create line noise. As such, I prefer a temporary variable, helper function, or traditional if.
A ternary like this:
$foo = ($bar) ? 'true result' : 'false result';
is functionally equivalent to:
if ($bar) {
$foo = 'true result';
} else {
$foo = 'false result';
}
Related
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 am a beginner when it comes to php.... I have the following:
I am not sure if it is my if statement expressed wrong or just at the wrong place. I want it not to show the records when the companyname == the businessname! Please advise?
if (mysql_num_rows($sql) > 0 && **($row['companyname'] == $user_data['businessname'])** )
{
while ($row = mysql_fetch_array($sql)){
if (($employed =='1')){
echo '<h4> ID : '.$row['idnumber'] ;
echo '<br> First Name : '.$row['firstname'];
echo '<br> Last Name : '.$row['lastname'];
echo '<br> Reference 1 : '.$row['ref1'];
echo '<br> Reference 2 : '.$row['ref2'];
echo '<br> Reference 3 : '.$row['ref3'];
echo '<br> Gender : '.$row['gender'];
echo '<br> Company : '.$row['companyname'];
echo ' </h4>';
echo '<br />';
echo '<h2>Some Additional Options</h2>';
echo '<br />';
include 'includes/admenu.php';
}
}
}
else
{
print ("$XX");
}
The $row variable doesn't exist outside your loop, so it's pointless trying to use it in the if statement. You need to move it inside the loop, like so:
while ($row = mysql_fetch_array($sql))
{
if ( ($employed =='1') && ($row['companyname'] == $user_data['businessname']) )
{
// code ...
}
}
I want it not to show the records when the companyname == the businessname!Move the statement inside your loop:
I'm not actually sure if you're trying to display when companyname == the businessname or companyname != the businessname. Either way, change your if condition accordingly and it should work.
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 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>";
}
}
?>
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.