Solved

correcting a bug or two in a list sorter

Posted on 2004-09-23
20
256 Views
Last Modified: 2012-03-15
i have tried and tried but i cannot figure out whats wrong with this:

----------playerlist.php
<html>
<head>
</head>
<body bgcolor="#000000" text="#FFFFFF">
<?php

$self = "playerlist.php";
if (!$sortby) {
$sortby = "strength";
}

$query= "SELECT `username` , `password` , `email` , `gold` , `hp` , `strength` , `level` , `exp` , `pos` , `special` , `date` , `sentence` , `map` , `ca` , `weapondmg` , `initiative` , `land` , `ann1` , `ann2` , `ann3` , `totalhp` , `defmonst` FROM `users` WHERE 1 ORDER BY `strength` DESC";
$result = mysql_query($query, $db) or die ("SQL error in query '$query' : ".mysql_error());
$num_rows = mysql_numrows($result);




?>
<script language="JavaScript">

function openDir( form ) {

      var newIndex = jumpmenu.sortmenu.selectedIndex;

      if ( newIndex == 0 ) {

            alert( "Please select a location!" );

      } else {

            cururl = jumpmenu.sortmenu.options[ newIndex ].value;

            window.location.assign( cururl );

      }

}

</script>

<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" bordercolor="#FFFFFF">
  <tr>
    <td background="images/top.jpg"><img src="images/corner.jpg" width="10" height="10"></td>
    <td background="images/top.jpg" bgcolor="#000000"> <div align="center"> </div></td>
    <td background="images/top.jpg" bgcolor="#000000"><img src="images/corner.jpg" width="10" height="10"></td>
  </tr>
  <tr>
    <td width="1%" background="images/side.jpg" bgcolor="#000000"></td>
    <td width="620" height="280" valign="top" bgcolor="#000000"><p align="left"><img src="images/playlist.jpg" width="197" height="50"></p>
      <table width="293" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td width="131">Sorting Method:</td>
          <td width="162"><form name="jumpmenu" method="post" action="">
              <select name="sortmenu" onChange="openDir( this.sortmenu )">
                <option value="<?php echo "$self"."?"."sortby=strength"; ?>" selected>By Most Powerful</option>
                <option value="<?php echo "$self"."?"."sortby=gold"; ?>">By Most Gold</option>
                <option value="<?php echo "$self"."?"."sortby=level"; ?>">By Highest Level</option>
                <option value="<?php echo "$self"."?"."sortby=exp"; ?>">By Most XP</option>
              </select>
            </form></td>
        </tr>
      </table>
     
        <?php
include('connect.php');
while ($rankingfield=mysql_fetch_array($result))  {
$player_name = $rankingfield[0];
$player_gold = $rankingfield[3];
$player_strength = $rankingfield[5];
$player_exp = $rankingfield[7];
$player_lvl = $rankingfield[6];    
$date_log = $rankingfield[10];

switch ($sortby) {
      case "strength":
      $sorted_data = $player_strength;
      $sort_label ="Strength";
      break;
      case "gold":
      $sorted_data = $player_gold;
      $sort_label ="Gold";
      break;
      case "level":
      $sorted_data = $player_lvl;
      $sort_label ="Level";
      break;
      case "exp":
      $sorted_data = $player_exp;
      $sort_label ="Experience";
      break;
      }
      
?>
      <table width="593" border="0" align="left" cellpadding="0" cellspacing="1">
        <tr bgcolor="#B9CDD9">
          <th width="270" height="16" bgcolor="#000000"><div align="left"><font size="2" face="Geneva, Arial, Helvetica, sans-serif">Users</font></div></th>
          <th width="159" bgcolor="#000000"><font size="2" face="Arial, Helvetica, sans-serif"><?php echo "Sorted by: "."$sortlabel"; ?></font></th>
          <th width="218" bgcolor="#000000"><div align="left">Date last Logged
              in</div></th>
        </tr>
        <?php
            


      /*
echo <<<EOS
<tr>
  <td height=19 bgcolor="#000000">$player_name</td>
  <td height=19 bgcolor="#000000">$sorted_data</td>
  <td height=19 bgcolor="#000000">$date_log</td>
 </tr>
EOS;
*/
}

?>
          </table> </td>
    <td width="1%" background="images/side.jpg" bgcolor="#000000"></td>
  </tr>
  <tr>
    <td background="images/top.jpg"><img src="images/corner.jpg" width="10" height="10"></td>
    <td height="1%" background="images/top.jpg" bgcolor="#000000"></td>
    <td background="images/top.jpg" bgcolor="#000000"><img src="images/corner.jpg" width="10" height="10"></td>
  </tr>
</table>

0
Comment
Question by:bartonjo2
  • 7
  • 5
  • 4
  • +1
20 Comments
 
LVL 13

Expert Comment

by:cLFlaVA
ID: 12133963
What error are you getting?
0
 
LVL 3

Author Comment

by:bartonjo2
ID: 12134021
SQL error in query 'SELECT `username` , `password` , `email` , `gold` , `hp` , `strength` , `level` , `exp` , `pos` , `special` , `date` , `sentence` , `map` , `ca` , `weapondmg` , `initiative` , `land` , `ann1` , `ann2` , `ann3` , `totalhp` , `defmonst` FROM `users` WHERE 1 ORDER BY `strength` DESC' :
0
 
LVL 13

Expert Comment

by:cLFlaVA
ID: 12134057
What is the MySQL error you're getting?

Try this and tell me what you get:

$result = mysql_query($query, $db) or die (mysql_error());
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 13

Expert Comment

by:cLFlaVA
ID: 12134072
Try getting rid of "WHERE 1"
0
 
LVL 3

Author Comment

by:bartonjo2
ID: 12134117
it still does it

i did try this:
$result = mysql_query($query, $db) or die (mysql_error());
thats how i got the error message
0
 
LVL 13

Expert Comment

by:cLFlaVA
ID: 12134221
You didn't give me the mysql error message.  you gave me your custom error message.  you most likely have a column mis-named.  run that line of code as i have written it above, and tell me the actual mysql_error message that you get.
0
 
LVL 3

Author Comment

by:bartonjo2
ID: 12134299
the mysql statement is used was generated by phpmyadmin so i dont think the columsn are misnamed
0
 
LVL 13

Expert Comment

by:cLFlaVA
ID: 12134349
And you still haven't displayed the mysql_error() message!
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 12134846
You noticed you haven't got any mysql_connect or mysql_select_db statement in your code so it would be quite logical to have an error while querying a db you aren't connected to.

-r-
0
 
LVL 3

Author Comment

by:bartonjo2
ID: 12136107
ok i figured out the problem and i added some more code but why wont the page load, its just blank

-------------------------------------www.j-barton.com/antioch/playerlist.php

<html>
<head>
</head>
<body bgcolor="#000000" text="#FFFFFF">
<?php
include('connect.php');
$self = "playerlist.php";
if (!$sortby) {
$sortby = "strength";
}




?>
<script language="JavaScript">

function openDir( form ) {

      var newIndex = jumpmenu.sortmenu.selectedIndex;

      if ( newIndex == 0 ) {

            alert( "Please select a location!" );

      } else {

            cururl = jumpmenu.sortmenu.options[ newIndex ].value;

            window.location.assign( cururl );

      }

}

</script>

<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" bordercolor="#FFFFFF">
  <tr>
    <td background="images/top.jpg"><img src="images/corner.jpg" width="10" height="10"></td>
    <td background="images/top.jpg" bgcolor="#000000"> <div align="center"> </div></td>
    <td background="images/top.jpg" bgcolor="#000000"><img src="images/corner.jpg" width="10" height="10"></td>
  </tr>
  <tr>
    <td width="1%" background="images/side.jpg" bgcolor="#000000"></td>
    <td width="620" height="280" valign="top" bgcolor="#000000"><p align="left"><img src="images/playlist.jpg" width="197" height="50"></p>
      <table width="293" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td width="131">Sorting Method:</td>
          <td width="162"><form name="jumpmenu" method="post" action="">
              <select name="sortmenu" onChange="openDir( this.sortmenu )">
                <option value="<?php echo "$self"."?"."sortby=strength"; ?>" selected>By Most Powerful</option>
                <option value="<?php echo "$self"."?"."sortby=gold"; ?>">By Most Gold</option>
                <option value="<?php echo "$self"."?"."sortby=level"; ?>">By Highest Level</option>
                <option value="<?php echo "$self"."?"."sortby=exp"; ?>">By Most XP</option>
              </select>
            </form></td>
        </tr>
      </table>
     
        <?php


switch ($sortby) {
      case "strength":
      $sorted_data = $player_strength;
      $sort_label ="Strength";
      break;
      case "gold":
      $sorted_data = $player_gold;
      $sort_label ="Gold";
      break;
      case "level":
      $sorted_data = $player_lvl;
      $sort_label ="Level";
      break;
      case "exp":
      $sorted_data = $player_exp;
      $sort_label ="Experience";
      break;
      }
      
