Jump to content

HTML table not working in php


jeggae

Recommended Posts

Hi.

I'm new to php and learning. I'm trying to get a program working but for some reason the table isnt showing up in ‘showtopic.php’ when the program is run, except the header, the header seems okay. I've pasted the table as an html document and the table ran perfect, but as part of the php, does not seem to be running except the header. The ‘topiclist.php’ part of the program the table, which is very similar, runs fine.

Any ideas why please? I’ve spent hours looking over it and trying to rectify it.

Thanks in advance

 

 

showtopic.php:

 

<?php 
//check for required info from the query string
if (!$_GET[topic_id]) {
    header("Location: topiclist.php");
}

//connect to server and select database
$conn = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("forum", $conn) or die(mysql_error());

//verify the topic exists
$verify_topic = "select topic_title from forum_topics  where topic_id = $_GET[topic_id]";
$verify_topic_res = mysql_query($verify_topic,$conn) or die (mysql_error());

if (mysql_num_rows($verify_topic_res) < 1) {
    //this topic does not exist
    $display_block = "<p><em>You have selected an invalid topic, please<a href=\"topiclist.php\">try again</a></em></p>";
} else {
    //get topic title
    $topic_title = stripslashes(mysql_result($verify_topic_res,0,'topic_title'));

    //gather the posts
    $get_posts = "select post_id, post_text, date_format(post_create_time, '%b %e %y at %r') as fmt_post_create_time, post_owner from forum_posts where topic_id = $_GET[topic_id] order by post_create_time asc";

    $get_posts_res = mysql_query($get_posts,$conn) or die (mysql_error());

    //create the display string
    $display_block = "
    <p>Showing posts for the <strong>$topic_title</strong> topic:</p>

    <table width=100% cellpadding=3 cellspacing=1 border=1>
    <tr>
    <th>AUTHOR</th>
    <th>POST</th>
    </tr>";

        while ($posts_info = mysql_fetch_array($get_posts_res))  {
        $post_id = $posts_info['post_id'];
        $post_text = nl2br(stripslashes($posts_info['post_text']));
        $post_create_time = $posts_info['fmt_post_create_time'];
        $post_owner = stripslashes($posts_info['post_owner']); 

        //add to display
        $display_block .= "
        
        <tr>
        <td width=35% valign=top>$post_owner<br>[$post_create_time]</td>
        <td width=65% valign=top>$post_text<br><br>
        <a href=\"replytopost.php?post_id=$post_id\"><strong>REPLY TO POST</strong></a></td>
        </tr>";
    }

    //close table
    $display_block .= "</table>";
}
?>

<html>
<head>
<title>Posts in Topic</title>
</head>
<body>
<h1>Posts in Topic</h1>
<?php print $display_block; ?>
</body>
</html>

 

 

Table that doesn't show up except the header, from showtopic.php:

 

<p>Showing posts for the <strong>$topic_title</strong> topic:</p>

    <table width=100% cellpadding=3 cellspacing=1 border=1>
    <tr>
    <th>AUTHOR</th>
    <th>POST</th>
    </tr>";

        while ($posts_info = mysql_fetch_array($get_posts_res))  {
        $post_id = $posts_info['post_id'];
        $post_text = nl2br(stripslashes($posts_info['post_text']));
        $post_create_time = $posts_info['fmt_post_create_time'];
        $post_owner = stripslashes($posts_info['post_owner']); 

        //add to display
        $display_block .= "
        
        <tr>
        <td width=35% valign=top>$post_owner<br>[$post_create_time]</td>
        <td width=65% valign=top>$post_text<br><br>
        <a href=\"replytopost.php?post_id=$post_id\"><strong>REPLY TO POST</strong></a></td>
        </tr>";
    }

    //close table
    $display_block .= "</table>";

 

 

 

 

topiclist.php [table runs fine]:

 

<?php
//connest to server and select database
$conn = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("forum", $conn) or die(mysql_error());

