Jump to content

[SOLVED] getting replys from db


runnerjp

Recommended Posts

ok guys i was messing around with my forum and i deleted all my test posts and be chnace found an error within my code

 

i will shouw you it then explain

 

<link rel="stylesheet" type="text/css" href="http://www.runningprofiles.com/css/login.css">
<link rel="stylesheet" type="text/css" href="http://www.runningprofiles.com/members/include/style.css">

<?php
include "connect.php"; //mysql db connection here
$id=$_GET['id']; 
?><A href='index.php?page=index'>Back to main forum</a>-<A href='index.php?page=post'>New Topic</a>- <? if ($gettopic3[locked] == 0){ ?><A href='index.php?page=reply&id=<?php $id=$_GET['id'];  echo $id ?>'>Reply<br> </A><?php
$gettopic="SELECT * from forumtutorial_posts where postid='$id'";

$gettopic2=mysql_query($gettopic) or die("Could not get topic");

$gettopic3=mysql_fetch_array($gettopic2);
//This checks to see if there is a page number. If not, it will set it to page 1
if (!(isset($pagenum)))
{
$pagenum = 1;
}

//Here we count the number of results
//Edit $data to be your query
$data = mysql_query("SELECT * from forumtutorial_posts where parentid='$id'") or die(mysql_error());
$rows = mysql_num_rows($data);

//This is the number of results displayed per page
$page_rows = 4;

//This tells us the page number of our last page
$last = ceil($rows/$page_rows);

//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}

//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
//This is your query again, the same one... the only difference is we add $max into it
{
?><?php }?>
<table class='maintable'>

<tr class='headline'><td width=20%>Author</td><td width=80%>Post</td></tr> <?php
if ($pagenum == 1){



?> <tr class='mainrow'><td valign='top'><?php echo $gettopic3[author] ?> </td>
<td vakign='top'>created on <?php echo $gettopic3[showtime]?><br>
  <hr>
  <p>
  <?php 

$message=strip_tags($gettopic3['post']);

$message=nl2br($message); ?>
    
  <?php echo $message ?></p>
  <p><br>
    </p></td></tr><?php }



   ?>

</table>
<?php
echo "<br>";
}
echo "<p>";

// This shows the user what page they are on, and the total number of pages
echo " --Page $pagenum of $last-- <p>";

// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1)
{
}
else
{
echo " <a href='index.php?page=message&id=$id&pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='index.php?page=message&id=$id&pagenum=$previous'> <-Previous</a> ";
}

//just a spacer
echo " ---- ";

//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='index.php?page=message&id=$id&pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='index.php?page=message&id=$id&pagenum=$last'>Last ->></a> ";
}
?>

 

the problem is when i thread as started and there are no current replys in my db i get the message Could not get replies but obviusly as there are non there so this means that the page numbers are not set up... and because of this it also means this will not work