?>
      <table width="593" border="0" align="left" cellpadding="0" cellspacing="1">
        <tr bgcolor="#B9CDD9">
          <th width="270" height="16" bgcolor="#000000"><div align="left"><font size="2" face="Geneva, Arial, Helvetica, sans-serif">Users</font></div></th>
          <th width="159" bgcolor="#000000"><font size="2" face="Arial, Helvetica, sans-serif"><?php echo "Sorted by: "."$sortlabel"; ?></font></th>
          <th width="218" bgcolor="#000000"><div align="left">Date last Logged
              in</div></th>
        </tr>
        <?php
            $query= "SELECT username, strength, date, exp, level, gold FROM users ORDER BY `$sortby` DESC";
$result = mysql_query($query, $db) or die ("SQL error in query '$query' : ".mysql_error());
$num_rows = mysql_numrows($result);
while(list($player_name, $player_strength, $date_log, $player_exp, $player_lvl, $player_gold) = mysql_fetch_array($result)) {
      
      
}


      
echo <<<EOS
<tr>
  <td height=19 bgcolor="#000000">$player_name</td>
  <td height=19 bgcolor="#000000">$sorted_data</td>
  <td height=19 bgcolor="#000000">$date_log</td>
 </tr>
EOS;

}

?>
          </table> </td>
    <td width="1%" background="images/side.jpg" bgcolor="#000000"></td>
  </tr>
  <tr>
    <td background="images/top.jpg"><img src="images/corner.jpg" width="10" height="10"></td>
    <td height="1%" background="images/top.jpg" bgcolor="#000000"></td>
    <td background="images/top.jpg" bgcolor="#000000"><img src="images/corner.jpg" width="10" height="10"></td>
  </tr>
</table>



ive raised the points as this is a harder question
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 12136343
You should add the line below to the top of your code:
<?php error_reporting(E_ALL); ?>

There might be numerous problems for example

if (!$sortby) {
$sortby = "strength";
}

should/could be

if(!isset($_GET['sortby']))
  $sortby = 'strength';
else
  $sortby = $_GET['sortby'];

And there might be more problems popping to the surface.

-r-
0
 
LVL 5

Expert Comment

by:TheClickMaster
ID: 12137347
In this part:

===============

while(list($player_name, $player_strength, $date_log, $player_exp, $player_lvl, $player_gold) = mysql_fetch_array($result)) {
     
     
}


     
echo <<<EOS
<tr>
  <td height=19 bgcolor="#000000">$player_name</td>
  <td height=19 bgcolor="#000000">$sorted_data</td>
  <td height=19 bgcolor="#000000">$date_log</td>
 </tr>
EOS;

======================

THE echo seems to be OUTSIDE of the While Loop.

Try it like this

while(list($player_name, $player_strength, $date_log, $player_exp, $player_lvl, $player_gold) = mysql_fetch_array($result))
{    
echo <<<EOS
<tr>
  <td height=19 bgcolor="#000000">$player_name</td>
  <td height=19 bgcolor="#000000">$sorted_data</td>
  <td height=19 bgcolor="#000000">$date_log</td>
 </tr>
EOS;    
}
0
 
LVL 3

Author Comment

by:bartonjo2
ID: 12138626
still just a blank page, i tried the isset i tried the error reporting i tried moving the bracket and it still doesnt work, what could it be, im adding 10 points
0
 
LVL 5

Expert Comment

by:TheClickMaster
ID: 12138786
what is the value of mysql_numrows($result); ?
0
 
LVL 5

Expert Comment

by:TheClickMaster
ID: 12138815
try

while($Data = mysql_fetch_array($Result))
{
    echo '<tr>
             <td height=19 bgcolor="#000000">'.$Data["player_name"].'</td>
             <td height=19 bgcolor="#000000">'.$Data[$sorted_data].'</td>
             <td height=19 bgcolor="#000000">'.$Data["date_log"].'</td>
             </tr>';
}
0
 
LVL 3

Author Comment

by:bartonjo2
ID: 12138972
can somebody rewrite the thing, there must be some other bug were missing, may as try some other way
thanks
0
 
LVL 5

Expert Comment

by:TheClickMaster
ID: 12139212
since it still displays a white page I guess there is an error but it doesnt show up.

Try enabling error display by setting the following in your PHP.ini

display_errors = On

or

ini_set("display_errors","On");

in your script

0
 
LVL 49

Expert Comment

by:Roonaan
ID: 12140310
@TheClickMarser

That's exeactly the purpose of error_reporting(E_ALL);, but in my opinion the error_reporting function goes over the ini_set thingies, like every build in function goes over ini_set for that matter.

-r-
0
 
LVL 49

Accepted Solution

by:
Roonaan earned 215 total points
ID: 12140330
Try this one

Regards

-r-

<html>
<head>
</head>
<body bgcolor="#000000" text="#FFFFFF">
<?php
error_reporting(E_ALL);
include('connect.php');
$self = "playerlist.php";

$sortby = isset($_REQUEST['sortby']) ? $_REQUEST['sortby'] : "strength";





?>
<script language="JavaScript">

function openDir( form ) {

     var newIndex = jumpmenu.sortmenu.selectedIndex;

     if ( newIndex == 0 ) {

          alert( "Please select a location!" );

     } else {

          cururl = jumpmenu.sortmenu.options[ newIndex ].value;

          window.location.assign( cururl );

     }

}

</script>

<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" bordercolor="#FFFFFF">
  <tr>
    <td background="images/top.jpg"><img src="images/corner.jpg" width="10" height="10"></td>
    <td background="images/top.jpg" bgcolor="#000000"> <div align="center"> </div></td>
    <td background="images/top.jpg" bgcolor="#000000"><img src="images/corner.jpg" width="10" height="10"></td>
  </tr>
  <tr>
    <td width="1%" background="images/side.jpg" bgcolor="#000000"></td>
    <td width="620" height="280" valign="top" bgcolor="#000000"><p align="left"><img src="images/playlist.jpg" width="197" height="50"></p>
      <table width="293" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td width="131">Sorting Method:</td>
          <td width="162"><form name="jumpmenu" method="post" action="">
              <select name="sortmenu" onChange="openDir( this.sortmenu )">
                <option value="<?php echo "$self"."?"."sortby=strength"; ?>" <?php if($sortby=='strength') echo 'selected="selected"';?>>By Most Powerful</option>
                <option value="<?php echo "$self"."?"."sortby=gold"; ?>"     <?php if($sortby=='gold')     echo 'selected="selected"';?>>By Most Gold</option>
                <option value="<?php echo "$self"."?"."sortby=level"; ?>"    <?php if($sortby=='level')    echo 'selected="selected"';?>>By Highest Level</option>
                <option value="<?php echo "$self"."?"."sortby=exp"; ?>"      <?php if($sortby=='exp')      echo 'selected="selected"';?>>By Most XP</option>
              </select>
            </form></td>
        </tr>
      </table>
     
        <?php
       
switch ($sortby) {
     case "gold":
      $sorted_data = 'player_gold';
      $sort_label ="Gold";
     break;
     case "level":
      $sorted_data = 'player_lvl';
      $sort_label ="Level";
     break;
     case "exp":
      $sorted_data = 'player_exp';
      $sort_label ="Experience";
     break;
     case "strength":default:
      $sorted_data = 'player_strength';
      $sort_label ="Strength";
     break;
     }
?>
      <table width="593" border="0" align="left" cellpadding="0" cellspacing="1">
        <tr bgcolor="#B9CDD9">
          <th width="270" height="16" bgcolor="#000000"><div align="left"><font size="2" face="Geneva, Arial, Helvetica, sans-serif">Users</font></div></th>
          <th width="159" bgcolor="#000000"><font size="2" face="Arial, Helvetica, sans-serif">Sorted by: <?php echo $sort_label; ?></font></th>
          <th width="218" bgcolor="#000000"><div align="left">Date last Logged
              in</div></th>
        </tr>
        <?php
          $query= "SELECT username, strength, date, exp, level, gold FROM users ORDER BY `$sortby` DESC";
$result = mysql_query($query, $db) or die ("SQL error in query '$query' : ".mysql_error());
$num_rows = mysql_numrows($result);
while(list($player_name, $player_strength, $date_log, $player_exp, $player_lvl, $player_gold) = mysql_fetch_array($result)) {
     
echo '<tr>
<td height=19 bgcolor="#000000">'.$player_name.'</td>
<td height=19 bgcolor="#000000">'.${$sorted_data}.'</td>
<td height=19 bgcolor="#000000">'.$date_log.'</td>
</tr>';
     
}


     

?>
          </table> </td>
    <td width="1%" background="images/side.jpg" bgcolor="#000000"></td>
  </tr>
  <tr>
    <td background="images/top.jpg"><img src="images/corner.jpg" width="10" height="10"></td>
    <td height="1%" background="images/top.jpg" bgcolor="#000000"></td>
    <td background="images/top.jpg" bgcolor="#000000"><img src="images/corner.jpg" width="10" height="10"></td>
  </tr>
</table>
0
 
LVL 3

Author Comment

by:bartonjo2
ID: 12144537
thanks, it works great!!, only one problem I get an error when trying to select sort by most powerful in the jumpmenu, it only happens after you have already chosen one sorting method.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question