Problems with sortby

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nancy

    Problems with sortby

    Having trouble getting this script to work. Errorr states query failed
    when click on sections to sort.

    $s = $HTTP_GET_VARS["sort"];
    include("deskhe ader.php");

    include("config .php");


    function loadlist($s) {
    Global $dbhost, $dbname, $dbuser, $dbpasswd;

    switch ($s) {
    case "title" :
    $sortby = "title";
    break;
    case "author" :
    $sortby = "author";
    break;
    case "section" :
    $sortby = "section";
    break;
    case "category" :
    $sortby = "category";
    break;
    case "subcat" :
    $sortby = "subcat";
    break;
    case "division" :
    $sortby = "division";
    break;
    case "subdiv" :
    $sortby = "subdiv";
    break;
    default:
    $sortby = "pageid";
    }

    $dbh = mysql_connect($ dbhost, $dbuser, $dbpasswd) or die("Could
    not connect");
    mysql_select_db ($dbname,$dbh) or die("Could not select database");
    $q1 = "SELECT page.pageid, page.title, author.name,
    section.name, category.name,
    subcat.name, division.name,
    subdiv.name
    FROM page
    INNER JOIN (author, section, category, subcat,
    division, subdiv)
    ON (author.authori d = page.authorid AND
    section.section id = page.sectionid AND
    category.catego ryid = page.categoryid AND
    subcat.subcatid = page.subcatid AND
    division.divisi onid = page.divisionid AND
    subdiv.subdivid = page.subdivid)
    ORDER BY $sortby";
    $rslt = mysql_query($q1 , $dbh) or die("Query failed");

    echo "<table><tr ><th colspan=9>Artic le List</th></tr>";
    echo "<tr><th><a href=".$_SERVER['SCRIPT_NAME'].">Seq</a></th><th><a
    href=".$_SERVER['SCRIPT_NAME']."?sort=name>Na me</a></th>
    <th><a href=".$_SERVER['SCRIPT_NAME']."?sort=title>t itle</a></th>
    <th><a
    href=".$_SERVER['SCRIPT_NAME']."?sort=author> author</a></th>
    <th><a
    href=".$_SERVER['SCRIPT_NAME']."?sort=section >section</a></th>
    <th><a
    href=".$_SERVER['SCRIPT_NAME']."?sort=categor y>category</a></th>
    <th><a
    href=".$_SERVER['SCRIPT_NAME']."?sort=subcat> subcat</a></th>
    <th><a
    href=".$_SERVER['SCRIPT_NAME']."?sort=divisio n>division</a></th>
    <th><a
    href=".$_SERVER['SCRIPT_NAME']."?sort=subdiv> subdiv</a></th>
    <th></th></tr>\n";
    while ($row1 = mysql_fetch_arr ay($rslt, MYSQL_ASSOC)) {
    echo "\t<tr>\n";

  • Michael Cooney

    #2
    Re: Problems with sortby

    $sortby should be the exact column name. eg: 'author.name', not just
    'author'

    Nancy wrote:
    Having trouble getting this script to work. Errorr states query failed
    when click on sections to sort.
    >
    $s = $HTTP_GET_VARS["sort"];
    include("deskhe ader.php");
    >
    include("config .php");
    >
    >
    function loadlist($s) {
    Global $dbhost, $dbname, $dbuser, $dbpasswd;
    >
    switch ($s) {
    case "title" :
    $sortby = "title";
    break;
    case "author" :
    $sortby = "author";
    break;
    case "section" :
    $sortby = "section";
    break;
    case "category" :
    $sortby = "category";
    break;
    case "subcat" :
    $sortby = "subcat";
    break;
    case "division" :
    $sortby = "division";
    break;
    case "subdiv" :
    $sortby = "subdiv";
    break;
    default:
    $sortby = "pageid";
    }
    >
    $dbh = mysql_connect($ dbhost, $dbuser, $dbpasswd) or die("Could
    not connect");
    mysql_select_db ($dbname,$dbh) or die("Could not select database");
    $q1 = "SELECT page.pageid, page.title, author.name,
    section.name, category.name,
    subcat.name, division.name,
    subdiv.name
    FROM page
    INNER JOIN (author, section, category, subcat,
    division, subdiv)
    ON (author.authori d = page.authorid AND
    section.section id = page.sectionid AND
    category.catego ryid = page.categoryid AND
    subcat.subcatid = page.subcatid AND
    division.divisi onid = page.divisionid AND
    subdiv.subdivid = page.subdivid)
    ORDER BY $sortby";
    $rslt = mysql_query($q1 , $dbh) or die("Query failed");
    >
    echo "<table><tr ><th colspan=9>Artic le List</th></tr>";
    echo "<tr><th><a href=".$_SERVER['SCRIPT_NAME'].">Seq</a></th><th><a
    href=".$_SERVER['SCRIPT_NAME']."?sort=name>Na me</a></th>
    <th><a href=".$_SERVER['SCRIPT_NAME']."?sort=title>t itle</a></th>
    <th><a
    href=".$_SERVER['SCRIPT_NAME']."?sort=author> author</a></th>
    <th><a
    href=".$_SERVER['SCRIPT_NAME']."?sort=section >section</a></th>
    <th><a
    href=".$_SERVER['SCRIPT_NAME']."?sort=categor y>category</a></th>
    <th><a
    href=".$_SERVER['SCRIPT_NAME']."?sort=subcat> subcat</a></th>
    <th><a
    href=".$_SERVER['SCRIPT_NAME']."?sort=divisio n>division</a></th>
    <th><a
    href=".$_SERVER['SCRIPT_NAME']."?sort=subdiv> subdiv</a></th>
    <th></th></tr>\n";
    while ($row1 = mysql_fetch_arr ay($rslt, MYSQL_ASSOC)) {
    echo "\t<tr>\n";

    Comment

    Working...