tags:

views:

369

answers:

3
$story_query = "SELECT table_name, id FROM planning WHERE parent = '$novelnum'";
$story_result = db_query($story_query);
while($story_row = db_fetch_array($story_result)) {
    $taleTable_Name = $story_row['table_name'];
    $postid[] = $story_row['id'];

    $q2 = "Select * from $taleTable_Name where approved='Y' order by id";
    $bset2 = db_query($q2);
    while($rset2 = db_fetch_array($bset2)) {
        $i[] = $rset2['id'];
        $t[] = $rset2['thread'];
        $s[] = $rset2['subject'];
        $a[] = $rset2['author'];
        $d[] = $rset2['datestamp'];

    }
}

if(isset($d)) {
    $fc = count($d);
    if($fc > 20) {
        $xs = $fc - 20;
    }
    else {
        $xs = 0;
    }

    for($c=$xs;$c<$fc;$c++) {
        if($s[$c] != "") {
            $newpost .= $d[$c];
            $newpost .= " <a href='../forums/read.php?f=";
            $newpost .= end($postid);
            $newpost .= "&i=";
            $newpost .= $i[$c];
            $newpost .= "&t=";
            $newpost .= $t[$c];
            $newpost .= "'>" ;
            $newpost .= $s[$c];
            $newpost .= "</a> by "; 
            $newpost .= $a[$c];
            $newpost .= $taleTable_Name;
            $newpost .= "<br>\n";
        }
    }
}
else {
    $newpost = "There are no posts for this scroll yet.";
}

The above code correctly presents me with all the entries found with $taleTable_Name, but only presents the last variable of $postid when I print $newpost. I want the id ($postid to match the table_name ($taleTable_Name) so that the url created actually goes to the correct forum.

+3  A: 

First off, I have no idea what your variable names mean. I've tried to guesstimate, but you should look into renaming them. To keep the sanity of your maintanence programmer.

Looking at your code, I know what it's doing, but I have no idea what it's supposed to do, or why. Below is an attempt to rename your variable names, but I have no idea if it's even right, or close to right:


$story_query = "SELECT table_name, id FROM planning WHERE parent = '$novelnum'";
$story_result = db_query($story_query);
while($story_row = db_fetch_array($story_result)) {
    $taleTable_Name = $story_row['table_name'];
    $postid[] = $story_row['id'];

    $query2 = "Select * from $taleTable_Name where approved='Y' order by id";
    $bset2 = db_query($query2);
    while($rowset2 = db_fetch_array($bset2)) {
        $id[] = $rowset2['id'];
        $thread[] = $rowset2['thread'];
        $subject[] = $rowset2['subject'];
        $author[] = $rowset2['author'];
        $datetime[] = $rowset2['datestamp'];

    }
}

if(isset($datetime)) {
    $forumcount = count($datetime);
    if($forumcount > 20) {
        $xs = $forumcount - 20;
    }
    else {
        $xs = 0;
    }

    for($postnum=$xs;$postnum<$forumcount;$postnum++) {
        if($subject[$postnum] != "") {
            $newpost .= $datetime[$postnum];
            $newpost .= " <a href='../forums/read.php?f=";
            $newpost .= end($postid);
            $newpost .= "&id=";
            $newpost .= $id[$postnum];
            $newpost .= "&thread=";
            $newpost .= $thread[$postnum];
            $newpost .= "'>" ;
            $newpost .= $subject[$postnum];
            $newpost .= "</a> by "; 
            $newpost .= $author[$postnum];
            $newpost .= $taleTable_Name;
            $newpost .= "<br>\n";
        }
    }
}
else {
    $newpost = "There are no posts for this scroll yet.";
}


What do the following variables mean? : $xs, $c, &i, &t, and $fc? These ought to be meaningful names. I've renamed your other variables, but for these I just guessed. I could be way off. Meaningful variable names go a long way to helping someone read code that doesn't have comments. As it is, if you had meaningful variable names, I may not need comments -- though what's the magic number of '20' doing? What is so meaningful about that number?

George Stocker
A: 

One thing I noticed about your code is that no where is $newpost printed out. Before the end of of the for loop, something needs to be done with $newpost. In this case, you're simply throwing away the result each time (or more correctly, overwriting it). You need to do something with $newpost (print it, store it in another value, send it back to the browser, etc).

George Stocker
How is the result being thrown away? It is being appended.
Paolo Bergantino
After the for loop ends, we don't see a use for $newpost. It's simply not being used in the code we're given. Results are being appended to it, but nothing is happening after that.
George Stocker
A: 

How about ?

unset($post); // ensure start afresh

$story_query = "SELECT table_name, id FROM planning WHERE parent = '$novelnum'";
$story_result = db_query($story_query);
while($story_row = db_fetch_array($story_result)) {
    $taleTable_Name = $story_row['table_name'];
    $postid         = $story_row['id'];

    $q2 = "Select * from $taleTable_Name where approved='Y' order by id";
    $bset2 = db_query($q2);
    while($rset2 = db_fetch_array($bset2)) {
        $post[$postid] = $rset2;
    }
}

if(isset($post)) {
    /***
     * Page control here    
     ****/

    foreach($post as $id => $msg) {
        if($row['subject'] != "") {
            $newpost .= $msg['datestamp'];
            $newpost .= " <a href='../forums/read.php?f=";
            $newpost .= $id;
            $newpost .= "&i=".$msg['id'];
            $newpost .= "&t=".$msg['thread'];
            $newpost .= "'>" ;
            $newpost .= $msg['subject'];
            $newpost .= "</a> by "; 
            $newpost .= $msg['author'];
            $newpost .= $taleTable_Name;
            $newpost .= "<br>\n";
        }
    }
}
else {
    $newpost = "There are no posts for this scroll yet.";
}
John Griffiths