简体   繁体   English

我的php循环有什么问题?

[英]What's wrong with my php loop?

I have a table called pages, and 2 entries with the same word 'about' in the column named Page. 我有一个名为pages的表,在名为Page的列中有2个条目带有相同的单词“ about”。 I want to create div s based around the number of entries with the same Page name. 我想基于具有相同页面名称的条目数创建div

This should be easy but it just can't see where it's going wrong. 这应该很容易,但是却看不到哪里出了问题。

I have a variable called page that i echo onto the index page it looks like this: 我有一个名为page的变量,我将其呼应到索引页上,如下所示:

$page = pageDivs($dbc, $path['call_parts'][0])

The $path variable would equal the string 'about' in this case. 在这种情况下, $path变量将等于字符串“ about”。

The function pageDivs($dbc, about) either looks like this: 函数pageDivs($dbc, about)如下所示:

function pageDivs($dbc, $id){
$q="SELECT * FROM pages WHERE Page='$id'";
$r = mysqli_query($dbc, $q);
$data=mysqli_fetch_assoc($r);
for ($i=1; $i<count($data); $i++) {
    echo '<div id="Content'.$i.'">'.$data['Content'].'</div>';
    }
}

or this: 或这个:

function pageDivs($dbc, $id){
$q="SELECT * FROM pages WHERE Page='$id'";
$r = mysqli_query($dbc, $q);
$data=mysqli_fetch_assoc($r);
$i=1;
foreach ($data AS $value){
    if ($i>count($data)){
    echo '<div id="Content'.$i.'">'.$value['Content'].'</div>';
    $i++;
        }
    }

or this: 或这个:

function pageDivs($dbc, $id){
$q="SELECT * FROM pages WHERE Page='$id'";
$r = mysqli_query($dbc, $q);
$data=mysqli_fetch_assoc($r);
$i=1;
foreach ($data AS $key=>$value){
    if ($i>count($data)){
    echo '<div id="Content'.$i.'">'.$data[$i]['Content'].'</div>';
    $i++;
        }
    }
}

I have tried fetch_assoc and fetch_array and every combination except obviously the one for $k => $v , i've put $data everywhere or $key everywhere or $value everywhere. 我已经尝试了fetch_assocfetch_array以及每种组合,除了显然$k => $v组合之外,我已经将$ data放置在任何地方,或将$key放置在任何地方,或将$value放置在任何地方。 It's driving me nuts. 它让我发疯。

I'm obviously missing something fundamental here so someone please could you enlighten me. 我显然在这里缺少基本的东西,所以请有人启发我。 I really don't care if I use a for loop or foreach loop. 我真的不在乎是使用for循环还是foreach循环。 The result should be the same. 结果应该是相同的。 Sometimes I get 7 iterations of entry 1 others I get nothing at all. 有时我得到条目1的7次迭代,而我却一无所获。 There are explicitly 2 entries with the same name so the max div s should be 2 in this case but I wish for it to remain dynamic. 显式地有2个具有相同名称的条目,因此在这种情况下max div应当为2,但我希望它保持动态。

While Loop was the one: Loop是一个:

function pageDivs($dbc, $id){
$q="SELECT * FROM pages WHERE Page='$id'";
$r = mysqli_query($dbc, $q);
$i=1;
while ($data=mysqli_fetch_assoc($r)){?> 
    <div id="Content<?php echo $i; ?>">
        <?php echo $data['Content']?>
    </div>
    <?php $i++;
    }
}

In $data variable have only one result and its better to use mysqli_num_rows($r); $data变量中只有一个结果,最好使用mysqli_num_rows($r); to for count purpose how many result you have. 为了计数目的,您有多少结果。

<?php 
function pageDivs($dbc, $id){
$q="SELECT * FROM pages WHERE Page='$id'";
$r = mysqli_query($dbc, $q);
//$data=mysqli_fetch_assoc($r);
$numofrowresult=mysqli_num_rows($r); //Count how many result you have
echo $numofrowresult;
$i = 1; //initalize counter for  the unique id

foreach($r as $values)
{
      echo '<div id="Content'.$i.'">'.$values['Content'].'</div>';
      $i++;
}
}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM