Current location: Hot Scripts Forums » Programming Languages » PHP » Ok, what's wrong? Please help


Ok, what's wrong? Please help

Reply
  #1 (permalink)  
Old 04-28-04, 10:28 AM
UVL's Avatar
UVL UVL is offline
Newbie Coder
 
Join Date: Apr 2004
Location: Deep Underground
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Ok, what's wrong? Please help

Ok guys, I was curious if someone could read over this code and let me know why it won't paginate correctly. I've replaced all "sensitive" information with *'s, but everything works great except for the paginate part. I'm currently trying to make it paginate one row of data from my database at a time until I get the swing of it, so that's why the code is set up this way, however it doesn't paginate the data at all. Instead it adds 1 to the startrow in the GET URL everytime you click the "next" link. Kind of like this:

Original Query String URL:
http://www.*****.com/php/*****.php?startrow=0

3 examples of what happens when you click the "next" link 3 times in a row:
http://www.*****.com/php/*****.php?startrow=1
http://www.*****.com/php/*****.php?startrow=2
http://www.*****.com/php/*****.php?startrow=3

while that looks great in the URL, it doesn't change the data displayed at all.

Here's the code (I didn't include the display code for how the data looks when it's pulled from the query, I didn't think that really applied to this question and it works great as is):



Code:
 
<? 
 
$hostname = "localhost"; // The DB server. 
$username = "***********"; // The username you created for this database. 
$password = "***********"; // The password you created for the username. 
$usertable = "***********"; // The name of the table you made. 
$dbName = "************"; // This is the name of the database you made. 
MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
@mysql_select_db( "$dbName") or die( "Unable to select database"); 
 
//error message (not found message)begins 
$XX = "No results found, please try your search again. Try limiting your search to specific keywords."; 
 
//query details table begins
$query = mysql_query("SELECT * FROM ***** WHERE keywords LIKE '%$search%' LIMIT 1");
while ($row = @mysql_fetch_array($query)) 
{ 
$variable1=$row["*****"];
$variable2=$row["*****"]; 
$variable3=$row["*****"]; 
$variable4=$row["*****"];
$variable5=$row["*****"];
$variable6=$row["*****"];
$variable7=$row["*****"];
$variable8=$row["*****"];
$variable9=$row["*****"];
$variable10=$row["*****"];
$variable11=$row["*****"];
 
//check if this is the first page
if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) {
//we give the value of the first row to 0
$startrow = 0;
 
//otherwise we take the value from the URL
} else {
$startrow = (int)$_GET['startrow'];
}
 
//this part goes after the checking of the $_GET var
$fetch = mysql_query("SELECT * FROM ***** LIMIT $startrow, 1")or
die(mysql_error());
//now this is the link..
echo '<a href="*****.php?startrow='.($startrow+1).'">Next</a>';
 
?>

Last edited by UVL; 04-28-04 at 10:36 AM.
Reply With Quote
  #2 (permalink)  
Old 04-28-04, 10:43 AM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Change this line:
PHP Code:

//otherwise we take the value from the URL

} else {
$startrow = (int)$_GET['startrow'];

to this:
PHP Code:

//otherwise we take the value from the URL

} else {
$startrow int($_GET['startrow']);

I assume you were in a hurry and messed up the int() function.
Reply With Quote
  #3 (permalink)  
Old 04-28-04, 10:49 AM
UVL's Avatar
UVL UVL is offline
Newbie Coder
 
Join Date: Apr 2004
Location: Deep Underground
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Keith
Change this line:
PHP Code:

//otherwise we take the value from the URL
} else {
$startrow = (int)$_GET['startrow'];

to this:
PHP Code:

//otherwise we take the value from the URL
} else {
$startrow int($_GET['startrow']);

I assume you were in a hurry and messed up the int() function.
Hmm....That didn't work. It gives me this error (once again *'s replace actual data):

