Help with listing stuff - $5 reward

Discussion in 'PHP' started by crazyryan, Feb 3, 2007.

  1. #1
    I have the following code, basically I want to sort by id, the highest would be the newest. I know I could change the query to something like ORDER by link_id ASC or something (well i dont know mysql but i know thats possible) .. however, when doing that, my links like /index.php?sortby=link_added&order=DESC do not work, I had someone change the mysql query so i could try it and I got mysql errors. Can anyone come up with a fix so the following automatically displays the newest links first and the things like /index.php?sortby=link_added&order=DESC, /index.php?sortby=link_visitors&order=DESC etc work.

    I will reward you with $5 + a positive rep.

    <?
    
                    // Include database
    
                    require '/home/proxygd/public_html/proxies/config.php';
    
                    // Number of items per page
    
                    $perpage = 10;
    
                    
    
                    // I am currently drunk.  This page works fine.  The code may not be the greatest, but it works.
    
                    // It's still better than Pixabit, bwuahahaha.
    
                    
    
                    // Base query
    
                    $q = "SELECT * from links WHERE confirmed = 'yes'";
    
                    
    
                    // Other modifiers
    
                    if ($_GET['type']) {
    
                        $type = $_GET['type'];
    
                        $q .= " WHERE link_type = '$type' && confirmed = 'yes'";
    
                    }
    
                    
    
                    // Check if sorting
    
                    
    
                    if ($_GET['sortby']) {
    
                        $sortby = $_GET['sortby'];
    
                        $q .= " ORDER BY $sortby";
    
                    }
    
                    
    
                    // Check sort order
    
                    if ($_GET['order']) {
    
                        $order = $_GET['order'];
    
                        $q .= " $order";
    
                    }
    
                    
    
                    $total = mysql_num_rows(mysql_query($q));
    
                    
    
                    // Check limit per page
    
                    if ($_GET['page']) {
    
                        $pp = $_GET['page'];
    
                        if ($pp == 1) {
    
                        $start = 0;
    
                        }
    
                        if ($pp > 1) {
    
                        $start = $perpage * ($pp - 1);
    
                        }
    
                        $q .= " LIMIT $start, $perpage";
    
                    }
    
                    else {
    
                    $q .= " LIMIT $perpage ";
    
                    }
    
                    $result = mysql_query($q) or die("Error in query " . mysql_error());
    
                    $numb = mysql_num_rows($result);
    
                    if ($numb > 0) {
    
                    while ($row = mysql_fetch_assoc($result)) {
    
                    // THIS IS THE PART WHERE YOU CAN MODIFY THE LINK OUTPUT
    
                    // DO NOT MODIFY ANY THING OUTSIDE OF THE FOLLOWING 4 LINES OR YOU WILL BREAK SOMETHING
    
                    echo "<p class=\"box\"><a href=\"$siteurl/$row[link_id]/\">$row[link_title]</a><br />";
    
                    echo "Description: " . $row['link_description'] . "<br />";
    
                    echo "Visitors: " . $row['link_visitors'] . "<br />";
    
                    echo "Type: " . $row['link_type'];
    
                    echo "</p>";
    
                    }
    
                    if ($pp > 1) {
    
                    $ts = 0;
    
                    foreach ($_GET as $key => $value) {
    
                        if ($key == "page") { $value--; }
    
                        if ($ts == 0) {
    
                        $ns .= "?" . $key . "=" . $value;
    
                        $ts++;
    
                        }
    
                        else {
    
                        $ns .= "&" . $key . "=" . $value;
    
                        $ts++;
    
                        }
    
                        }
    
                        echo "<a href=\"view_proxies.php" . $ns . "\">PREVIOUS</a>";
    
                    }
                    // Fix for pagination.
                    // PP was set to 0, causing equations to evaluate wrong.
                    if (($perpage * $pp) == "0") { $pp = "1"; }
                    // Fix for the pagination part 2
                    unset($ns);
                    if ($total > ($perpage * $pp)) {
                        $ts = 0;
                            foreach ($_GET as $key => $value) {
                                if ($key == "page") { $value++; }
                                if ($ts == 0) {
                                $ns .= "?" . $key . "=" . $value;
                                $ts++;
                                }        
                                else {
                                $ns .= "&" . $key . "=" . $value;
                                $ts++;
                                }
                            }
                            if (empty($ns) and $total > ($perpage * $pp)) { echo "<a href=\"view_proxies.php?page=2\">NEXT</a>"; }
                            if (!empty($ns) and $total > ($perpage * $pp)) { echo "<a href=\"view_proxies.php" . $ns . "\">NEXT</a>"; }
    
                        }
    
                    }
    
                    else {
    
                    echo "There are no links for the criteria specified";
    
                    }
    
                    ?>
    PHP:

     
    crazyryan, Feb 3, 2007 IP
  2. maiahost

    maiahost Guest

    Messages:
    664
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #2
    when you put code (php var) in your mysql queries it should be like :
    
    select * from asd where type = '$type' 
    // just an example 
    
    //so your lines here : 
    $q .= " ORDER BY $sortby";
    
    $q .= " $order";
    
    $q .= " LIMIT $perpage ";
    
    //should be :
    
    $q .= " ORDER BY '$sortby' "; //you can add ASC or DESC at the end
    
    $q .= " '$order' ";
    
    $q .= " LIMIT '$perpage' ";
    
    
    PHP:
     
    maiahost, Feb 3, 2007 IP
  3. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Try this:

    require '/home/proxygd/public_html/proxies/config.php';
    
    $perpage = 10;
    
    $q = 'SELECT * FROM links WHERE confirmed = \'yes\'';
    
    if (!empty($_GET['type'])) {
       $q .= ' AND link_type = \''. addslashes($_GET['type']) .'\'';
    }
    
    if (!empty($_GET['sortby'])) {
       $q .= ' ORDER BY \''. addslashes($_GET['sortby']) .'\'';
    }
    
    if (!empty($_GET['order']) AND $_GET['order'] == 'DESC') {
       $q .= ' DESC';
    }
    
            $total = mysql_num_rows(mysql_query($q));								
    
    if (!empty($_GET['page'])) {
       if ($_GET['page'] == 1) {
    	    $start = 0;
    	 }
    	 else {
    	    $start = $perpage * ($_GET['page'] - 1);
    	 }
    	 $q .= ' LIMIT '. $start .', '. $perpage;
    }
    else {
       $q . = ' LIMIT '. $perpage;
    }
    
    $result = mysql_query($q) or die("Error in query " . mysql_error());
    
    [...]
    PHP:
    If that doesn't work, please post the error message here.
     
    Icheb, Feb 3, 2007 IP
  4. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #4
    Parse error: syntax error, unexpected '=' in /home/proxygd/public_html/index.php on line 137

    137 - $q . = ' LIMIT '. $perpage;
     
    crazyryan, Feb 3, 2007 IP
  5. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ah yeah, one space too much.

    Should be .= instead of . =
     
    Icheb, Feb 3, 2007 IP
  6. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #6
    OK - Well that works, but it doesn't list them newest first automatically.
     
    crazyryan, Feb 3, 2007 IP
  7. maiahost

    maiahost Guest

    Messages:
    664
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #7
    OK what's the name of the auto increment column
     
    maiahost, Feb 3, 2007 IP
  8. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Use this:

    if (!empty($_GET['sortby'])) {
       $q .= ' ORDER BY '. addslashes($_GET['sortby']);
    	 if (!empty($_GET['order']) AND $_GET['order'] == 'DESC') {
          $q .= ' DESC';
    	 }
    }
    else {
       $q .= ' ORDER BY link_id DESC';
    }
    PHP:
     
    Icheb, Feb 3, 2007 IP
  9. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #9
    Where do I place that?
     
    crazyryan, Feb 3, 2007 IP
  10. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Replace the existing if clause for $_GET['sortby'].
     
    Icheb, Feb 3, 2007 IP
  11. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #11
    OK that worked, however.

    /index.php?sortby=link_title&order=DESC doesn't, but ASC does.

    The DESC/ASC thing is the same for sortby=link_title, link_visitors and link_url.
     
    crazyryan, Feb 3, 2007 IP
  12. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Try this:

    if (!empty($_GET['sortby'])) {
       $q .= ' ORDER BY '. addslashes($_GET['sortby']);
       if (!empty($_GET['order'])) {
          $q .= ' '. addslashes($_GET['order']);
       }
    }
    else {
       $q .= ' ORDER BY link_id DESC';
    }
    PHP:
     
    Icheb, Feb 3, 2007 IP
  13. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #13
    Nope, that didn't work :(
     
    crazyryan, Feb 3, 2007 IP
  14. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Why don't you post an error message or the query? Would certainly make it easier.
     
    Icheb, Feb 3, 2007 IP
  15. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #15
    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/proxygd/public_html/index.php on line 131
    Error in query You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DESC LIMIT 10' at line 1

    When I visit index.php?sortby=link_visitors&order=DESC
     
    crazyryan, Feb 3, 2007 IP
  16. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Use echo $q; and post the output.
     
    Icheb, Feb 3, 2007 IP
  17. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #17
    When I'm on index.php it's:
    SELECT * FROM links WHERE confirmed = 'yes' ORDER BY link_id DESC LIMIT 10

    /index.php?sortby=link_visitors&order=ASC :
    SELECT * FROM links WHERE confirmed = 'yes' ORDER BY link_visitors ASC LIMIT 10
     
    crazyryan, Feb 3, 2007 IP
  18. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #18
    There is nothing wrong with that query from what I can see. Are you sure that's the correct one?
     
    Icheb, Feb 3, 2007 IP
  19. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #19
    Yeah, well, my code is below.

    Visit: http://www.proxy.gd/index.php?sortby=link_visitors&order=DESC and you'll see it doesn't work : (

    <?
    
    				// Include database
    
    require '/home/proxygd/public_html/proxies/config.php';
    
    $perpage = 10;
    
    $q = 'SELECT * FROM links WHERE confirmed = \'yes\'';
    
    if (!empty($_GET['type'])) {
       $q .= ' AND link_type = \''. addslashes($_GET['type']) .'\'';
    }
    
    if (!empty($_GET['sortby'])) {
       $q .= ' ORDER BY '. addslashes($_GET['sortby']);
       if (!empty($_GET['order'])) {
          $q .= ' '. addslashes($_GET['order']);
       }
    }
    else {
       $q .= ' ORDER BY link_id DESC';
    }
    
    if (!empty($_GET['order']) AND $_GET['order'] == 'DESC') {
       $q .= ' DESC';
    }
    
            $total = mysql_num_rows(mysql_query($q));                        
    
    if (!empty($_GET['page'])) {
       if ($_GET['page'] == 1) {
            $start = 0;
         }
         else {
            $start = $perpage * ($_GET['page'] - 1);
         }
         $q .= ' LIMIT '. $start .', '. $perpage;
    }
    else {
       $q .= ' LIMIT '. $perpage;
    }
    
    $result = mysql_query($q) or die("Error in query " . mysql_error());
    
    				$numb = mysql_num_rows($result);
    
    				if ($numb > 0) {
    
    				while ($row = mysql_fetch_assoc($result)) {
    
    				// THIS IS THE PART WHERE YOU CAN MODIFY THE LINK OUTPUT
    
    				// DO NOT MODIFY ANY THING OUTSIDE OF THE FOLLOWING 4 LINES OR YOU WILL BREAK SOMETHING
    
    				echo "<p class=\"box\"><a href=\"$siteurl/$row[link_id]/\">$row[link_title]</a><br />";
    
    				echo "Description: " . $row['link_description'] . "<br />";
    
    				echo "Visitors: " . $row['link_visitors'] . "<br />";
    
    				echo "Type: " . $row['link_type'];
    
    				echo "</p>";
    
    				}
    
    				if ($pp > 1) {
    
    				$ts = 0;
    
    				foreach ($_GET as $key => $value) {
    
    					if ($key == "page") { $value--; }
    
    					if ($ts == 0) {
    
    					$ns .= "?" . $key . "=" . $value;
    
    					$ts++;
    
    					}
    
    					else {
    
    					$ns .= "&" . $key . "=" . $value;
    
    					$ts++;
    
    					}
    
    					}
    
    					echo "<a href=\"view_proxies.php" . $ns . "\">PREVIOUS</a>";
    
    				}
    				// Fix for pagination.
    				// PP was set to 0, causing equations to evaluate wrong.
    				if (($perpage * $pp) == "0") { $pp = "1"; }
    				// Fix for the pagination part 2
    				unset($ns);
    				if ($total > ($perpage * $pp)) {
    					$ts = 0;
    						foreach ($_GET as $key => $value) {
    							if ($key == "page") { $value++; }
    							if ($ts == 0) {
    							$ns .= "?" . $key . "=" . $value;
    							$ts++;
                				}		
    							else {
    							$ns .= "&" . $key . "=" . $value;
    							$ts++;
    							}
    						}
    						if (empty($ns) and $total > ($perpage * $pp)) { echo "<a href=\"view_proxies.php?page=2\">NEXT</a>"; }
    						if (!empty($ns) and $total > ($perpage * $pp)) { echo "<a href=\"view_proxies.php" . $ns . "\">NEXT</a>"; }
    
    					}
    
    				}
    
    				else {
    
    				echo "There are no links for the criteria specified";
    
    				}
    
    				?>
    PHP:
     
    crazyryan, Feb 3, 2007 IP
  20. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #20
    Remove this:

    if (!empty($_GET['order']) AND $_GET['order'] == 'DESC') {
       $q .= ' DESC';
    }
    PHP:
    It's there twice.
     
    Icheb, Feb 3, 2007 IP