简体   繁体   中英

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. I want to create div s based around the number of entries with the same Page name.

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 = pageDivs($dbc, $path['call_parts'][0])

The $path variable would equal the string 'about' in this case.

The function pageDivs($dbc, about) either looks like this:

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. 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. The result should be the same. Sometimes I get 7 iterations of entry 1 others I get nothing at all. 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.

While Loop was the one:

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); 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++;
}
}

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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