Solved

correcting a bug or two in a list sorter

Posted on 2004-09-23
20
254 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
Comment Utility
What error are you getting?
0
 
LVL 3

Author Comment

by:bartonjo2
Comment Utility
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
Comment Utility
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
 
LVL 13

Expert Comment

by:cLFlaVA
Comment Utility
Try getting rid of "WHERE 1"
0
 
LVL 3

Author Comment

by:bartonjo2
Comment Utility
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
Comment Utility
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
Comment Utility
the mysql statement is used was generated by phpmyadmin so i dont think the columsn are misnamed
0
 
LVL 13

Expert Comment

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

Expert Comment

by:Roonaan
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 49

Expert Comment

by:Roonaan
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
what is the value of mysql_numrows($result); ?
0
 
LVL 5

Expert Comment

by:TheClickMaster
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
@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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now