Jump to content

multiple querys..to view by Columns in DB...


shane85

Recommended Posts

hey guys

 

question - right now on my page I have it set up to pull info from my db and display the info from my prospect_id column in DESC order. the code I am using is

 

$prospectEntries = mysql_query('SELECT * FROM prospects ORDER BY prospect_id DESC ' . $queryLimit) or trigger_error(mysql_error());

while ($prospectEntry = mysql_fetch_array($prospectEntries))
{
print_prospectEntry($prospectEntry);
}

 

At the top of my page, I have links:

 

View By:

Contact Next

Date Added

Company Name

City

 

now, I know I could achieve this by adjusting my query to view by date_added, company_name etc instead of prospect_id, However, how do I make it so I can have multiple querys in my code so that initially the page displays the way it currently is, by prospect_id, however if someone where to click the link "contact next" it would display by contact_next istead of prospect_id....same for the other links.

 

sorry for the newb quetson - thnx in advance

Link to comment
https://forums.phpfreaks.com/topic/197392-multiple-querysto-view-by-columns-in-db/
Share on other sites

page.php?orderby=prospect 

contact, date, company, city etc..

then your sql

switch($_GET['orderby'])
{
       case "contact": 
        $orderby="contact_next field"; 
      break; 
     
      case "date": 
     $orderby="date field"; 
     break; 
       
      case "company": 
       $orderby="company field"; 
       break; 

       case "city"; 
       $orderby="city field"; 
       break; 

       case default: 
        $orderby="prospect_id"; 
        break; 
       
}
$sql="select * from prospects ORDER BY ".mysql_real_escape_string($orderby)." DWSC '.$queryLimit; 

I commented out that line and adjusted the code to this but doesnt work...what is wrong???

 

switch($_GET['orderby'])
{
case "contact":
$orderby="$arrEntry[contact_next]";
break;
case "date":
$orderby="$arrEntry[tstamp]";
break;
case "company":
$orderby="$arrEntry[company_name]";
break;
case "city";
$orderby="$arrEntry[city]";
break;
//case default:
//$orderby="prospect_id";
//break;
}

// Prints all records from Prospect ID

//$prospectEntries = mysql_query('SELECT * FROM prospects ORDER BY prospect_id DESC ' . $queryLimit) or trigger_error(mysql_error());
$prospectEntries = mysql_query('SELECT * from prospects ORDER BY ".mysql_real_escape_string($orderby)." DESC '.$queryLimit) or trigger_error(mysql_error());

I commented out that line and adjusted the code to this but doesnt work...what is wrong???

 

switch($_GET['orderby'])
{
case "contact":
$orderby="$arrEntry[contact_next]";
break;
case "date":
$orderby="$arrEntry[tstamp]";
break;
case "company":
$orderby="$arrEntry[company_name]";
break;
case "city";
$orderby="$arrEntry[city]";
break;
//case default:
//$orderby="prospect_id";
//break;
}

// Prints all records from Prospect ID

//$prospectEntries = mysql_query('SELECT * FROM prospects ORDER BY prospect_id DESC ' . $queryLimit) or trigger_error(mysql_error());
$prospectEntries = mysql_query('SELECT * from prospects ORDER BY ".mysql_real_escape_string($orderby)." DESC '.$queryLimit) or trigger_error(mysql_error());

 

Change the switch to:

switch($_GET['orderby'])
{
case "contact":
$orderby="$arrEntry[contact_next]";
break;
case "date":
$orderby="$arrEntry[tstamp]";
break;
case "company":
$orderby="$arrEntry[company_name]";
break;
case "city";
$orderby="$arrEntry[city]";
break;
default:
$orderby="prospect_id";
break;
}

and you're mixing up your single and double quotes

$prospectEntries = mysql_query('SELECT * from prospects ORDER BY ".mysql_real_escape_string($orderby)." DESC '.$queryLimit) or trigger_error(mysql_error());

should be

$prospectEntries = mysql_query("SELECT * from prospects ORDER BY ".mysql_real_escape_string($orderby)." DESC ".$queryLimit) or trigger_error(mysql_error());

 

I commented out that line and adjusted the code to this but doesnt work...what is wrong???

 

switch($_GET['orderby'])
{
case "contact":
$orderby="$arrEntry[contact_next]";
break;
case "date":
$orderby="$arrEntry[tstamp]";
break;
case "company":
$orderby="$arrEntry[company_name]";
break;
case "city";
$orderby="$arrEntry[city]";
break;
//case default:
//$orderby="prospect_id";
//break;
}

// Prints all records from Prospect ID

//$prospectEntries = mysql_query('SELECT * FROM prospects ORDER BY prospect_id DESC ' . $queryLimit) or trigger_error(mysql_error());
$prospectEntries = mysql_query('SELECT * from prospects ORDER BY ".mysql_real_escape_string($orderby)." DESC '.$queryLimit) or trigger_error(mysql_error());

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.