Announcement

Collapse
No announcement yet.

A couple of php\mySQL questions

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • A couple of php\mySQL questions

    I'm sorry if these sound like "n00b" questions, but I really need help with them.

    The 2 questions are:

    1) How can I make it so that on a php\mySQL news page the news posts show up most recent first, rather than first on in the table first?

    and

    2) How can I make multiple page resource links, like say 20 resources per page sort of thing?

    If anyone can help it would be most appreciated.

  • #2
    Thank you for that worked lovely, now all I need is an answer to my 2nd query and I can finish my project thingy

    Comment


    • #3
      try something similar to this:
      Code:
      if ($entries_per_page < $total) {
       $remain = $total % $entries_per_page;
       $i = $total-$remain;
       while ($i > 0) {
         $num_max = $i;
         $num_min = $num_max-$entries_per_page;
         $num_min++;
         echo "<option value=\"$remain\">$num_min-$num_max</option>\n";
         $i = $num_min-1;
         $remain += $entries_per_page;
       }
      }
      
      
      $id = $total-$entry;
      $i=0;

      sorry for not including this:
      Code:
      $entries_per_page ='10'; //entries per page
      Last edited by Dark_Wizard; Fri 18 Jan '02, 3:05pm.

      Comment


      • #4
        Paginated list, eh?

        snippet from working code... modified a bit to remove anything specific to the previous project
        PHP Code:
        // num to display per page
        // the more records per page, the slower the page will load
        $PerPage 10;
        // Number of page links displayed (on either side of current page)
        $Page_Links 2;


        if (
        $pageNum || !$pageNum$pageNum 1;
        /*****************************************/
        function Do_Links($querystring) {
            global 
        $PerPage$Page_Links$pageNum$num_rows$PHP_SELF;
            
            
        $num_pages ceil($num_rows $PerPage);
            
            
        $start = ($pageNum > ($Page_Links 1)) ? ($pageNum $Page_Links) : 1;
            
        $end = ($num_pages > ($pageNum $Page_Links)) ? ($pageNum $Page_Links) : $num_pages;
            
        echo<<<myHTML
        <table border=0 cellpadding=0 cellspacing=0 align=center>
         <tr>
          <td>
            <font size=2 face=arial>
            <a href="
        $PHP_SELF?pageNum=1&$querystring">First</a>
            &nbsp;
        myHTML;
            
            for (
        $i=$start$i<$end$i++) {
                if (
        $i == $pageNum)
                    echo 
        "\t<b>$i</b>\n";
                else
                    echo 
        "\t<a href=\"$PHP_SELF?pageNum=$i&$querystring\">$i</a>\n";
            }
            if (
        $end == $pageNum)
                echo 
        "<b>$end</b>\n";
            else
                echo 
        "<a href=\"$PHP_SELF?pageNum=$end&$querystring\">$end</a>\n";
                
        echo<<<myHTML
            &nbsp;
            <a href="
        $PHP_SELF?pageNum=$num_pages&$querystring">Last</a>
            </font>
          </td>
         </tr>
        </table>
        myHTML;
        //END Do_Links()
        /*****************************************/




        // Build the query
        $query "SELECT * FROM table_name";

        $result mysql_query($query);

        if (!
        $result) {
            echo 
        "<tr>\n";
            echo 
        "<td colspan='3' align='center'>\n";
            echo 
        "<b>Query Error:</b>" mysql_error() . "<BR>\n";
            echo 
        "<b>Query:</b> $query\n";
            echo 
        "</td>\n";
            echo 
        "</tr>\n";
        }
        $num_rows mysql_num_rows($result);

        if (
        $num_rows == 0) {
            echo 
        "<tr>\n";
            echo 
        "<td colspan='3' align='center'>\n";
            echo 
        "<b>No matches found.</b>\n";
            echo 
        "<!--\n";
            echo 
        "Query Error:" mysql_error() . "\n";
            echo 
        "Query: $query\n";
            echo 
        "-->\n";
            echo 
        "</td>\n";
            echo 
        "</tr>\n";
        }
        else {

        // where do we start?
        $pageNum = ($pageNum 1) ? $pageNum;
        $start $PerPage * ($pageNum 1);
        // go there!
        mysql_data_seek($result$start);
        }

        for (
        $i=0$i<$PerPage$i++) {
        if (
        $tmp mysql_fetch_array($result)) {
            
        extract($tmp);
            
            echo 
        " <tr>\n";
            echo 
        "  <td>$field1</td>\n";
            echo 
        "  <td>$field2</td>\n";
            echo 
        "  <td>$field3</td>\n";
            echo 
        " </tr>\n";
        }
        }

        echo 
        "</TABLE>\n";

        if (
        $num_rows 0) {
            
        // build a querystring
            
        $querystring "this=$this";
            
        //pagenate it!
            
        Do_Links($querystring);


        Comment


        • #5
          Thank you very much for all the help

          Comment


          • #6
            Hehe...I like yours better Max Albert

            Comment


            • #7
              does it still work?
              I took a lot of junk out.. I hope I didn't break it.

              Comment


              • #8
                Hey Max,

                I almost have this working based on the code you posted above, but there is still a small problem. Each page number's results are one off... and brings up the final listing from the previous page. Why is this happening? I got the sort by letter drop-down box working correctly, but I just need to finnish this last little problem off.

                example: http://worldclassdesigns.com/forums/manufacturers.php

                Keep in mind that I have it set to 2 records per page for testing purposes.

                PHP Code:
                if ($pageNum || !$pageNum$pageNum 1
                global 
                $PerPage$Page_Links$pageNum$num_rows$PHP_SELF

                $num_pages ceil($num_rows $PerPage); 

                $start = ($pageNum > ($Page_Links 1)) ? ($pageNum $Page_Links) : 1
                $end = ($num_pages > ($pageNum $Page_Links)) ? ($pageNum $Page_Links) : $num_pages
                eval(
                "\$first .= \"".gettemplate("manufacturer_first",1,0)."\";");

                for (
                $i=$start$i<$end$i++) { 
                eval(
                "\$number2 .= \"".gettemplate("manufacturer_number2",1,0)."\";"); 
                }
                eval(
                "\$number4 .= \"".gettemplate("manufacturer_number4",1,0)."\";"); 
                eval(
                "\$last .= \"".gettemplate("manufacturer_last",1,0)."\";");
                if (
                $ltr) {
                $condition "name LIKE \"$ltr%\" ";
                $condition_str "WHERE " $condition;
                $limit_str "LIMIT $PerPage";
                } else {
                $condition '';
                $limit_str "LIMIT " $pageNum ", $PerPage";

                Thanks,
                -JRW
                Last edited by jrwap; Wed 13 Feb '02, 12:43pm.
                LostDiscussion.com - Fan base community for ABC's Lost | Club-tC.com - Scion tC Enthusiast Site | PcTechTalk.com - What's your issue? | eInvesting.com - Play the Market, Learn Investing, WIN!

                Comment


                • #9
                  try changing your code to look like this...
                  PHP Code:
                  if ($pageNum || !$pageNum$pageNum 1

                  $num_pages ceil($num_rows $PerPage); 
                  $start = ($pageNum > ($Page_Links 1)) ? ($pageNum $Page_Links) : 1;
                  $end = ($num_pages > ($pageNum $Page_Links)) ? ($pageNum $Page_Links) : $num_pages;

                  eval(
                  "\$first .= \"".gettemplate("manufacturer_first",1,0)."\";");

                  for (
                  $i=$start$i<$end$i++) { 
                      eval(
                  "\$number2 .= \"".gettemplate("manufacturer_number2",1,0)."\";"); 
                  }
                  eval(
                  "\$number4 .= \"".gettemplate("manufacturer_number4",1,0)."\";"); 
                  eval(
                  "\$last .= \"".gettemplate("manufacturer_last",1,0)."\";");

                  // where do we start?
                  $start $PerPage * ($pageNum 1);

                  if (
                  $ltr) {
                      
                  $condition "name LIKE \"$ltr%\" ";
                      
                  $condition_str "WHERE " $condition;
                      
                  $limit_str "LIMIT $PerPage";
                  } else {
                      
                  $condition '';
                      
                  $limit_str "LIMIT $start$PerPage";

                  Comment


                  • #10
                    Max, you are the man!!!

                    Thanks,

                    -JRW
                    LostDiscussion.com - Fan base community for ABC's Lost | Club-tC.com - Scion tC Enthusiast Site | PcTechTalk.com - What's your issue? | eInvesting.com - Play the Market, Learn Investing, WIN!

                    Comment

                    widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
                    Working...
                    X