php在html中循环时出现循环错误

时间:2014-06-18 09:13:03

标签: javascript php html mysql ajax

您好我有小项目和预定义格式,因为我想从selectbox中选择并传递此值,以便我可以从数据库中获取数据并在html中回显它。我能够传递选择值,也能够从数据库中获取数据但不能在html中回显它。我从数据库中获取多个值。有解决方案吗谢谢.. 这是我的代码:

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 个答案:

答案 0 :(得分:1)

您多次影响同一个变量:

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

将你的html代码放入while循环:

$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>';
}

编辑:

你不能像你一样插入你的时间。 连接用于字符串而不是循环。 使用串联时也不需要echo。

$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>......';

答案 1 :(得分:1)

总是尝试正确格式化代码,以便读者能够理解哪个语句/子句在哪里结束,我在代码中发现了一些问题,这可能是您遇到问题的原因

  • 您的if(isset($_POST['saveId']))语句正文在while语句正文之后结束,而仍有一些代码仍然应该在if语句的正文中执行

  • 您没有将先前设置的$html数据连接到新设置的$html数据,因此只有最后一位将保留在变量中

这是更改后的代码,我没有测试它,但我修复了我发现的两个问题并应用了一些格式,以便其他人可以更好地理解它

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';
}