• Doodlebee

    (@doodlebee)


    I was wondering if I could get a hand with some scripting. I’m using a WordPress installation for a sort of online auction. It’s hard to explain, so I won’t bother. But what I need to do is export the information from the database to a printable list. I’ve managed to do this, however, if there’s more than one comment in a post, then it prints the post over and over again, showing each new comment.

    I know it’s an issue with my “while” statement – I need a loop within a loop, but I can’t seem to get it right.

    What I have is this:

    $default_sort = 'post_title';
    $allowed_order = array ('post_title', 'post_content', 'comment_author', 'comment_content');
    if (!isset ($_GET['order']) ||
        !in_array ($_GET['order'], $allowed_order)) {
        $order = $default_sort;
    } else {
        $order = $_GET['order'];
    }
    /* connect to db */
    mysql_connect ('localhost','name','password');
    mysql_select_db ('db_name');
    /* construct and run our query */
    $query = "SELECT * FROM auction_posts, auction_comments where comment_post_ID = auction_posts.ID ORDER BY $order";
    $result = mysql_query ($query);
    /* make sure data was retrieved */
    $numrows = mysql_num_rows($result);
    if ($numrows == 0) {
       echo "No data to display!";
       exit;
    }
    /* now grab the first row and start the table */
    while ($row = mysql_fetch_assoc($result)) { ?>
    <div id="n<?php echo ($row['ID']); ?>" class="listing">
    <h1><?php echo stripslashes($row['post_title']); ?></h1>
    <?php echo stripslashes($row['post_content']); ?><br />
    <?php $c = $row['comment_content'];
    while ($c > 0) { ?>
    	<?php echo $row['comment_content']; ?> by <?php echo $row['comment_author']; ?><br />
    <?php } ?>
    </div>
    <?php  }?>

    It’s the “while $c…” stuff that I *know* is wrong. But I can’t seem to figure out the proper syntax to see if there’s more than one comment, then loop that into lines showing the output of each comment *without* showing the post over and over again, each time with a new comment. (I also know the “while $c” part is insanely stupid…I’ve tried other, more complicated, variations to no effect. That was my “I’m about to quit” effort LOL)

    Any help would be appreciated.

The topic ‘Trying to export database content’ is closed to new replies.