Kevin Harrison
Kevin Harrison

Reputation: 337

Loops in Function Not Working Proplerly

Could somebody by any chance help me cleanup this function and make it work properly?

It is supposed to look like this: http://gyazo.com/04c31a3edaabeca5f5c6376f1cb607ca.png Except under category it should only list the categories matching up with the cat_id which does work.

But the page looks weird. This is what it looks like now: http://gyazo.com/f217603bede98210dce21328e1aab34f.png

function getcatposts($cat_id) 
{
    $sql = mysql_query("SELECT COUNT(*) FROM `topics`");
    if(!$sql){
      echo 'Error: ',mysql_error();
    }
    $r = mysql_fetch_row($sql);

    $numrows = $r[0];
    $rowsperpage = 10;

    $totalpages = ceil($numrows / $rowsperpage);

    echo '
<table class="table table-striped table-bordered table-hover">  
        <thead>  
          <tr class="info">  
            <th>Title</th>  
            <th>Username</th>  
            <th>Date</th>  
            <th>Category</th>  
          </tr>  
        </thead>';
    if(isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
       $currentpage = (int) $_GET['currentpage'];
    } else {
       $currentpage = 1;
    }

    if($currentpage > $totalpages) {
       $currentpage = $totalpages;
    }

    if($currentpage < 1) {
      $currentpage = 1;
    }

    $offset = ($currentpage - 1) * $rowsperpage;

    $sql = mysql_query("SELECT * FROM `topics` ORDER BY topic_id DESC LIMIT $offset, $rowsperpage");
    if(!$sql){
      echo 'Error: '.mysql_error();
    }
    $link = mysqli_connect("localhost", "lunar_lunar", "", "lunar_users");
    $qc = mysqli_query($link, "SELECT * FROM topics WHERE topic_cat='$cat_id'");

    while($row = mysqli_fetch_array($qc)) 
    {
      $topic_title[]=$row['topic_subject'];
      $topic_id[]=$row['topic_id'];
    }

    $qc2 = mysqli_query($link, "SELECT * FROM categories WHERE cat_id='$cat_id'");

    while($row2 = mysqli_fetch_array($qc2)) 
    {
      $cat_name[]=$row2['cat_name'];
    }
    for ($i=0; $i < count($topic_id); $i++) 
    {
      echo '
        <tbody><tr>
          <td><a href="topic.php?id='.$topic_id[$i].'">'.$topic_title[$i].'</a></td>
          <td><a href="../public.php?id='.$res['topic_by'].'">'.getOwner($res['topic_by']).'</td></a>
          <td>'.$res['topic_date'].'</td>
          <td>'.$cat_name[$i].'</td>
        </tr></tbody>
            ';
    }
}

Upvotes: 0

Views: 58

Answers (1)

Snowburnt
Snowburnt

Reputation: 6922

First, I don't see where $res was defined, that would explain why topic by and topic date aren't showing anything. Maybe you forgot to change the variable name?

second you're selecting the name of a particular category, so there will only be one result row. When you cycle through the topics you'll display the first one but cat_name[1] and cat_name[2] will be empty. Since you know that you're only selecting a single category, just assign the result to a variable rather than array. cat_name instead of cat_name[]

you say:

SELECT * FROM categories WHERE cat_id='$cat_id'

assuming cat_id is the primary key of the table you will NEVER have more than one result.

When you assign it to the cat_name[] you go like this:

while($row2 = mysqli_fetch_array($qc2)) 
    {
      $cat_name[]=$row2['cat_name'];
    }

it will fetch one row and assign it to $cat_name[0].

The problem comes here:

<td>'.$cat_name[$i].'</td>

inside the loop $i is incrementing, and it will go through 3 times in this case. $cat_name[0] will return "web development". But $cat_name[1] and $cat_name[2] will have nothing. either just call $cat_name[0] instead of $cat_name[$i] or assign the value to just plain $cat_name and call that in the loop.

Upvotes: 2

Related Questions