I want to write an if statement inside a string variable, it goes like this :-

$report_list="";
    $sql = mysql_query("SELECT * FROM `reports` ORDER BY `id` DESC");
    $reports_count = mysql_num_rows($sql);
    if($reports_count>0){
        while($row = mysql_fetch_array($sql)){
            $id = $row['id'];
            $by = $row['by'];
            $title = $row['title'];
            $message = $row['message'];
            $date = strftime("%d- %b- %Y",strtotime($row['date']));
            $reports_list.="<tr>
            <td>$by</td>
            <td>$title</td>
            <td>$date</td>
            <td><a href='view_report.php?id=$id'>View Report Message</a></td>
            <td>
            if(file_exists('reports/$id.xlsm')){
            <a href='reports/$id.xlsm' download>Download Attached File</a>'.
            } else if(file_exists('reports/$id.xlsx')){
            <a href='reports/$id.xlsx' download>Download Attached File</a>'.
            }
            </td>
            </tr>";

how can i make this code works? how can i write these if statements properly?

有帮助吗?

解决方案

You can do that using concatenation and ternary operator <cond> ? <if-true> : <if-false>:

$reports_list .= "<tr>...<td>"
              . (file_exists('reports/$id.xlsm')
                ? "<a href='reports/$id.xlsm' download>Download Attached File</a>" : "")
              . (file_exists('reports/$id.xlsx')
                ? "<a href='reports/$id.xlsx' download>Download Attached File</a>" : "")
              . "</td></tr>";

It is up to you to decide whether it is more or less readable than just using several if statements -- this way is suggested more for shorter and simpler conditions.

其他提示

What you want is a ternary operator. I'll give a simplified example so it's more applicable to other situations:

$reports_list .= "<tr><td>$by</td>" . (file_exists('reports') ? "<td>show if true</td>" : "<td>show if false</td>") . '</tr>';

You need to do it in between:

$reports_list.="<tr>
<td>$by</td>
<td>$title</td>
<td>$date</td>
<td><a href='view_report.php?id=$id'>View Report Message</a></td>
<td>";
if(file_exists('reports/$id.xlsm')){
$reports_list.="<a href='reports/$id.xlsm' download>Download Attached File</a>";
} else if(file_exists('reports/$id.xlsx')){
$reports_list.="<a href='reports/$id.xlsx' download>Download Attached File</a>";
}
$reports_list.="</td>
</tr>";

I think it is good practice to provide this variable content in an actual variable.

it gets easier to read ;)

$id = $row['id'];
$by = $row['by'];
$title = $row['title'];
$message = $row['message'];
$date = strftime("%d- %b- %Y",strtotime($row['date']));

$link="";
if(file_exists('reports/$id.xlsm')){
   $link="<a href='reports/$id.xlsm' download>Download Attached File</a>";
} else if(file_exists('reports/$id.xlsx')){
   $link="<a href='reports/$id.xlsx' download>Download Attached File</a>";
}

$reports_list.="<tr>
<td>$by</td>
<td>$title</td>
<td>$date</td>
<td><a href='view_report.php?id=$id'>View Report Message</a></td>
<td>$link</td>
</tr>";
 $report_list="";
    $sql = mysql_query("SELECT * FROM `reports` ORDER BY `id` DESC");
    $reports_count = mysql_num_rows($sql);
    if($reports_count>0){
        while($row = mysql_fetch_array($sql)){
            $id = $row['id'];
            $by = $row['by'];
            $title = $row['title'];
            $message = $row['message'];
            $date = strftime("%d- %b- %Y",strtotime($row['date']));
            $reports_list.="<tr>
            <td>$by</td>
            <td>$title</td>
            <td>$date</td>
            <td><a href='view_report.php?id=$id'>View Report Message</a></td>
            <td>";

            if(file_exists('reports/$id.xlsm'))
         $reports_list.="<a href='reports/".$id.".xlsm' download>Download Attached File</a>";
             else 
          $reports_list.=  "<a href='reports/".$id.".xlsx' download>Download Attached File</a>";


  $reports_list.="</td> </tr>";
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top