0
$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.

Adam Bellaire
  • 108,003
  • 19
  • 148
  • 163
  • Please edit your question, it is unreadable in its present sate. – Eran Galperin Jan 10 '09 at 01:23
  • Your variable names make your code a ridiculous pain to read. – Paolo Bergantino Jan 10 '09 at 02:03
  • There, at least the bracing style is consistent now... – Adam Bellaire Jan 10 '09 at 02:04
  • My apologies for the state of the code but when I copied into this web site it took out my formating. You're also asking me to rewrite the variable names? –  Jan 10 '09 at 02:26
  • @panhistoria: I can't make sense of what xs, $c, &i, &t, and $fc refer to. I've done a search/replace for everything else, however. – George Stocker Jan 10 '09 at 03:48
  • @Gortok - thank you. I think I'll take a look at this and repost the code tomorrow with an explanation of each piece. BTW I am the maintenance programmer. An idiot, but someone has to do it. –  Jan 10 '09 at 04:21

3 Answers3

3

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?

Community
  • 1
  • 1
George Stocker
  • 57,289
  • 29
  • 176
  • 237
0

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
  • 57,289
  • 29
  • 176
  • 237
0

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
  • 3,263
  • 4
  • 21
  • 19