Fatal error: Call to undefined function: int() in /home/*****/public_html/php/*****.php on line 106

(Line 106 is where (int)_GET['startrow']; was, and I got the error after I changed it to what you mentioned)

Last edited by UVL; 04-28-04 at 10:55 AM.
Reply With Quote
  #4 (permalink)  
Old 04-28-04, 02:20 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Whoops... I was in a hurry too... int should be intval.
PHP Code:

$startrow intval($_GET['startrow']); 

Reply With Quote
  #5 (permalink)  
Old 04-29-04, 05:18 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
Quote:
Originally Posted by Keith
Change this line:
PHP Code:

 //otherwise we take the value from the URL 

} else { 
$startrow = (int)$_GET['startrow']; 

to this:
PHP Code:

 //otherwise we take the value from the URL 

} else { 
$startrow int($_GET['startrow']); 

I assume you were in a hurry and messed up the int() function.
no no ..
this line is correct !! it's used to cast type on $_GET['startrow'] to make it an integer ..

and for UVL, why are you having 2 queries! also your code will only display one new record at a time !

your code is this:
PHP Code:

//this part goes after the checking of the $_GET var

$fetch mysql_query("SELECT * FROM ***** LIMIT $startrow, 1")or
die(
mysql_error());
//now this is the link..
echo '<a href="*****.php?startrow='.($startrow+1).'">Next</a>'
you didn't call mysql_fetch_assoc() to bring the new data from this query !?
so you need something like this:
PHP Code:

]//this part goes after the checking of the $_GET var

$fetch mysql_query("SELECT * FROM ***** LIMIT $startrow, 1")or
die(
mysql_error());
$result mysql_fetch_array($fetch);

echo 
$result[0];

//now this is the link..
echo '<a href="*****.php?startrow='.($startrow+1).'">Next</a>'
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]

Last edited by NeverMind; 04-29-04 at 05:22 AM.
Reply With Quote
  #6 (permalink)  
Old 04-29-04, 08:32 AM
UVL's Avatar
UVL UVL is offline
Newbie Coder
 
Join Date: Apr 2004
Location: Deep Underground
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Ok neither of those worked. Could one of you please show me what the entire script should look like. I'm still new to PHP and I'd really appreciate an example. I had the search Query written before I went to Paginate it, I had thought I could just add on to the Query Script and Data Display Script to paginate it. So, I'm getting confused quickly.If you would do that I would be very greatful.
Reply With Quote
  #7 (permalink)  
Old 04-29-04, 09:02 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
PHP Code:

$hostname "localhost"// The DB server. 

$username "***********"// The username you created for this database. 
$password "***********"// The password you created for the username. 
$usertable "***********"// The name of the table you made. 
$dbName "************"// This is the name of the database you made. 
MYSQL_CONNECT($hostname$username$password) OR DIE("DB connection unavailable");
@
mysql_select_db"$dbName") or die( "Unable to select database"); 
 
//error message (not found message)begins 
$XX "No results found, please try your search again. Try limiting your search to specific keywords."

//check if this is the first page
if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) {
//we give the value of the first row to 0
$startrow 0;
//otherwise we take the value from the URL
} else {
$startrow = (int)$_GET['startrow'];
}
 
//query details table begins
$query mysql_query("SELECT * FROM ***** WHERE keywords LIKE '%$search%' LIMIT $startrow, 1");
while (
$row = @mysql_fetch_array($query)) 

$variable1=$row["*****"];
$variable2=$row["*****"]; 
$variable3=$row["*****"]; 
$variable4=$row["*****"];
$variable5=$row["*****"];
$variable6=$row["*****"];
$variable7=$row["*****"];
$variable8=$row["*****"];
$variable9=$row["*****"];
$variable10=$row["*****"];
$variable11=$row["*****"];


//now this is the link..
echo '<a href="*****.php?startrow='.($startrow+1).'">Next</a>';
?> 
try this one, it should bring one row per page ..

I think you added the extra query which useless in your code..
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #8 (permalink)  
Old 04-29-04, 09:17 AM
UVL's Avatar
UVL UVL is offline
Newbie Coder
 
Join Date: Apr 2004
Location: Deep Underground
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
ok that worked and it's paginating, but it's still not keeping the search parameters together. For example, if I search for "Anitiques" it pulls up one record to display, (because the code is set up that way at the moment). When I click next, it just goes to the next record regardless what was searched for, which might be "Pillows". I'm trying to get it so if I search for "Antiques" it will only display all of the Antique Database Records, and paginates through only them. Something like that.
Reply With Quote
  #9 (permalink)  
Old 04-29-04, 09:28 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
you need to pass the search keywords in the URL as well with the "next" link ..

so add this part before mysql_query();
PHP Code:

if (isset($_GET['search'])) {

  
$search urldecode($_GET['search']);
} else {
  
$search $_POST['search'];

and now your link should be like this:
PHP Code:

echo '<a href="*****.php?startrow='.($startrow+1).'&search='.urlencode($search).'">Next</a>'
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #10 (permalink)  
Old 04-29-04, 09:31 AM
UVL's Avatar
UVL UVL is offline
Newbie Coder
 
Join Date: Apr 2004
Location: Deep Underground
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
NeverMind you completely rock!Thankyousomuch.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
What's wrong? 7crystal7 PHP 1 03-25-04 12:23 PM
what's wrong in this statment? freak Perl 1 02-03-04 07:45 AM
What have I done wrong now??? DAL Perl 4 11-24-03 06:26 PM
help me determine whats wrong Cmrdrv The Lounge 6 08-12-03 11:57 AM
something wrong with this code superman PHP 3 07-06-03 10:55 PM


All times are GMT -5. The time now is 10:30 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.