Pulling MySQL Data, printing and counting in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tsm4781
Forum Commoner
Posts: 38
Joined: Wed Jul 09, 2003 7:17 pm

Pulling MySQL Data, printing and counting in PHP

Post by tsm4781 »

I am attempting to write my own blog software, and I am at a component which is a little confusing to me, so I am wondering if maybe someone here can assist me in finishing up this little side project.

Here is my content page, which has built in pagnation. I know some of it is busted, just trying to see what direction I need to go in, or what I need to do to make this thing work.

Code: Select all

<?PHP
include ("db.inc.php");

    $limit = 20;                  
    $query_count = "SELECT * FROM blog, blogcomments ORDER BY id DESC";      
    $result_count = mysql_query($query_count);       
    $totalrows = mysql_num_rows($result_count);  
	
	if(empty($page))
	&#123;
        $page = 1; 
	&#125;;
	
	$limitvalue = (($page * $limit) - $limit); 
//	$query = "SELECT blog.id, blog.title, blog.cat, blog.date, blogcomments.name, blogcomments.fid COUNT(blogcomments.fid) as commentstotal FROM blogcomments, blog GROUP BY blogcomments.name ORDER BY blog.id DESC LIMIT $limit, $limit";
$query = "SELECT COUNT(fid) AS commenttotal FROM blogcomments WHERE blogid=58";
$query2 = "SELECT * FROM blog ORDER BY id DESC LIMIT $limitvalue, $limit";
$result = mysql_query($query);
$result2 = mysql_query($query2);

print "$query<br />";

print $commenttotal;

print $query2;
	
    	if(mysql_num_rows($result) == 0 || mysql_num_rows($result2) == 0)
		&#123; 
        	echo "Nothing to Display!";
			exit();
		&#125;; 
		
		while ($r = mysql_fetch_array($result,$result2))
		&#123;
		extract($r);
print $r;
		if ($cat == "private")
		&#123;
    		  print "<p><a href="blogprivate.php?id=$id">$title</a><br />";

                    if ($leadin == "")
                      print "";

                    else
                      print "$leadin &#1111;<a href="blogprivate.php?id=$id">More</a>]<br />";
    		  
    		  print "<strong>Date:</strong> $date | <strong>View:</strong> Private | Comments ($commenttotal)</p>";
		&#125;		
		else if ($cat == "public")
		&#123;
    		  print "<p><a href="blogpublic.php?id=$id">$title</a><br />";
                    
                    if ($leadin == "")
                      print "";

                    else
                      print "$leadin &#1111;<a href="blogpublic.php?id=$id">More</a>]<br />";

    		  print "<strong>Date:</strong> $date | <strong>View:</strong> Public | Comments (0)</p>";
		&#125;
		echo "<hr>";
		if ($totalrows > 1) &#123;echo "";&#125;;
		&#125;;
			
		if($page != 1)
	&#123;  
           $pageprev = $page - 1; 
           echo "<strong><a href='$PHP_SELF?page=$pageprev'><< PREV</a></strong>&nbsp;&nbsp;";  
	&#125; 
	else
	&#123; 
           echo "<strong>PREV</strong>&nbsp;&nbsp;"; 
	&#125;;

   	$numofpages = $totalrows / $limit;  

    	for($i = 1; $i <= $numofpages; $i++)
&#123; 
        if($i == $page) 
	&#123;
            echo "<strong>$i&nbsp;&nbsp;</strong>"; 
	&#125;
        else 
	&#123;
            echo "<strong><a href='$PHP_SELF?page=$i'>$i</a></strong>&nbsp;&nbsp;";  
	&#125;
&#125;

if(($totalrows % $limit) != 0)
&#123; 
       	if($i == $page) 
	&#123;
            echo "<strong>$i</strong>&nbsp;&nbsp;"; 
	&#125;
        else
	&#123; 
            echo "<strong><a href='$PHP_SELF?page=$i'>$i</a></strong>&nbsp;&nbsp;"; 
	&#125;
&#125;


if(($totalrows - ($limit * $page)) > 0)
&#123; 
        $pagenext = $page + 1; 
    	echo "<strong><a href='$PHP_SELF?page=$pagenext'>NEXT >></a></strong>";  
&#125;
	else
&#123; 
        echo "&nbsp;&nbsp; <strong>NEXT</strong>"; 
&#125;;

	$page = $_GET&#1111;"page"];

	mysql_free_result($result);

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

want to detail what you want to do, how you want to do it, something more about your "problem" ? or shall we guess?
tsm4781
Forum Commoner
Posts: 38
Joined: Wed Jul 09, 2003 7:17 pm

Post by tsm4781 »

Oops.. that's my bad. Basically, this page is a list view. It pulls the title, a leadin, and the date for the blog table, and should count the number of comments that match the ID of the blog posting. If the posting is blog.id 58, then I would display the title, lead in, and date, as well as display the #2 for number of comments.

The error I am receiving is that I can't seem to figure out how I would pull that data and also do a count from a seperate table and extract it into the page via the PHP code. I either receive num_rows errors or just nothing displays regardless of what I attempt to print for data.

This is how I am displaying the data on the front end:

TITLE
LEAD IN
Date | Comments(#)

Basically, I just can't get the order right and I think that is why I receive num_rows errors. I am still quite new at PHP, but know enough to make me dangerous. This is my first attempt at a real application and this is the last component (counting the data and extracting at the same time) left to get it all together.

Does that suffice, or is there other information you might need to know?

Thanks!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your first (original commented out) query has a logic error in the limit clause, doesn't contain a where clause and will therefore return far more rows than you wish (read up on joining).

The maximum number of rows $result will have is 1. If you did some basic sql debugging, you should find the error you have in the sql or whatever, as that will create an error output from mysql_num_rows().
Post Reply