($pagenum == 1){ 

so that means my thread stater message also does not show...

 

can nay think of a way around this?

Link to comment
Share on other sites

What you should do is put a check at the top of the page

 

$pagenum = isset($_GET['pagenum']) ? $_GET['pagenum'] : "1";

 

might want to use this rather than.

//This checks to see if there is a page number. If not, it will set it to page 1
if (!(isset($pagenum)))
{
$pagenum = 1;
}

 

the above code will not work unless you have globals on which are off by default.

 

Ray

 

Link to comment
Share on other sites

if you put the check i gave at the top of the page it will be on 1 at the start no matter if there are no results or not.

 

$id=$_GET['id'];
$pagenum = isset($_GET['pagenum']) ? $_GET['pagenum'] : "1";

 

Obviously when you first open the page $_GET['pagenum'] is not set so it will default to 1 and wheather you have results from your query or not it will be at 1.

 

You don't want to set it based on results because when you click the links to go to the rest of your results you can run into problems.

 

Ray

Link to comment
Share on other sites

cheers ray but it didnt do anything... does not even say --page 0 of 0--

 

<link rel="stylesheet" type="text/css" href="http://www.runningprofiles.com/css/login.css">
<link rel="stylesheet" type="text/css" href="http://www.runningprofiles.com/members/include/style.css">

<?php 
include "connect.php"; //mysql db connection here
$id=$_GET['id'];
$pagenum = isset($_GET['pagenum']) ? $_GET['pagenum'] : "1";
?><A href='index.php?page=index'>Back to main forum</a>-<A href='index.php?page=post'>New Topic</a>- <? if ($gettopic3[locked] == 0){ ?><A href='index.php?page=reply&id=<? $id=$_GET['id'];  echo $id ?>'>Reply<br> </A><?
$gettopic="SELECT * from forumtutorial_posts where postid='$id'";

$gettopic2=mysql_query($gettopic) or die("Could not get topic");

$gettopic3=mysql_fetch_array($gettopic2);

//Here we count the number of results
//Edit $data to be your query
$data = mysql_query("SELECT * from forumtutorial_posts where parentid='$id'") or die(mysql_error());
$rows = mysql_num_rows($data);

//This is the number of results displayed per page
$page_rows = 4;

//This tells us the page number of our last page
$last = ceil($rows/$page_rows);

//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}

//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
//This is your query again, the same one... the only difference is we add $max into it
{
?><?}?>
<table class='maintable'>

<tr class='headline'><td width=20%>Author</td><td width=80%>Post</td></tr> <? 
if ($pagenum == 1){



?> <tr class='mainrow'><td valign='top'><? echo $gettopic3[author] ?> </td>
<td vakign='top'>created on <? echo $gettopic3[showtime]?><br>
  <hr>
  <p>
  <? 

$message=strip_tags($gettopic3['post']);

$message=nl2br($message); ?>
    
  <? echo $message ?></p>
  <p><br>
    
    </p></td></tr><? }

$getreplies="Select * from forumtutorial_posts where parentid='$id' $max"; //getting replies

$getreplies2=mysql_query($getreplies) or die("Could not get replies");

while($getreplies3=mysql_fetch_array($getreplies2))

{

   ?><tr class='mainrow'><td valign='top'><? echo $getreplies3[author]?> </td><td vakign='top'>replied  at <? echo $getreplies3[showtime]?> <br>
  <hr>
    <p>
      <?

   $message=strip_tags($getreplies3['post']);

   $message=nl2br($message);

   ?> 
      <? echo $message?></p>
    <p><br>
      
      </p></td></tr>
<?
}
?>
</table><?
echo "<br>";
}
echo "<p>";

// This shows the user what page they are on, and the total number of pages
echo " --Page $pagenum of $last-- <p>";

// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1)
{
}
else
{
echo " <a href='index.php?page=message&id=$id&pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='index.php?page=message&id=$id&pagenum=$previous'> <-Previous</a> ";
}

//just a spacer
echo " ---- ";

//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='index.php?page=message&id=$id&pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='index.php?page=message&id=$id&pagenum=$last'>Last ->></a> ";
}
?>

 

could it still be due to

 

$getreplies="Select * from forumtutorial_posts where parentid='$id' $max"; //getting replies

 

$getreplies2=mysql_query($getreplies) or die("Could not get replies");

 

while($getreplies3=mysql_fetch_array($getreplies2))

 

as its obviusly echoing Could not get replies as its impossible for it to do so as there are non

 

