Displaying records in a cyclic order

Einklappen
X
 
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • Displaying records in a cyclic order

    Hello,

    I use the following code in-order to display records based on the week day.My intention behind writing this one was to display 90 records in a cyclic fashion.(i.e, first 90 on monday, next 90 on tue and so on...)
    PHP-Code:
    $today getdate();  
    $day $today['weekday']; 

    switch(
    $day)  
    {  
    case 
    "Monday":  
        
    $query="SELECT id,introtext,zipcode,image,telephone,description FROM tbl_personals ORDER BY id Limit 90";  

        
    $numresults=mysql_query($query,$connection);  
        
    $numrows=mysql_num_rows($numresults); 
    break;  
    case 
    "Tuesday":  
        
    $query="SELECT id,introtext,zipcode,image,telephone,description FROM tbl_personals ORDER BY id Limit 90,90";  

        
    $numresults=mysql_query($query,$connection);  
        
    $numrows=mysql_num_rows($numresults); 
    break;  
    case 
    "Wednesday"
        
    $query="SELECT id,introtext,zipcode,image,telephone,description FROM tbl_personals ORDER BY id Limit 180,90";  

        
    $numresults=mysql_query($query,$connection);  
        
    $numrows=mysql_num_rows($numresults); 
    break;  
    case 
    "Thursday"
        
    $query="SELECT id,introtext,zipcode,image,telephone,description FROM tbl_personals ORDER BY id Limit 270,90";  

        
    $numresults=mysql_query($query,$connection);  
        
    $numrows=mysql_num_rows($numresults); 
    break; 
    case 
    "Friday"
        
    $query="SELECT id,introtext,zipcode,image,telephone,description FROM tbl_personals ORDER BY id Limit 360,90";  

        
    $numresults=mysql_query($query,$connection);  
        
    $numrows=mysql_num_rows($numresults); 
    break; 
    case 
    "Saturday"
        
    $query="SELECT id,introtext,zipcode,image,telephone,description FROM tbl_personals ORDER BY id Limit 450,90";  

        
    $numresults=mysql_query($query,$connection);  
        
    $numrows=mysql_num_rows($numresults); 
    break; 
    case 
    "Sunday"
        
    $query="SELECT id,introtext,zipcode,image,telephone,description FROM tbl_personals ORDER BY id Limit 540,90";  

        
    $numresults=mysql_query($query,$connection);  
        
    $numrows=mysql_num_rows($numresults); 
    break;         


    My question is - what happens if there are 1000 records???
    How can i get the second weekday(Monday) displaying results from 631 onwards and so on??The condition is I want to display every day 90 records

    or in other words, if i have 100 records in the table, I like to display first 90 today and from 91 to 100 and from 1 to 80 tomorrow

    Is there any way to modify my code or does some body have any other alternative??

    Thanks
    Happiness is a state of mind.
    Change your mind and be happy!

  • #2
    Well at first make it smaller

    Code:
    $day = date("W");
    $offset = $day*90;
    
    $query="SELECT id,introtext,zipcode,image,telephone,description FROM tbl_personals ORDER BY id Limit ". $offset . ",90";  
    $numresults=mysql_query($query,$connection);
    by the way $numrows=mysql_num_rows($numresults); will be 90 in any case...
    Zuletzt geändert von MelloPie; 21.06.2003, 20:55.
    Beantworte nie Threads mit mehr als 15 followups...
    Real programmers confuse Halloween and Christmas because OCT 31 = DEC 25

    Kommentar


    • #3
      for more than 1000 records you should think of a new column
      Beantworte nie Threads mit mehr als 15 followups...
      Real programmers confuse Halloween and Christmas because OCT 31 = DEC 25

      Kommentar


      • #4
        Mellopie,
        Is it really that simple as you said.I was wondering that i would be bangging my head all day today in order to make it working.Anyways, let me make it clear once again with less no of records in-order to avoid confusion.

        Say, I want to display 5 records everyday.I have only 8 records in a table.Ok, today, I'll show first 5, i.e(1,2,3,4,5) records and tomorrow the records should be (6,7,8,1,2).So, this will be a cyclic process.

        Well, thanks again.I'll give a try.
        Happiness is a state of mind.
        Change your mind and be happy!

        Kommentar


        • #5
          Mellopie,
          Sorry that has'nt helped me.It always returns there are no records.
          I have 12 records in a table and then i'm trying to show the first 7 records today and then the next 5 plus the first 2 tomorrow(all together 7).

          At first, I get the day and then set the off-set.
          PHP-Code:
          $day date("W");
          $offset $day*7
          and heres the query:
          PHP-Code:
          // Lets select the record fields from our database table and check for results 

              
          $query="SELECT id,introtext,zipcode,image,telephone,description FROM tbl_personals ORDER BY id Limit "$offset ",7";  
              
          $numresults=mysql_query($query,$connection); 
              
          $numrows=mysql_num_rows($numresults); 
              
          // get results

              
          $result mysql_query($query) or die("Couldn't execute query");

          // We have no results 

              
          if ($numrows == 0) { 
              echo 
          "Sorry,there are no records"
              } 

          // We carry out the table population using a 'while' loop 
          Any ideas??
          Thanks once again.
          Happiness is a state of mind.
          Change your mind and be happy!

          Kommentar


          • #6
            PLS check content of $offset date('W') works with php >= 4.1.0
            I havent checked that.
            Well this cycling is a tricky thing maybe you shoul make a
            SELECT COUNT(*) FROM tbl_personals
            first to get the amount of records
            Then something like
            if ( $offset + $limit > $result_of_count)
            get the first x records

            where $limit is you amount of records you get per datafetch
            that my idea
            Beantworte nie Threads mit mehr als 15 followups...
            Real programmers confuse Halloween and Christmas because OCT 31 = DEC 25

            Kommentar


            • #7
              you might be able to do it like this
              PHP-Code:
              $test = array('News 1','News 2','News 3','News 4','News 5','News 6''News 7');
              $day=date('w'); //Numeric representation of the day of the week  0 (for Sunday) through 6 (for Saturday)
              $key=each($test);
              while (
              $key['key']!=$day)
                  
              $key=each($test);
              prev($test);
              prev($test);
              for (
              $i=0;$i<7;$i++)
              {
                  
              $news = (next($test)) ? current($test) : reset($test);
                  echo 
              $news.'<br />';

              just a short example but i think it should do what you want it to do.

              of course it would be better to ask the database for the newsid only since you have to fetch all records
              Ich denke, also bin ich. - Einige sind trotzdem...

              Kommentar

              Lädt...
              X