Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 01-07-2007, 05:10 AM   #1 (permalink)
markster
Code Monkey
 
markster's Avatar
 
Join Date: Jun 2006
Posts: 65
markster is on a distinguished road
Question How can i make php do this?

im making a jokes website and one of the pages is jokes.pho?cat=4 which will display all jokes with the category set as 4. the code so far is:

PHP Code:
<?php
require_once("mysql_connect.php");
$query "SELECT * FROM jokes WHERE cat=$cat ORDER BY id DESC";
$result mysql_query($query);
## CODE THAT DISPLAYS THE RESULTS IN A TABLE ##
?>
what im trying to make is a system that counts how many jokes in that category, and displays links that will be like:

view: 1-20, 21-40, 41-60

but it will need to auto figure out how many links it will need to produce and link them to a page like:

jokes.php?cat=4&selection=1,20

please help!
markster is offline   Reply With Quote
Old 01-07-2007, 05:42 AM   #2 (permalink)
markster
Code Monkey
 
markster's Avatar
 
Join Date: Jun 2006
Posts: 65
markster is on a distinguished road
nevermind, i think ive fixed it
markster is offline   Reply With Quote
Old 01-07-2007, 09:47 AM   #3 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,490
sde is on a distinguished road
heh missed your second post and had a hack at it anyway.

here's code that would print your group links (un-tested so it may or may not work first time)
PHP Code:
<?
// get the count for a specific group
$result mysql_query("select count(*) from jokes where cat=1");
$count mysql_result($result,0,0);

// find the number of groups by dividing the
// result my 10 and rounding it down
$number_of_groups floor($count/10);

// use this variable to incremenet the
// starting index. (add 10 each iteration)
$starting_group_number 1;

// loop through groups of 10s
for ($i=1;$i<=$number_of_groups;$i++) {
  
// print the link
  
echo '<a href="jokes.php?star='.$starting_group_number.'">$starting_group_number . "-" . ($starting_group_number+10) . "</a>&nbsp; &nbsp;\n';
  
  
// add 10 to the starting group number
  
$starting_group_number += 10;
}
?>
and here is how you would handle the links
PHP Code:
<?
// clean input by stripping non-numeric characters
if (!$_GET['start') {
  
$start 1;
} else {
    
$start preg_replace("[^0-9]","",$_GET['start'];
}

// now query with the start number and get the next 10 results
$result mysql_query("select * from jokes where cat=1 limit start, 10");

// print results with the query above
?>
__________________
Mike
sde is offline   Reply With Quote
Old 01-07-2007, 10:23 AM   #4 (permalink)
markster
Code Monkey
 
markster's Avatar
 
Join Date: Jun 2006
Posts: 65
markster is on a distinguished road
I dunno if it works, but thanks soooooo much!
markster is offline   Reply With Quote
Old 01-07-2007, 11:44 AM   #5 (permalink)
markster
Code Monkey
 
markster's Avatar
 
Join Date: Jun 2006
Posts: 65
markster is on a distinguished road
Thanks!

Thanks sooo much! I've sorted out your mistakes, edited it down into one script, and updated it to include 15 results per page. I've posted it below incase another user might find it useful.

PHP Code:
<?php
    
require_once("mysql_connect.php");
    
$result mysql_query("SELECT count(*) FROM jokes WHERE cat=$cat");
    
$count mysql_result($result,0,0);
    
$number_of_groups ceil($count/15);
    
$starting_group_number 1;
    for (
$i=1;$i<=$number_of_groups;$i++) {
      
$second_num $starting_group_number+14;
      echo(
"<a href=\"jokes.php?star=$starting_group_number&cat=$cat\">$starting_group_number - $second_num</a>&nbsp; &nbsp;\n");
      
$starting_group_number += 15;
    }    
    echo(
"<br><br>");
    if (!
$_GET['start']) {
      
$start 1;
    } else {
      
$start $_GET['start'];
    }
    
$begnum $start 1;
    
$query2 "SELECT id, title, author FROM jokes WHERE cat=$cat LIMIT $begnum, 15";
    
$result2 mysql_query($query2);
    echo(
"<table width=\"80%\" border=\"0px\">\n");
    echo(
"<tr><td width=\"80%\"><b>Title<b></td><td width=\"20%\"><b>Written By</b></td></tr>");
    while (
$row mysql_fetch_array($result2MYSQL_ASSOC)) {
        
$id $row['id'];
        
$title $row['title'];
        
$author $row['author'];
        echo(
"<tr><td><a href=\"joke.php?id=$id\">$title</a></td><td><a href=\"user.php?user=$author\">$author</a></td></tr>");
    }
    echo(
"</table>");
    
?>
markster is offline   Reply With Quote
Old 01-08-2007, 12:56 PM   #6 (permalink)
toe_cutter
Code Monkey
 
Join Date: Aug 2002
Location: Boston, MA
Posts: 79
toe_cutter is on a distinguished road
Send a message via ICQ to toe_cutter Send a message via AIM to toe_cutter Send a message via Yahoo to toe_cutter
Great job, I love it when people post their solutions instead of just saying I got it to work.

-Toe
__________________
toe_cutter is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Include file shown correctly in FF not in IE Stoner HTML, XML, Javascript, AJAX 2 04-21-2006 07:56 AM
Make a search engine for your website with PHP zhisede PHP 3 10-25-2005 07:54 AM
PHP 5.0.4 and 4.3.11 Released sde Code Newbie News 0 04-20-2005 10:56 AM
PHP vs .NET Redline Lounge 1 11-24-2004 06:10 AM


All times are GMT -8. The time now is 06:38 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting