It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming PHP and MySQL Forum

Grabbing from a database

Moderator: Malcolm

Grabbing from a database

Postby reed682 on Mon Dec 13, 2004 2:50 pm

I have atag board script which grabbs a name, message, and a url, from a database. It works greak, the only thing is I don't want to display all the entries in the database, just maybe the 15 most recent. is there a way to tell php to grab the 15 most recent entries?

I would appriciate any help. If someone could give me a hint (I really want to figure it out myself but could use a helpful nudge) as to adding smilies? That would be nice.

Thanks again.
User avatar
reed682
 
Posts: 16
Joined: Mon Dec 13, 2004 2:43 pm
Location: Florida

Postby Malcolm on Tue Dec 14, 2004 1:47 pm

SELECT * FROM table WHERE condition=true LIMIT 10

That will only return 10 records

SELECT * FROM table WHERE condition=true LIMIT 10, 10

That will select records 11 to 20

Now finally you can do something like this to select by date

SELECT * FROM table WHERE condition=true ORDER BY date DESC LIMIT 10, 10

For more info check out MySQL's manual:
http://dev.mysql.com/doc/mysql/en/SELECT.html
User avatar
Malcolm
100+ Club
 
Posts: 198
Joined: Thu Oct 07, 2004 9:53 pm
Location: Ontario, Canada

Postby reed682 on Tue Dec 28, 2004 3:56 pm

Thanks, that really worked. What about smilies or emoticons? How can I set up a a smilie that will insert a set of symbols into the text input field and then when I retreive the text render those symbols as a graphic? I know it can be done, but can it be done simply?
User avatar
reed682
 
Posts: 16
Joined: Mon Dec 13, 2004 2:43 pm
Location: Florida

Postby webmaster on Wed Dec 29, 2004 12:41 am

Code: Select all
$variable = "Weee :)";
$print = preg_replace("/:)/","<img src='smile.gif' alt='' />",$variable);

echo $print;


And welcome to DEVPPL :D
Make sure to check out our TNX Review and Link Vault Review
User avatar
webmaster
Site Admin
 
Posts: 2695
Joined: Tue Aug 17, 2004 1:07 pm
Location: Sweden

Postby reed682 on Wed Feb 16, 2005 5:45 pm

Malcolm wrote:SELECT * FROM table WHERE condition=true LIMIT 10, 10

That will select records 11 to 20


But what if there are more records than that? Is there a way to make a button that refreshes and displays the previous 15 entries, and then the previous 15 before that? Am I making snese? What if say someone wanted to go through the tag board archives, all of them. Is there a way to do that dynamically?
User avatar
reed682
 
Posts: 16
Joined: Mon Dec 13, 2004 2:43 pm
Location: Florida

Postby Malcolm on Thu Feb 17, 2005 1:29 am

Yeppers,
Heere's an example of a URL that would work with this code:
page.php?page=10&offset=31
Code: Select all
<?php
if(isset($_GET['page'])){
  $page = $_GET['page'];
}else{
  $page = 1;
}

if(isset($_GET['offset'])){
  $offset = $_GET['offset'];
}else{
  $offset = 10;
}

$current = $page * $offset;

$query = mysql_query("SELECT * FROM table WHERE condition = true LIMIT " . $current . ", " . $offset);
while($result = mysql_fetch_array($query)){
  echo "<hr /><pre>";
  print_r($result);
  echo "</pre>";
}

$total_IDs = mysql_result(mysql_query("SELECT COUNT(ID) from TABLE"), 0);

$total_pages = ceil($total_IDs / $offset );

for($x=1;$x < $total_pages; $x++){
  if($x == $page){
    echo "<b>" . $x . "</b> ";
  }else{
    echo "<a href='?page=" . $x . "&offset=" . $offset"'>" . $x . "</a> ";
  }
}
?>


Again, like most code posted here, this hasn't been tested but should work fine :)

{EDIT}
Note that this code isn't secure as it doesn't check for numerical values for the page and offset values nore does it check for escapable characters in the input. Just thought I'd let'cha know :)
Image
User avatar
Malcolm
100+ Club
 
Posts: 198
Joined: Thu Oct 07, 2004 9:53 pm
Location: Ontario, Canada

Postby reed682 on Tue May 10, 2005 1:44 pm

Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in /home/virtual/site49/fst/home/normal/public_html/tag_board/tag_board.php on line 219

This is the error I get when I put that script in my tag board. What am I doing wrong?
User avatar
reed682
 
Posts: 16
Joined: Mon Dec 13, 2004 2:43 pm
Location: Florida

Postby webmaster on Tue May 10, 2005 1:55 pm

Can you show the line 219 and some lines before and after that row.
Make sure to check out our TNX Review and Link Vault Review
User avatar
webmaster
Site Admin
 
Posts: 2695
Joined: Tue Aug 17, 2004 1:07 pm
Location: Sweden

Postby reed682 on Tue May 10, 2005 2:01 pm

for($x=1;$x < $total_pages; $x++){
if($x == $page){
echo "<b>" . $x . "</b> ";
line 218 --> }else{
line 219 -- > echo "<a href='?page=" . $x . "&offset=" . $offset"'>" . $x . "</a> ";
line 220 --> }
line 221 -->}
line 222 -->?>

Does that help? I know this thread is old but I had to research loops because I could not begin to understand that code but it is making more sense. Thanks for your help.
User avatar
reed682
 
Posts: 16
Joined: Mon Dec 13, 2004 2:43 pm
Location: Florida

Postby webmaster on Tue May 10, 2005 3:50 pm

Change line 219 to:
Code: Select all
echo "<a href=\"?page=$x&offset=$offset\">$x</a>";
Make sure to check out our TNX Review and Link Vault Review
User avatar
webmaster
Site Admin
 
Posts: 2695
Joined: Tue Aug 17, 2004 1:07 pm
Location: Sweden

Next

Who is online

Users browsing this forum: No registered users and 0 guests