i deleted Could not get replies to see if your $pagenum = isset($_GET['pagenum']) ? $_GET['pagenum'] : "1"; would work and im affrde it does now :(

 

 

Link to comment
Share on other sites

mostly because your reassigning the page number after you already checked it.

Try this

<link rel="stylesheet" type="text/css" href="http://www.runningprofiles.com/css/login.css">
<link rel="stylesheet" type="text/css" href="http://www.runningprofiles.com/members/include/style.css">

<?php
include "connect.php"; //mysql db connection here

$id=$_GET['id'];
$pagenum = isset($_GET['pagenum']) ? $_GET['pagenum'] : "1";

$gettopic="SELECT * from forumtutorial_posts where postid='$id'";

$gettopic2=mysql_query($gettopic) or die("Could not get topic");

$gettopic3=mysql_fetch_array($gettopic2);
?>
<A href='index.php?page=index'>Back to main forum</a>-<A href='index.php?page=post'>New Topic</a>- <?php if ($gettopic3['locked'] == 0){ ?><A href='index.php?page=reply&id=<?php $id=$_GET['id'];  echo $id ?>'>Reply<br> </A>
<?php

//Here we count the number of results
//Edit $data to be your query
$data = mysql_query("SELECT * from forumtutorial_posts where parentid='$id'") or die(mysql_error());
$rows = mysql_num_rows($data);

//This is the number of results displayed per page
$page_rows = 4;

//This tells us the page number of our last page
$last = ceil($rows/$page_rows);

//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
//This is your query again, the same one... the only difference is we add $max into it
{
?>
<?php
}
?>
<table class='maintable'>

<tr class='headline'><td width=20%>Author</td><td width=80%>Post</td></tr> 
<?php
if ($pagenum == 1){



?> <tr class='mainrow'><td valign='top'><? echo $gettopic3['author'] ?> </td>
<td vakign='top'>created on <? echo $gettopic3['showtime']?><br>
  <hr>
  <p>
  <?php
$message=strip_tags($gettopic3['post']);

$message=nl2br($message); ?>

  <?php echo $message ?></p>
  <p><br>

    </p></td></tr>
<?php
}

$getreplies="Select * from forumtutorial_posts where parentid='$id' $max"; //getting replies

$getreplies2=mysql_query($getreplies) or die(mysql_error()."Could not get replies");

while($getreplies3=mysql_fetch_array($getreplies2))

{

   ?><tr class='mainrow'><td valign='top'><? echo $getreplies3['author']?> </td><td vakign='top'>replied  at <? echo $getreplies3['showtime']?> <br>
  <hr>
    <p>
      <?php

   $message=strip_tags($getreplies3['post']);

   $message=nl2br($message);

   ?>
      <?php echo $message?></p>
    <p><br>

      </p></td></tr>
<?php
}
?>
</table><?php
echo "<br>";
}
echo "<p>";

// This shows the user what page they are on, and the total number of pages
echo " --Page $pagenum of $last-- <p>";

// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1)
{
}
else
{
echo " <a href='index.php?page=message&id=$id&pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='index.php?page=message&id=$id&pagenum=$previous'> <-Previous</a> ";
}

//just a spacer
echo " ---- ";

//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='index.php?page=message&id=$id&pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='index.php?page=message&id=$id&pagenum=$last'>Last ->></a> ";
}
?>

</body>

</html>

 

Ray

Link to comment
Share on other sites

OK let me clean up a little. Not sure why you have all the queries but it's yours :)

 

try this

<?php
include "connect.php"; //mysql db connection here
$id=$_GET['id'];
$pagenum = isset($_GET['pagenum']) ? $_GET['pagenum'] : "1";

$gettopic="SELECT * from forumtutorial_posts where postid='$id'";
$gettopic2=mysql_query($gettopic) or die("Could not get topic");
$gettopic3=mysql_fetch_array($gettopic2);
?>
<A href="index.php?page=index">Back to main forum</a>-<A href="index.php?page=post">New Topic</a>- <? if ($gettopic3['locked'] == 0){ ?><A href="index.php?page=reply&id=<? echo $id ?>">Reply<br> </A>
<?php

//Here we count the number of results
//Edit $data to be your query
$data = mysql_query("SELECT * from forumtutorial_posts where parentid='$id'") or die(mysql_error());
$rows = mysql_num_rows($data);

//This is the number of results displayed per page
$page_rows = 4;

//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
//This is your query again, the same one... the only difference is we add $max into it
{
?>
<?
}?>
<table class='maintable'>

<tr class='headline'><td width=20%>Author</td><td width=80%>Post</td></tr> <?
if ($pagenum == 1){
?>
<tr class='mainrow'><td valign='top'><? echo $gettopic3['author'] ?> </td>
<td vakign='top'>created on <? echo $gettopic3['showtime']?><br>
  <hr>
  <p>
  <?

$message=strip_tags($gettopic3['post']);

$message=nl2br($message); ?>

  <? echo $message ?></p>
  <p><br>

    </p></td></tr>
<?php
}

$getreplies="Select * from forumtutorial_posts where parentid='$id' $max"; //getting replies

$getreplies2=mysql_query($getreplies) or die(mysql_error()."Could not get replies");

while($getreplies3=mysql_fetch_array($getreplies2))

{

   ?><tr class='mainrow'><td valign='top'><? echo $getreplies3['author']?> </td><td vakign='top'>replied  at <? echo $getreplies3['showtime']?> <br>
  <hr>
    <p>
      <?

   $message=strip_tags($getreplies3['post']);

   $message=nl2br($message);

   ?>
      <? echo $message?></p>
    <p><br>

      </p></td></tr>
<?
}
?>
</table><?
echo "<br>";
}
// Find out the total number of pages depending on the limit set
    $numofpages = $rows / $page_rows;
    $totalpages = ceil($numofpages);
// Start links for pages
echo "<p align=center>Page $pagenum of $totalpages<br /> ";

// Sets link for previous 25 and return to page 1
    if($pagenum != 1){
        $pageprev = ($pagenum - 1);
         echo "<a href=\"".$_SERVER['PHP_SELF']."?page=message&id=$id&pagenum=1\"><<</a>  ";
         echo "<a href=\"".$_SERVER['PHP_SELF']."?page=message&id=$id&pagenum=$pageprev\">PREV </a> ";
    }else{
        echo "PREV ";
    }

// Loop thru all the pages and echo out the links
    for($i = 1; $i <= $numofpages; $i++){
        if($i == $pagenum){
            echo "[".$i."] ";
        }else{
            echo "<a href=\"".$_SERVER['PHP_SELF']."?page=message&id=$id&pagenum=$i\">$i</a> ";
        }
    }

// Check for straglers after the limit blocks
    if(($rows % $page_rows) != 0){
        if($i == $pagenum){
            echo "[".$i."] ";
        }else{
            echo "<a href=\"".$_SERVER['PHP_SELF']."?page=message&id=$id&pagenum=$i\">$i</a> ";
        }
    }
// Print out the Next 25 and Goto Last page links
    if(($rows - ($page_rows * $pagenum)) > 0){
        $pagenext = $pagenum++;
          echo "<a href=\"".$_SERVER['PHP_SELF']."?page=message&id=$id&pagenum=$pagenext\">NEXT </a>  ";
          echo "<a href=\"".$_SERVER['PHP_SELF']."?page=message&id=$id&pagenum=$totalpages\">>></a>  ";
    }else{
        echo("NEXT");
    }
echo "</p>";
?>
</body>

</html>

 

Ray

Link to comment
Share on other sites

wow that works great and i was also after i got it working attempt to do the whol 12345 thing myself uv saved me a couple of hourse work there.... but im beeing very very picky here but lol  atm it says

 

Page 1 of 0

PREV NEXT

 

is there any chnace it can just say page 1 of 1 untill there is a second page?

Link to comment
Share on other sites

copy and pasted it ALL in

 

prev and next are not clickable i can get rid of them by obviusly just doing

 

  if($pagenum != 1){

        $pageprev = ($pagenum - 1);

        echo "<a href=\"".$_SERVER['PHP_SELF']."?page=message&id=$id&pagenum=1\"><<</a>  ";

        echo "<a href=\"".$_SERVER['PHP_SELF']."?page=message&id=$id&pagenum=$pageprev\">PREV </a> ";

    }else{

        echo "";

 

its just the page 1 of 0 being wierd lol

Link to comment
Share on other sites

OK i know why, You are talking if there is NO returns, can't round up 0 :)

 

add this

// Start links for pages
$maxpage = $totalpages == 0 ? 1 : $totalpages; // add this line
echo "<p align=center>Page $pagenum of $maxpage <br /> "; // change this

 

Also the PREV and NEXT shouldn't be clickable if there are no pages to go to :)

Ray

Link to comment
Share on other sites

Yes, since the amount of rows returned is 0 this line will still return 0

 

$totalpages = ceil($numofpages);

 

So we just have to tell it to set it to 1 if it is 0, but just for that part because the other links will need that variable to be correct for the rest of the script. That is why I assigned another variable to it.

 

Ray

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.