简体   繁体   中英

PHP syntax help on if statement

I'm trying to determine the right syntax from what I'm trying to do with MySQL. I'm basically saying that if a value in a certain column of a row of a table is equal to some session variables, I want to echo out info.

I have a table with subject , description and user . User is set by taking the current user's first name and last name and inserting it into the table under user . This is done by the following code:

$sql="INSERT INTO tbl_name (subject, description, user)
VALUES
('$_POST[subject]','$_POST[description]','$_SESSION[firstname] $_SESSION[lastname]')";

Then once I'm calling this data back out, I want to basically allow the user to delete content that they submitted themselves. The first step for me is to be able to display it in the table. I believe this is just syntax error, but I've confused myself now with how things are set up:

$sql = "SELECT * FROM tbl_name ORDER BY subject, description 
  LIMIT {$startpoint},{$limit}";
$result = $mysqli->query($sql);
$num_rows = mysqli_num_rows($result);

if($num_rows>0){
$field_num = $mysqli->field_count;
echo "<h1>HERE ARE SOME EXAMPLES:</h1>";
echo "<table border='0'><tr>";

for($i=0; $i<$fields_num; $i++)
    {
        $field = mysql_fetch_field($result);
        echo "<td>{$field->subject}</td>";
        echo "<td>{$field->description}</td>";
        echo "<td>{$field->user}</td>";
        if('$field->user' == '$_SESSION[firstname] $_SESSION[lastname]'){
          echo '<td>You can delete this</td>';
        }
    }

I figured the $field->user would equal $_SESSION[firstname] $_SESSION[lastname] because that's how it was initially submitted to the table (without the '.' for concatenation).

Any help would be appreciated. Thanks!

EDIT

Here's the result of my table output code. The results are actually being display with $cell instead of from within the for loop I believe. I've added the if statement in after the but it doesn't seem to recognize echo "<td>".$field->user."</td>"; which makes me think that that is where the problem lies. What I would like to do is be able to add the if statement in a immediately after `echo {$field->user}"; to keep the code clean. I think I've confused myself thoroughly:

if($num_rows>0){
$field_num = $mysqli->field_count;
echo "<h1>HERE ARE SOME JOBS:</h1>";
echo "<table border='0'><tr>";

for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo "<td>{$field->subject}</td>";
    echo "<td>{$field->description}</td>";
    echo "<td>{$field->user}</td>";

    if($field->user == $_SESSION[firstname]." ".$_SESSION[lastname]){
    echo '<td>You can delete this</td>';
    }
    else{
    echo "<td>".$field->user."</td>";
    echo "<td>".$_SESSION[firstname]." ".$_SESSION[lastname]."</td>";
    }
}

echo "</tr>\n";
while($row = mysqli_fetch_row($result))
{
    echo"<tr>";

    foreach($row as $cell)
        echo "<td>$cell</td>";
        echo "</tr>\n";
}
mysqli_free_result($result);
}
else{
echo 'There are no jobs!';
}

I re-wrote the code in a way that was a little bit easier for me to understand (though maybe not the shortest way to do it):

while($row = mysqli_fetch_array($result))
{
    echo"<tr>";
    echo"<td>".$row['subject']."</td>";
    echo"<td>".$row['description']."</td>";
    echo"<td>".$row['user']."</td>";
    if($row['user'] == $_SESSION['firstname']." ".$_SESSION['lastname']){
    echo"<td>You can delete this</td>";
    }
    else{
    echo"<td>Code didn't work</td>";
    }
    echo "</tr>\n";
}

It ended up working this way. If there's way to do this shorter then feel free to post it here otherwise thanks for the help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM