将推文纳入MySQL循环

时间:2013-10-04 12:00:10

标签: php mysql loops

我需要从mysql查询输出的每两个结果输出一条推文。有问题的页面是here,以便更好地解释我想要实现的目标。

这是我到目前为止使用的代码,它不起作用......

 foreach($multi_array as $key => $value ){

// start tweet output for loop. twitter authentication is before this foreach loop

 ?>
<div id="masonry-container">
<?php 

$dbleads = new mysqli('localhost','****','*****','******'); 

$query = mysqli_query($dbleads, "SELECT * FROM plugin_blog ORDER BY date_added DESC LIMIT 0,10");
$i = 0;
$j = 0;
while ($row=mysqli_fetch_array($query)){
    preg_match('/(<img[^>]+>)/i', $row['html'], $matches);
    $img = $matches[1];
    ++$i;
    ++$j;
    if ($i%2 == 0){
            if($j%10==0){
             echo "<div class='masonryImage tweets' style='width:300px; height:175px;'><div class='tweet-content'>" . $value['text'] . "</div></div>";
            }
    }
    else{
        echo "<div class='masonryImage blogImage' style='width: 300px; height:250px;'>" . $img . " </div>";
    }
  }
}

我出错的任何想法?

更新:现在设法将推文放在正确的位置,但是,只显示了一条推文,它回显了推文的前五个字母,这是我用过的代码:

foreach($multi_array as $key => $value ){
// printing each tweet wrapped in a <li> tag
$tweets = $value['text'];
}

$dbleads = new mysqli('********','***********','**********','************'); 

$query = mysqli_query($dbleads, "SELECT * FROM plugin_blog WHERE published = 1 ORDER BY date_added DESC LIMIT 0,10");
$i = 0;
$j=-1;
while ($row=mysqli_fetch_array($query)){
    preg_match('/(<img[^>]+>)/i', $row['html'], $matches);
    $img = $matches[1];
    ++$i;
    if ($i%2 == 0){
        ++$j;
        echo "<div class='masonryImage tweets' style='width:300px; height:175px;'><div class='tweet-content'>" . $tweets[$j] . "</div></div>";
        echo $j; // here only for debugging, checking the counter works
    }
    else{
        echo "<div class='masonryImage blogImage' style='width: 300px; height:200px;'>" . $img . " </div>";
    }
}

3 个答案:

答案 0 :(得分:2)

您需要更改

$tweets = $value['text'];

$tweets[] = $value['text'];

创建数组$ tweets;否则当使用$ tweets [$ i]时,你只能在索引$ i

获得该单个字母

答案 1 :(得分:0)

如果您有以下结果:

while ($row=mysqli_fetch_array($query)){
var_dump($row);

然后你失败了

if ($i%2 == 0) {

            if($j%10==0){

你的查询限制为10,你每隔2进10次,我猜你永远不会进入第二名。 检查您是否进入第一个if ($i%2 ==0) {echo 'i am in';}

答案 2 :(得分:0)

如果我理解正确,而不是每9张图片的当前1条推文,你想要每1张图片发送1条推文吗?我无法理解为什么你的循环中同时包含$i$j,因为$i已经为您提供了“每隔一个”的概念,我只是删除了$j的所有提及: - )

foreach($multi_array as $key => $value ){

// start tweet output for loop. twitter authentication is before this foreach loop

 ?>
<div id="masonry-container">
<?php 

$dbleads = new mysqli('localhost','****','*****','******'); 

$query = mysqli_query($dbleads, "SELECT * FROM plugin_blog ORDER BY date_added DESC LIMIT 0,10");
$i = 0;
while ($row=mysqli_fetch_array($query)){
    preg_match('/(<img[^>]+>)/i', $row['html'], $matches);
    $img = $matches[1];
    ++$i;
    if ($i%2 == 0){
        echo "<div class='masonryImage tweets' style='width:300px; height:175px;'><div class='tweet-content'>" . $value['text'] . "</div></div>";
    }
    else{
        echo "<div class='masonryImage blogImage' style='width: 300px; height:250px;'>" . $img . " </div>";
    }
  }
}