//gather the topics
$get_topics = "select topic_id, topic_title, date_format(topic_create_time, '%b %e %y at %r') as fmt_topic_create_time, topic_owner from forum_topics order by topic_create_time desc";
$get_topics_res = mysql_query($get_topics,$conn) or die(mysql_error());
if (mysql_num_rows($get_topics_res) < 1) {
    //there are no topics, so say so
    $display_block = '<p><em>No Topics exist,</em></p>';
    } else {
    //create the display topics
    $display_block = "
    <table cellpadding=3 cellspacing=1 border=1>
    <tr>
    <th>Topic Title</th>
    <th>Number of posts</th>
    </tr>";
    
    while ($topic_info = mysql_fetch_array($get_topics_res)) {
        $topic_id = $topic_info['topic_id'];
        $topic_title = stripslashes($topic_info['topic_title']);
        $topic_create_time = $topic_info['fmt_topic_create_time'];
        $topic_owner = stripslashes($topic_info['topic_owner']);

        //get number of posts
        $get_num_posts = "select count(post_id) from forum_posts where topic_id = $topic_id";
        $get_num_posts_res = mysql_query($get_num_posts,$conn) or die(mysql_error());
        $num_posts = mysql_result($get_num_posts_res,0,'count(post_id)');

        //add to display
        $display_block .= "
        <tr>
        <td><a href=\"showtopic.php?topic_id=$topic_id\"><strong>$topic_title</strong></a><br>
        Created on $topic_create_time by $topic_owner</td>
        <td align=center>$num_posts</td>
        </tr>";
        }
        
        //close up the table
        $display_block .= "</table>";
    }
    ?>

    <html>
    <head>
    <title>Topics in the Forum</title>
    <body>
    <h1>Topics in the Forum</h1>
    <?php print $display_block; ?>
    <p>Would you like to <a href="addTopicForm.html"> add a Topic</a>?</p>
    </body>
    </head>
    </html>

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

Test #1: Take your query

select post_id, post_text, date_format(post_create_time, '%b %e %y at %r') as fmt_post_create_time, post_owner from forum_posts where topic_id = $_GET[topic_id] order by post_create_time asc

 

put the actual topic_id intead of $_GET[topic_id] and run in MySQL console, phpMyAdmin or whatever you use.

 

Test #2: use mysql_num_rows to check it in the actual script

Link to comment
Share on other sites

Thanks Mchl , didn't know about testing sql queries in MySQL console in phpMyAdmin. But I entered the query as you suggested with the below results. Does it mean that the query is OK?

 

√ Showing rows 0 - 6 (7 total, Query took 0.0496 sec)

SELECT post_id, post_text, date_format( post_create_time, '%b %e %Y at %r' ) AS fmt_post_create_time, post_owner
FROM forum_posts
WHERE topic_id = topic_id
ORDER BY post_create_time ASC 
LIMIT 0 , 30

Link to comment
Share on other sites

Thanks Mchl , didn't know about testing sql queries in MySQL console in phpMyAdmin. But I entered the query as you suggested with the below results. Does it mean that the query is OK?

 

√ Showing rows 0 - 6 (7 total, Query took 0.0496 sec)

SELECT post_id, post_text, date_format( post_create_time, '%b %e %Y at %r' ) AS fmt_post_create_time, post_owner
FROM forum_posts
WHERE topic_id = topic_id
ORDER BY post_create_time ASC 
LIMIT 0 , 30

 

Also showed 'post_id' from 'forum_posts'.

Link to comment
Share on other sites

try adding

echo mysql_num_rows($get_posts_res);

 

after running this query, and see if it returns more than 0 rows

 

Sorry, try adding it where? I've tried adding it on the end of the above query in the console also in the console on its own, but got errors.

Link to comment
Share on other sites

In your PHP code after

$get_posts_res = mysql_query($get_posts,$conn) or die (mysql_error());

 

Thanks for your patience Mchl.

With that line of code, I'm getting a '0' in the top left hand corner.

Link to comment
Share on other sites

Which means the query returns no results. Nothing to display.

 

Ok thanks. Now I know that I'll have a good think and invetegate it, and see if I can solve it. I'll either come back and close this thread or ask for more help.  ;)

Many thanks for your help so far.

Link to comment
Share on other sites

Take a look at the source you have in topiclist.php

It uses mysql_num_rows, to display a message if no rows are found, or a table otherwise.

 

I've been looking through it but cant find any problem with the query in topiclist.php. The database and tables seem okay. Any ideas please?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.