0

Hi i have small project and predefined format because of which i want to select from selectbox and pass this value so that i can fetch the data from database and echo it in html. I am able to pass the select value and also be able to fetch the data from database but not able to echo it in html.And i am getting multiple values from database. Any solution ? thanks.. Here is my code:

dashborad.php

if($param['aktion'] == 'save-widget-news')
{
    //$param['news'] 
    //UPDATE SQL...

    $page['register-news'] = array(
        1   => array( 'News','aktiv',$page['script'],'',''),
        0   => array( 'Edit News','enabled',$page['script'],''),    
    );
    $selectValue = $_POST['news'];
    if(isset($_POST['saveId']))
    {
     if(($selectValue)==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);

}
 if(($selectValue)==6){

      $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( ) ) +42
DAY AND curdate( )
";

$sql_select=mysql_query($sql);

}

if(($selectValue)==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);

}
$html = '<table width="538" cellspacing="0" cellpadding="0" border="0">
        <tr>
            <td>
                <div>'.CreateRegister($page['register-news']).'</div>
                '.CreateMessage().'
                <div class="cont-liste-verlauf register">                       
                    <table id="news">
                    <div class="welcome-rahmen krz toggleNews" id="news_261_kurz"> ';
                    while($row = mysql_fetch_assoc($sql_select)){
                        $news = $row['headline'] . " " .$row['datum_archiv'] ; 
                        $html = '<p class="welcome-subheadline"><input type="text" name="type" value="'. $news .'" ></p>';  
                        }}                                                                                  
                    $html = '</table>                                           
                </div>
            </td>
    </tr>
    </table>';


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

    echo json_encode($return);
    die();
    $param['aktion'] = 'get-widget-news';
}

if($param['aktion'] == 'get-widget-news')
{
    $newsId = 1;
    $page['register-news'] = array(
        1   => array( 'News','aktiv',$page['script'],''),
        0   => array( 'Edit-News','enabled',$page['script'],'',''), 
    );
        $param['aktion'] = 'save-widget-news';
        $html = '<table width="538" cellspacing="0" cellpadding="0" border="0" >
            <tr>
                <td>
                <div>'.CreateRegister($page['register-news']).'</div>
                '.CreateMessage().'
                <div class="cont-liste-verlauf register">               
                <table id="news">
<div class="welcome-rahmen lng toggleNews" id="news_269_kurz">
<a href="news.php?id=269" class="TrackNews" id="01" target="_blank">
<p class="welcome-breadcrump">Montag, 19.05.2014</p>
<p class="welcome-subheadline">Teilnahme von MAN Top Used an der Samoter 2014</p>
<div class="newsText">
<p class="welcome-text"><img src="http://intern.autodo.de/admin/news/man-it.jpg" width="165" class="text_fixed" border="0"></p>
<p class="welcome-text">Die 29. Internationale Erd- und Bautechnik-Ausstellung Samoter fand zwischen dem 8. und 11. Mai in Verona statt und zog rund 100.000 Besucher an. Samoter ist die wichtigste italienische Messe ihrer Art, die den Themen Erdbewegung, Hochbau und Baumaschinen gewidmet ist. Zugleich ist diese Veranstaltung damit auch f? europ?chen Markt bedeutsam.</p>
</div>
</div>
</a>

</table>
</div>
</td>
</tr>
</table>';

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

    echo json_encode($return);
    die();
}

dashboard.js

function saveNewsWidget()
    {
        var selectBoxValue = $('select[name="news"]').val();
        $.ajax({
        type: "POST",
        url: "ajax/dashboard.php",
        dataType : 'json',
        cache: false,
        data: {'aktion' : 'save-widget-news', 'news' : selectBoxValue},
        success: function(data)
        {
            //getNewsWidget();
            $('#news').html(data['html']);

        }
      });
  }

    function getNewsWidget()
    {
        $.ajax({
        type: "POST",
        url: "ajax/dashboard.php",
        dataType : 'json',
        cache: false,
        data: {'aktion' : 'get-widget-news'},
        success: function(data){
            //alert(data);
            $('#news').html(data['html']);
        },
        error: function(data){
            alert('error');
            //$('#news').html(data.html);
        }
      });
  }

2 Answers 2

1

