Best way to sort records

0

I'm trying to update my archaic and terribly coded php/mysql inventory system over to a Pods setup. What is the best way to accomplish this using pods and magic tags?

I want to create a dropdown on my page that lets users pick sort options and have the data reflect that. I'll paste the code that I used to use below. Just looking for a step in the right direction.

<script type="text/javascript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
//-->
</script>
<form name="form" id="form">
  <select name="jumpMenu" id="jumpMenu" onchange="MM_jumpMenu('parent',this,1)">
    <option value="#">Sort By</option>
    <option value="index.php?sorting=1">Year</option>
    <option value="index.php?sorting=2">Make</option>
    <option value="index.php?sorting=3">Miles</option>
    <option value="index.php?sorting=4">Down Payment</option>
    <option value="index.php?sorting=5">Price on Payment Plan</option>
    <option value="index.php?sorting=6">Cash Price</option>
  </select>
</form>

<?php
mysql_connect(left blank); 
mysql_select_db(left blank); 

switch($_GET['sorting']) {
                case 1:
        $result = mysql_query("select * from vehicles ORDER BY year DESC");
        break;
                case 2:
        $result = mysql_query("select * from vehicles ORDER BY make ASC");
        break;
            case 3:
        $result = mysql_query("select * from vehicles ORDER BY miles ASC");
        break;
            case 4:
        $result = mysql_query("select * from vehicles ORDER BY downpay ASC");
        break;
            case 5:
        $result = mysql_query("select * from vehicles ORDER BY pricepay ASC");
        break;
            case 6:
        $result = mysql_query("select * from vehicles ORDER BY pricecash ASC");
        break;
                default:
        $result = mysql_query("select * from vehicles");
        break;
}

asked Feb 13 at 8:18

poxin

20

add comment
enter at least 15 characters

4 Answers

1

It's much easier with Pods than it is with your old way.

You can leave your form the same and change your sort / display logic to something like this:

switch ( pods_url_variable ( 'sorting', 'get') ) {
    case 1:
      $orderby = 't.year DESC';
      break;
   ...
  default:
      $orderby = 't.id DESC';
      break;
}
$pods = new Pod('vehicles');
$pods->findRecords($orderby, -1);
echo $pods->showTemplate('vehicle_listing');

Your template will contain the html code with magic tags to display each line of data, something like

<div>
<p>Year: {@year}</p><p>Make: {@make}</p><p>Model: {@model}</p> ...
</div>

answered Feb 13 at 9:43

chris.pilko

889

edited Feb 13 at 10:57

add comment
enter at least 15 characters
0

Thank you Chris! I'm seems much simplier that way, although I am still having issues.

<?php
switch (pods_url_variable ('sort', 'get')) 
{
case 1:  
      $orderby = 't.year DESC';  
      break; 

case default:  
      $orderby = 't.date DESC';  
      break; 
} 

$pods = new Pod('car');  
$pods->findRecords($orderby, -1); 
echo $pods->showTemplate('car_list'); 
?>

It doesn't like the case default. Not sure what's up there. Took me a little bit to figure out 'swtich' had a typo in it too!

answered Feb 13 at 10:32

poxin

20

edited Feb 13 at 10:44

add comment
enter at least 15 characters
0

Change 'swtich' to 'switch' and change 'case default:' to 'default' and all will compile.

Coding would be so much easier if typos didn't matter...

answered Feb 13 at 10:45

chris.pilko

889

edited Feb 13 at 10:56

I just found that, thanks! – poxin Feb 13 at 10:47
add comment
enter at least 15 characters
0

Everything is working except when I sort by miles, and some other ones, the results seem to display semi randomly? At least I can't make sense why they are being displayed out of order. The rest of them work great!

Edit: Turns out they were txt fields, not number fields. I think it confused it. I'll try playing more before posting stupid comments! I apologize.

answered Feb 13 at 11:16

poxin

20

edited Feb 13 at 11:19

add comment
enter at least 15 characters