-2

Hi i have small project which will give you news from past 4,6,10 weeks by using select option in html. I am getting an syntax error, unexpected T_STRING in select option tag. I understand the error as i cant use php inside open php tag. But i dont know how can i solve this error. Any help.Thanks. Here is my code:

<?php
$page['doctype'] = true;
$param = array_merge($_GET, $_POST);

$return = array();
if($param['aktion'] == 'edit-news')
{
    $page['register-edit-news'] = array(
    1   => array( 'Edit-News','aktiv',$page['script'],'',''),
);


if(isset($_POST['BtnSubmit']))
   {

      if(($_POST['news'])==4){

      $sql=" SELECT DISTINCT ad_news_texte.headline, ad_news.datum_archiv
FROM ad_news_texte
INNER JOIN ad_news_oe ON ad_news_texte.news_id = ad_news_oe.id_ad_news
INNER JOIN ad_news ON ad_news_oe.id_ad_news = ad_news.id
WHERE ad_news.datum_archiv
BETWEEN curdate( ) - INTERVAL DAYOFWEEK( curdate( ) ) +28
DAY AND curdate( )
";

$sql_select=mysql_query($sql);
while($row = mysql_fetch_array($sql_select)) {
  echo $row['headline'] . " " .$row['datum_archiv'] ;
  echo "<br>";
}
}


if(($_POST['news'])==10){

      $sql=" SELECT DISTINCT ad_news_texte.headline, ad_news.datum_archiv
FROM ad_news_texte
INNER JOIN ad_news_oe ON ad_news_texte.news_id = ad_news_oe.id_ad_news
INNER JOIN ad_news ON ad_news_oe.id_ad_news = ad_news.id
WHERE ad_news.datum_archiv
BETWEEN curdate( ) - INTERVAL DAYOFWEEK( curdate( ) ) +70
DAY AND curdate( )
";

$sql_select=mysql_query($sql);
while($row = mysql_fetch_array($sql_select)) {
  echo $row['headline'] . " " .$row['datum_archiv'] ;
  echo "<br>";
}
}



$html =     
'
<body bgcolor="#FFFFFF">
<table width="100%" border="0" cellspacing="0" cellpadding="0">

<tr>
<td class="welcome-page-hint">

<table width="538" cellpadding="0" cellspacing="0" border="0">
<tr>
<td>

<h3>News</h3>

<form name="UserInformationForm" method="POST" action="#">

      <select name="news">
         <option value="4" <?php if($_POST['news']=="4") echo "selected=selected"; ?>>Show news from the last 4 weeks</option>
         <option value="6" <?php if($_POST['news']=="6") echo "selected=selected"; ?>>Show news from the last 6 weeks</option>

      </select>
      <br/><br/>
      <input name="BtnSubmit" type="submit" value="Submit">
</form>

</td>
</tr>
</table>

</td>
</tr>

</table>';
$return = array(
        'status' => 1,
        'html'  => $html
    );

    echo(json_encode($return)) ;
}
?>
user3702602
  • 127
  • 3
  • 15

2 Answers2

0

You can't have php code inside a variable in php code.

What you should do is to escape the string and then concatenate.

<option value="4" '. (($_POST['news']=="4") ? "selected=selected" : "") .'>Show news from the last 4 weeks</option>
<option value="6" '. (($_POST['news']=="6") ? "selected=selected" : "") .'>Show news from the last 6 weeks</option>

Instead of trying to put a new php code block in there I have just escaped the string, used ternary operator and then concatenated the 3 parts.

Dharman
  • 26,923
  • 21
  • 73
  • 125
  • Hi @Dharman do i need to used If clause also in option tab because i am not getting outout now by clicking on button and i have to echo the output right in option tag ? – user3702602 Jun 10 '14 at 08:37
  • @user3702602 I don't know why you are not getting the right output, but I suppose that is a separate issue. – Dharman Jun 10 '14 at 13:20
0

You are starting $hmtl = ' and inside your <option value="4" <?php if($_POST['news']=="4") you are closing the single quote at $_POST[', and that is messing up your HTML and PHP code as the $html, a string variable, is closed. You need to either close it with ; or escape the single quote with a \'. That should fix it.

contactsunny
  • 641
  • 7
  • 11
  • Hi i did '. (($_POST['news']=="4") ? "selected=selected" : "") .' as Dharman said, its working but unable to echo the output .. – user3702602 Jun 10 '14 at 08:40