Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
next / previous row ...?
Old 05-25-2005, 08:24 AM next / previous row ...?
Novice Talker

Posts: 14
Trades: 0
Hi!

Ive been trying to paginating my articles. First Ive been trying with this script:

PHP Code:
        $goin_back $sub_action 1;
        
$goin_ahead $sub_action 1;

        
$NEXT "<a href='content.php?article.$goin_ahead'>next article >></a> ";
        
$PREV "<a href='content.php?article.$goin_back'><< previous article</a> ";

        echo 
"$PREV";
        echo 
"$NEXT"
$sub_action is the article_id for example article.4

this works but the problem is that the article_id:s arn't like: 1,2,3,4,5,6,7 ...
some of the id:s are missing so the articles_id:s are more like: 1,3,6,7,10,11 ...

therefor ive been thinking that there should be a way of calling for the next and previous row in the database instead of the previous and next article_id.

How can I do this?

Anyone knows a code, maybe similiar to mine above but selecting the next row instead of next article_id???

Also how can make the next link dissapear when you have reached the last row in the databastable and same thing with previouslink when you reach the first row?

Something like:

PHP Code:
if($sub_action FIRST ROW IN THE TABLE){
$PREV =  "";
}
if(
$sub_action LAST ROW IN THE TABLE){
$NEXT =  "";

Thanks for your help!

Per
perik is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-25-2005, 08:44 AM Try This
webmasterjunkie's Avatar
Average Talker

Posts: 20
Location: Maryland
Trades: 0
Change the words in red to match your MySQL DB.
To get the total amount of articles try this:

Code:
$db_host = "yourhost";
$db_user = "username";
$db_pass = "password";
$db_name = "database";
$db_table = "table";
$db_connection = mysql_connect ($db_host, $db_user, $db_pass)OR die (mysql_error());  
$db_select = mysql_select_db ($db_name)or die (mysql_error());
$limit = 30;                
$query_count = "SELECT * FROM $db_table"; 
$result_count = mysql_query($query_count);     
$totalrows = mysql_num_rows($result_count); 
if(empty($page)){ 
    $page = 1; 
}
Perform another query to store your database variables in:

Code:
$limitvalue = $page * $limit - ($limit); 
$query  = "SELECT * FROM $db_table LIMIT $limitvalue, $limit";         
$result = mysql_query($query) or die("Error: " . mysql_error()); 
while($row = mysql_fetch_array($result)){
    $content = $row['article_text'];
    $sub_action = $row['article_id'];
}
Then you have to paginate your results:

Code:
if($page != 1) { 
    $pageprev = $page -1;          
    $page_results = "<a href=\"$PHP_SELF?page=$pageprev\">PREV $limit</a> "; 
} else { 
    $page_results = "PREV $limit "; 
} 
$numofpages = $totalrows / $limit; 
for($i = 1; $i <= $numofpages; $i++){ 
    if($i == $page) { 
        $page_results .= ($i." ");
    } else { 
        $page_results .= "<a href=\"$PHP_SELF?page=$i\">$i</a> "; 
    } 
} 
if(($totalrows % $limit) != 0) { 
    if($i == $page) { 
        $page_results .= ($i." "); 
    } else { 
        $page_results .= "<a href=\"$PHP_SELF?page=$i\">$i</a> "; 
    } 
} 
if(($totalrows - ($limit * $page)) >= 0){ 
    $pagenext = $page +1; 
    $page_results .= "<a href=\"$PHP_SELF?page=$pagenext\">NEXT $limit</a>"; 
} else { 
    $page_results .= "NEXT $limit"; 
}
All of the code above should get you started pretty good. Forgive me if it's a bit choppy, but as I said, it should give you a good start.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by webmasterjunkie; 05-25-2005 at 09:45 AM..
webmasterjunkie is offline
Reply With Quote
View Public Profile Visit webmasterjunkie's homepage!
 
Old 05-25-2005, 09:17 AM
Novice Talker

Posts: 14
Trades: 0
thanks for your reply!!!

where shall I put this three parts script???

I got I file called content.php which are calling the articles from the database. Its in there I got my previous script, the one I talked about in my frist post. Shall I replace my old code whith these three or shall I put theme on different places and in that case, where?

and by the way ... what is a query???

thanks

Last edited by perik; 05-25-2005 at 09:19 AM..
perik is offline
Reply With Quote
View Public Profile
 
Old 05-25-2005, 09:24 AM
webmasterjunkie's Avatar
Average Talker

Posts: 20
Location: Maryland
Trades: 0
Place all three parts of the code I posted onto one page to replace your old code. The only thing it will display as is, is the total amount of pages/articles. You will have to output the other content as well. Don't mind my terminology, i.e., query, just a phrase.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
webmasterjunkie is offline
Reply With Quote
View Public Profile Visit webmasterjunkie's homepage!
 
Old 05-25-2005, 09:38 AM
Novice Talker

Posts: 14
Trades: 0
I put them all together like this:


PHP Code:
// ##### NEXT ---------------------------------------------------------------------

$db_host "yourhost";
$db_user "username";
$db_pass "password";
$db_name "database";
$db_table "table";
$db_connection mysql_connect ($db_host$db_user$db_pass)OR die (mysql_error());  
$db_select mysql_select_db ($db_name)or die (mysql_error());
$limit 30;                
$query_count "SELECT * FROM $db_table"
$result_count mysql_query($query_count);     
$totalrows mysql_num_rows($result_count); 
if(empty(
$page)){ 
    
$page 1



$limitvalue $page $limit - ($limit); 
$query  "SELECT * FROM $db_table ORDER BY url ASC LIMIT $limitvalue$limit";         
$result mysql_query($query) or die("Error: " mysql_error()); 
while(
$row mysql_fetch_array($result)){
    
$content $row['article_text'];
    
$sub_action $row['article_id'];
}


if(
$page != 1) { 
    
$pageprev $page -1;          
    
$page_results "<a href=\"$PHP_SELF?page=$pageprev\">PREV $limit</a> "
} else { 
    
$page_results "PREV $limit "

$numofpages $totalrows $limit
for(
$i 1$i <= $numofpages$i++){ 
    if(
$i == $page) { 
        
$page_results .= ($i." ");
    } else { 
        
$page_results .= "<a href=\"$PHP_SELF?page=$i\">$i</a> "
    } 

if((
$totalrows $limit) != 0) { 
    if(
$i == $page) { 
        
$page_results .= ($i." "); 
    } else { 
        
$page_results .= "<a href=\"$PHP_SELF?page=$i\">$i</a> "
    } 

if((
$totalrows - ($limit $page)) >= 0){ 
    
$pagenext $page +1
    
$page_results .= "<a href=\"$PHP_SELF?page=$pagenext\">NEXT $limit</a>"
} else { 
    
$page_results .= "NEXT $limit"
}


// ##### End ---------------------------------------------------------------------- 
then I replaced the values for connecting to database but got this errorlog:

Quote:
Error: Unknown column 'url' in 'order clause'
I also tried to delete the connection part cause Im already connected I guess to the database but I couldnt manage.

One more thing, my page sometimes for some reason wont like " sometimes only acceptes ' . I dont know, I hardly know any programing so maybe this is a part of the php-language

any suggestion of what I can do to get it working
perik is offline
Reply With Quote
View Public Profile
 
Old 05-25-2005, 09:43 AM
webmasterjunkie's Avatar
Average Talker

Posts: 20
Location: Maryland
Trades: 0
Remove the

ORDER BY url ASC

If you can, PM me the page you are working on, so I can troubleshoot with you.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
webmasterjunkie is offline
Reply With Quote
View Public Profile Visit webmasterjunkie's homepage!
 
Old 05-25-2005, 09:53 AM
Novice Talker

Posts: 14
Trades: 0
Thanks for your help but I found another script that works:

PHP Code:
 //previous 
if($sql -> db_Select("content""content_id""content_id<$sub_action ORDER BY content_id DESC LIMIT 1")){ 
$row $sql -> db_Fetch();  
extract($row); 
$prev $content_id
echo 
"<a href='content.php?article.$prev'><< previous article</a> "


//next 
if($sql -> db_Select("content""content_id""content_id > $sub_action ORDER BY content_id Asc LIMIT 1")){ 
$row $sql -> db_Fetch();  
extract($row); 
$next $content_id
echo 
"<a href='content.php?article.$next'>next article>></a> "

thanks

Perik
perik is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to next / previous row ...?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.36555 seconds with 12 queries