php echo不打印出数据

时间:2012-03-13 05:11:35

标签: php mysql jquery-ui

我使用jQuery UI选项卡使用php和mysql制作动态标签。 我下面的php代码从mysql数据库中获取数据并显示出来。

通常情况下,html代码如下:

<div id="featured" >  
    <ul class="ui-tabs-nav">  
        <li class="ui-tabs-nav-item ui-tabs-selected" id="nav-fragment-1"><a href="#fragment-1"><img src="images/image1-small.jpg" alt="" /><span>15+ Excellent High Speed Photographs</span></a></li>  
        <li class="ui-tabs-nav-item" id="nav-fragment-2"><a href="#fragment-2"><img src="images/image2-small.jpg" alt="" /><span>20 Beautiful Long Exposure Photographs</span></a></li>  
        <li class="ui-tabs-nav-item" id="nav-fragment-3"><a href="#fragment-3"><img src="images/image3-small.jpg" alt="" /><span>35 Amazing Logo Designs</span></a></li>  
        <li class="ui-tabs-nav-item" id="nav-fragment-4"><a href="#fragment-4"><img src="images/image4-small.jpg" alt="" /><span>Create a Vintage Photograph in Photoshop</span></a></li>  
    </ul>  
    <!-- First Content -->  
    <div id="fragment-1" class="ui-tabs-panel" style="">  
        <img src="images/image1.jpg" alt="" />  
        <div class="info" >  
        <h2><a href="#" >15+ Excellent High Speed Photographs</a></h2>  
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla tincidunt condimentum lacus. Pellentesque ut diam....<a href="#" >read more</a></p>  
        </div>  
    </div>  
    <!-- Second Content -->  
    <div id="fragment-2" class="ui-tabs-panel ui-tabs-hide" style="">  
        <img src="images/image2.jpg" alt="" />  
        <div class="info" >  
        <h2><a href="#" >20 Beautiful Long Exposure Photographs</a></h2>  
        <p>Vestibulum leo quam, accumsan nec porttitor a, euismod ac tortor. Sed ipsum lorem, sagittis non egestas id, suscipit....<a href="#" >read more</a></p>  
        </div>  
    </div>  
    <!-- Third Content -->  
    <div id="fragment-3" class="ui-tabs-panel ui-tabs-hide" style="">  
        <img src="images/image3.jpg" alt="" />  
        <div class="info" >  
        <h2><a href="#" >35 Amazing Logo Designs</a></h2>  
        <p>liquam erat volutpat. Proin id volutpat nisi. Nulla facilisi. Curabitur facilisis sollicitudin ornare....<a href="#" >read more</a></p>  
        </div>  
    </div>  
    <!-- Fourth Content -->  
    <div id="fragment-4" class="ui-tabs-panel ui-tabs-hide" style="">  
        <img src="images/image4.jpg" alt="" />  
        <div class="info" >  
        <h2><a href="#" >Create a Vintage Photograph in Photoshop</a></h2>  
        <p>Quisque sed orci ut lacus viverra interdum ornare sed est. Donec porta, erat eu pretium luctus, leo augue sodales....<a href="#" >read more</a></p>  
        </div>  
    </div>  
</div>  

我正在使用php来动态回显html:

<div id="featured" >
<ul class="ui-tabs-nav">
<?php
    $count = 0; // Initialize counter
    $rows = array();
    while($row = mysql_fetch_array( $query )) {
        $rows[] = $row;
        $count = ++$count;
        echo "<li class='ui-tabs-nav-item ui-tabs-selected' id='nav-fragment-" . $count . "'><a href='#fragment-" . $count . "'><img class='thumb' src='$row[imagelink]' alt='' /><span>$row[title]</span></a></li>\n";
}
?>
</ul>
<?php

    $count2 = 0; // Initialize counter
    $rows2 = array();
    while($row2 = mysql_fetch_array( $query )) {
        $rows2[] = $row2;
        $count2 = ++$count2;
        echo "<div id='fragment-" . $count2 . "' class='ui-tabs-panel' style=''>\n";
        echo "<img src='$row[imagelink]' alt='' />\n";
        echo "<div class='info' ><h2><a href='$row[link]'>$row[title]</a></h2><p>$row[description]</p></div>\n";
    }

?>
</div>

但是,它只生成li(制表符)而不生成片段(内容)。

这里有什么问题?

2 个答案:

答案 0 :(得分:1)

您的问题是您的$ query(MySQL结果对象)到达结果行的末尾,然后您的第二个循环将不会从头开始。

这可以解决问题:http://www.krio.me/loop-twice-through-a-php-mysql-result-set/

但是,我建议更接近于创建自己的临时PHP变量来存储所有数据并使用它来第一次和第二次循环它。只是一个建议。

我不知道上面链接的网站中描述的数据搜索方法的性能。

编辑:您已经将数据存储在$ rows变量中。在第二个循环中,循环遍历$ rows变量,而不是使用mysql_fetch_array函数。

添加了代码(没有测试,但应该给你一个好主意):

<div id="featured" >
<ul class="ui-tabs-nav">
<?php
    $count = 0; // Initialize counter
    $rows = array();
    while($row = mysql_fetch_array( $query )) {
        $rows[] = $row;
        $count = ++$count;
        echo "<li class='ui-tabs-nav-item ui-tabs-selected' id='nav-fragment-" . $count . "'><a href='#fragment-" . $count . "'><img class='thumb' src='$row[imagelink]' alt='' /><span>$row[title]</span></a></li>\n";
}
?>
</ul>
<?php

    $count2 = 0; // Initialize counter
    $rows2 = array();
    foreach($rows as $row2) {
        $count2 = ++$count2;
        echo "<div id='fragment-" . $count2 . "' class='ui-tabs-panel' style=''>\n";
        echo "<img src='$row[imagelink]' alt='' />\n";
        echo "<div class='info' ><h2><a href='$row[link]'>$row[title]</a></h2><p>$row[description]</p></div>\n";
    }
?>
</div>

答案 1 :(得分:0)

只需更改

即可
while($row2 = mysql_fetch_array( $query )) {

foreach($rows as $row2) {

但是,这是一个更好的代码版本

首先,将您的数据放入数组中。在您运行查询的地方附近进行。将所有SQL操作保存在一个位置。并且不要将它们与HTML操作混合在一起!

$count = 0; // Initialize counter
$rows = array();
while($row = mysql_fetch_array( $query )) {
    $rows[++$count] = $row;
}

然后转到HTML操作:

<div id="featured" >
<ul class="ui-tabs-nav">
<?php foreach($rows as $count => $row): ?>
<li class='ui-tabs-nav-item ui-tabs-selected' id='nav-fragment-<?=$count?>'>
  <a href='#fragment-<?=$count?>'>
    <img class='thumb' src='<?=$row['imagelink']?>' alt='' />
    <span><?=$row['title']?></span>
  </a>
</li>
<?php endforeach ?>
</ul>
<?php foreach($rows as $count => $row): ?>
<div id='fragment-<?=$count?>' class='ui-tabs-panel' style=''>
  <img src='<?=$row[imagelink]?>' alt='' />
  <div class='info' >
    <h2><a href='<?=$row['link']?>'><?=$row['title']?></a></h2>
    <p><?=$row['description']?></p>
  </div>
</div>
<?php endforeach ?>

了解它如何变得简洁,同时保持所有HTML不变。