General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 28th, 2002, 06:18 AM
TheMike TheMike is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Posts: 6 TheMike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy Dynamic paging class

I really appreciated the article "Building A Dynamic MySQL Paging Class With PHP" from Joe O'Donnell. After modifiying it for my own use I still have one question.
Where in the code can I modify the data I retrieve from the database??
For instance I only want to show the year information from a date field. I know what PHP function to use but not where to put it in the code.
Regards TheMike

Reply With Quote
  #2  
Old April 28th, 2002, 09:06 AM
mytch mytch is offline
Dev Articles Novice (500 - 999 posts)
 
Join Date: Apr 2002
Location: Sydney, Australia
Posts: 589 mytch User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Hi,
For that you'd use the date() function. So for example, if $theDate was the date, you'd output its year in the format of 2002 like this:

echo date("Y", $theDate);

Reply With Quote
  #3  
Old May 1st, 2002, 09:43 AM
samscripts samscripts is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2002
Location: London, UK
Posts: 2 samscripts User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
or you could just format the date how you want it in the mysql query using the date_format() function:

eg: SELECT date_format(datefield,'%Y') FROM yourtable

Reply With Quote
  #4  
Old August 13th, 2002, 06:48 AM
craigyk craigyk is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: UK
Posts: 4 craigyk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Help With the Building A Dynamic MySQL Paging Class With PHP Article

I have just found this article that is ideal for what I need to acheive for a site I am developing. Unfortunately the support material URL on page 2 is no longer there.

I have tried the link http://www.devarticles.com/files/class.recnav.php.txt but no page is available. Does anyone have the original file ??

Reply With Quote
  #5  
Old August 13th, 2002, 08:12 PM
Ben Rowe
Guest
Dev Articles Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Firstly, welcome to the forums, nice having you hear

craigyk: Unfortunally we lost some files during a server crash on our old host. The file that you are asking about has its suport file missing because of that crash, and we are unable to retrive it again

Sorry for the Inconvience

Reply With Quote
  #6  
Old August 14th, 2002, 03:43 AM
craigyk craigyk is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: UK
Posts: 4 craigyk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thats OK, I have gone thorugh the article and managed to piece it together from another forum post and it works great

Reply With Quote
  #7  
Old August 14th, 2002, 04:58 AM
craigyk craigyk is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: UK
Posts: 4 craigyk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I have just stumbled into a slight problem. The records that I want to produce are going to be based on a random selection (ORDER BY RAND()) The problem with this is that when i navigate between pages, the results keep randomising, Does anyone have any suggestions on how to keep the original records produced in tact?

Reply With Quote
  #8  
Old August 14th, 2002, 04:59 AM
TheMike TheMike is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Posts: 6 TheMike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Ben:
I will check my computer at home tonight I think I still have this file, if you want me to mail it so you can fix the url I will.

Reply With Quote
  #9  
Old August 14th, 2002, 10:43 AM
crazytrain81 crazytrain81 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 232 crazytrain81 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Lightbulb Here's what you need

At the portion of the code below:

for($i = 0; $i < mysql_num_fields($result); $i++) {

$newRow = str_replace("<| row" . $i . " |>", $row[$i], $newRow);

}

You need to make your change here. Something like this should work:

for($i = 0; $i < mysql_num_fields($result); $i++) {

if ($i == $dateField) {
$row[$i] = date();
}
$newRow = str_replace("<| row" . $i . " |>", $row[$i], $newRow);

}

$dateField would of course be the array index that would hold your date value (column number in you SQL table).

Hope that helps

Reply With Quote
  #10  
Old August 14th, 2002, 07:59 PM
Ben Rowe
Guest
Dev Articles Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Quote:
Originally posted by TheMike
Ben:
I will check my computer at home tonight I think I still have this file, if you want me to mail it so you can fix the url I will.


Hey if you could find it and send it to either me or mytch, that would be great, just email to URL

craigyk: Randomizing your code could be a bit hard, what you would have to do is, create a query that randomizes ALL the rows in the table, then print out that the primary keys into something like this

1,6,3,4,8,9,2,5,7 something like that, then you would have to send that to each page, explode it, and do something like this

then on each page, go thought the ids, and somehow limit which ones are printed out.

If you could also explain why you want to randomize the query, that would be helpful, as there may be a easier way

Reply With Quote
  #11  
Old August 15th, 2002, 03:43 AM
craigyk craigyk is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: UK
Posts: 4 craigyk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
The reason for the randomising is because it is going to be used for a directory listing.

I am tryig to be unbiased towards the entries in there, I don't want to order them alphabetically because people would just use names begining with a or numbers.

I don't want to order them by ID either because it is not fair towards the top/end of the list. The point for randomising is to create a fair output. ope this has made sense.

Craig

Reply With Quote
  #12  
Old August 15th, 2002, 05:53 PM
Isaac Isaac is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: pants.
Posts: 7 Isaac User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to Isaac Send a message via Yahoo to Isaac
I can forshadow the future and managed to save the file.
PHP Code:
<?php

  
// Default number of records
  
define("DEFAULT_NUM_RECS"10);
  
define("DEFAULT_TEMPLATE_HEADER""<table width='100%' cellspacing='0' cellpadding='0' border='0'><tr><td width='10%' height='21' bgcolor='black' align='center'><font color='white' face='verdana' size='1'><b>Id</b></font></td><td width='80%' height='21' bgcolor='black'><font color='white' face='verdana' size='1'><b>Description</b></font></td><td width='10%' height='21' bgcolor='black' align='center'><font color='white' face='verdana' size='1'><b>Price</b></font></td></tr>");
  