Always try to format your code properly so the reader can understand which statement/clause ends where, i found a few issues in your code which might be the reason of the issue you are having

  • Your if(isset($_POST['saveId'])) statement body ends right after the while statement body, while there is still some code remaining which should logically be executed within the body of the if statement

  • You are not concatenating the previously set data of $html to the newly set $html data, so only the last bit will stay in the variable

Here is the altered code, i didn't test it but i fixed the two issues i found and applied some formatting so other can understand it better

if($param['aktion'] == 'save-widget-news')
{
    //$param['news'] 
    //UPDATE SQL...

    $page['register-news'] = array(
        1   => array( 'News','aktiv',$page['script'],'',''),
        0   => array( 'Edit News','enabled',$page['script'],''),    
    );
    $selectValue = $_POST['news'];
         if(($selectValue)==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);
        }
        if(($selectValue)==6)
        {
            $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( ) ) +42
            DAY AND curdate( )
            ";

            $sql_select=mysql_query($sql);
        }

        if(($selectValue)==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);

        }

        $html = '<table width="538" cellspacing="0" cellpadding="0" border="0">
                <tr>
                    <td>
                        <div>'.CreateRegister($page['register-news']).'</div>
                        '.CreateMessage().'
                        <div class="cont-liste-verlauf register">                       
                            <table id="news">
                            <div class="welcome-rahmen krz toggleNews" id="news_261_kurz"> ';
                            while($row = mysql_fetch_assoc($sql_select)){
                                $news = $row['headline'] . " " .$row['datum_archiv'] ; 
                                $html .= '<p class="welcome-subheadline"><input type="text" name="type" value="'. $news .'" ></p>';  
                                }                                                                                 
                            $html .= '</table>                                           
                        </div>
                    </td>
            </tr>
            </table>';


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

        echo json_encode($return);
        die();
    $param['aktion'] = 'get-widget-news';
}
Sign up to request clarification or add additional context in comments.

4 Comments

Hi @Junaid you have corrected the errors but still i dont know why its directly passing to this condition and showing the result from: $param['aktion'] = 'get-widget-news'; instead of going through while loop and sql condition...
Because maybe the if(isset($_POST['saveId'])) isn't satisfied and hence the whole body is skipped, are you sure you are sending saveId using POST method? if not then try changing it to $_REQUEST['saveId'], also change $_POST['news'] to $_REQUEST['news'];
i have share more code with you in question..i dont know why everytime i click it goes to $param['aktion'] = 'get-widget-news'; condition instead of going to 'save-widget-news' where i have my sql query and conditons..
Its because from the saveNewsWidget function you are only sending value for aktion and news while the if statement is expecting for a posted saveId, either remove the if statement or add one send saveId with the request, i have changed the code and removed the if statement
1

You are affecting the same variable multiple times :

while($row = mysql_fetch_assoc($sql_select)) {
    $news= $row['headline'] . " " .$row['datum_archiv'] ;
}

Put your html code in while loop :

$html = null;

while($row = mysql_fetch_assoc($sql_select))
{
    $news = $row['headline'] . " " .$row['datum_archiv'] ;
    $html .= '<table width="538" cellspacing="0" cellpadding="0" border="0">...</table>';
}

EDIT :

You cannot insert your while like you have done. Concatenation is for string not for a loop. Also echo isn't needed when you use concatenation.

$html = '<table width="538" cellspacing="0" cellpadding="0" border="0">
           <tr>
             <td>
               ...';


while($row = mysql_fetch_assoc($sql_select))
{ 
    $news = $row['headline'] . " " .$row['datum_archiv'];

    $html .= '<p class="welcome-subheadline"><input type="text" name="type" value="'. $news .'" ></p>';
}                                                                                   

$html .= '</table>......';

8 Comments

i think you are right but as i write my html code inside php quote so in this case how can i loop and echo the value ? can you please tell me ? i tried in few ways but not able to echo anything ..
yes i want it thanks i am trying to do according to your way i think it should work now but last thing how do i echo it ? I mena how cvan i write echo statement inside my html code ?
Not sure to understand. You want to make and echo of html in dashborad.php to see if it's correct and see it when you make your ajax call ? If so, you can make in success of ajax call : console.log(data.html) or launch your file directly (without ajax) to see what's happen.
Hi i am making a write ajax call, i checked it in firebug.I have updated my question.As per your answer i write while loop inside html but now i am getting an unexpected error as unexpected T_WHILE . I am only making an mistake of writing php code inside html and echoing it..
I've modified my answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.