define("DEFAULT_TEMPLATE""<tr><td width='10%' height='21' align='center'><font face='verdana' size='1' color='darkblue'><b><| row0 |></b></font></td><td width='80%' height='21'><font face='verdana' size='1' color='black'><| row1 |></font></td><td width='10%' height='21'><font face='verdana' size='1' color='red'>$<| row2 |></font></td></tr>");
  
define("DEFAULT_TEMPLATE_FOOTER""</table>");

  class 
RecNav
  
{
    function 
RecNav(&$LinkIdentifier$Query$Template=DEFAULT_TEMPLATE$TemplateHeader=DEFAULT_TEMPLATE_HEADER$TemplateFooter=DEFAULT_TEMPLATE_FOOTER$RecsPerPage=DEFAULT_NUM_RECS)
    {
      
// Validate constructor parameters
      
if(!@mysql_query("SELECT 1"$LinkIdentifier))
      { die(
"MYSQL link identifier is invalid"); }
      else
      {  
        
$this->__linkId $LinkIdentifier;
      }
      
      if(!
ereg("^SELECT"$Query))
        { die(
"Invalid query: query must start with 'SELECT'"); }

      else
        { 
$this->__query $Query; }
        
      if(
$Template == "")
        { 
$this->__template DEFAULT_TEMPLATE; }
      else
        { 
$this->__template $Template; }

      if(
$TemplateHeader == "")
        { 
$this->__templateHeader DEFAULT_TEMPLATE_HEADER; }
      else
        { 
$this->__templateHeader $TemplateHeader; }

      if(
$TemplateFooter == "")
        { 
$this->__templateFooter DEFAULT_TEMPLATE_FOOTER; }
      else
        { 
$this->__templateFooter $TemplateFooter; }
        
      if(!
is_numeric($RecsPerPage) || $RecsPerPage 1)
        { 
$this->__recsPerPage DEFAULT_NUM_RECS; }
      else
        { 
$this->__recsPerPage $RecsPerPage; }
    }
    
    function 
ShowRecs($Page=1)
    {
      
// Using the classes variables and the $Page variable,
      // the ShowRecs function will query the database and
      // return the records based on the $__template variable.
      
      
$finalOutput $this->__templateHeader;
      
      if(
$Page <= 1)
        {
          
$Page 1;
          
$query $this->__query " LIMIT 0, " $this->__recsPerPage;
        }
      else
        { 
$query $this->__query " LIMIT " . (($Page-1) * $this->__recsPerPage) . ", " $this->__recsPerPage; }

      
$result mysql_query($query);
      
$hasRecords mysql_num_rows($result) == false true;
          
      if(
$hasRecords)
      {
        
// At least one records returned from the query
        
while($row mysql_fetch_row($result))
        {
          
$newRow $this->__template;

          for(
$i 0$i mysql_num_fields($result); $i++)
           { 
$newRow str_replace("<| row" $i " |>"$row[$i], $newRow); }

          
$finalOutput .= $newRow;
        }
      }
      else
      {
        
// No records returned from the query
        
$newRow $this->__template;
        
$newRow str_replace("<| row0 |>""No records found"$newRow);
        
        
// Replace all template tags with &nbsp;
        
$newRow ereg_replace("<\| row[0-9] \|>""&nbsp;"$newRow);

        
$finalOutput .= $newRow;
      }
      
      
$finalOutput .= $this->__templateFooter;
      
      
// Build the recordset paging links
      
$numTotalRecs mysql_num_rows(mysql_query($this->__query));
      
$numPages ceil($numTotalRecs $this->__recsPerPage);
      
$nav "";
      
      
// Can we have a link to the previous page?
      
if($Page 1)
        
$nav .= "<a href='$PHP_SELF?page=" . ($Page-1) . "'><< Prev</a> |";

      for(
$i 1$i $numPages+1$i++)
      {
        if(
$Page == $i)
        {
          
// Bold the page and dont make it a link
          
$nav .= " <b>$i</b> |";
        }
        else
        {
          
// Link the page
          
$nav .= " <a href='$PHP_SELF?page=$i'>$i</a> |";
        }
      }

      
// Can we have a link to the next page?
      
if($Page $numPages)
        
$nav .= " <a href='$PHP_SELF?page=" . ($Page+1) . "'>Next >></a>";
        
      
// Strip the trailing pipe if there is one
      
$nav ereg_replace("\|$"""$nav);
      
      
$finalOutput .= "<div align='right'><font face='verdana' size='1'><br>Pages: $nav</font></div>";
      
      return 
$finalOutput;
    }
    
    var 
$__linkId;
    var 
$__dbType;
    var 
$__query;
    var 
$__template;
    var 
$__templateHeader;
    var 
$__templateFooter;
    var 
$__recsPerPage;
  
  }
?>

Please shower me with praise and I may help devArticles.com when it is in need again in the future.

Reply With Quote
  #13  
Old August 15th, 2002, 08:04 PM
Ben Rowe
Guest
Dev Articles Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Oh thank you, O wise one!

ehehh

thx

Ben Rowe

Reply With Quote
  #14  
Old August 25th, 2002, 07:27 PM
dennisj dennisj is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: Carlsbad, CA
Posts: 2 dennisj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Move class.navrec.php to server?

Hi

Using the code Isaaic posted and the html code fr