PHP Help

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
Hey guys,

Really didn't want to do this... but i'm really confused. xD

I think i went too in-depth to this.

Heres my problem.

I'm trying to make personal Highscores for a RuneScape private server that is SQL/IPB integrated.

and, i got all the data I need, but I have the levels in experience not in the levels.

I have the function and stuff to do it, just can't figure out how to implement it.

Here it is.

Also, if you could help me try to do this automaticly, like put in a loop or something, that would be great.

PHP:
<style type="text/css">
table {
width: 50%;
}
</style>
<?php
$c1 = mysql_connect("localhost", "*****", "*******");
mysql_select_db("cali_3", $c1);
if (isset ($_GET['name']))
 {
$name = $_GET['name'];
$result = mysql_query("SELECT * FROM ibf_members WHERE name='$name'") or die(mysql_error());

while($row = mysql_fetch_array($result))
  {
 function experience($L) {
  $a=0;
  for($x=1; $x<$L; $x++) {
   $a += floor($x+300*pow(2, ($x/7)));
  }
  return floor($a/4);
 }

if($playerxp >= 13034430) //fix
    $playerLevel = 99;

  echo "<a href=#search><h3><center><u>Search again?</u></center></h3></a><br /><br />";
  echo "<center><table border=1>";
  $stats = array("attack", "defence", "strength", "hitpoints", "range", "prayer", "magic", "cooking","woodcutting","fletching","fishing","firemaking","crafting","smithing","mining","herblore","agility","thieving","slayer","farming","runecrafting");

  echo "<tr>";
  echo "<td><strong>" . $row['name'] . "</strong></td>";
  echo "<td>Level</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td>Attack</td>";
  echo "<td>" . $row['attack'] . "</td>";
  echo "<td>" . $L['$stats'] . "</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td>Defence</td>";
  echo "<td>" . $row['defence'] . "</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td>Strength</td>";
  echo "<td>" . $row['strength'] . "</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td>Hitpoints</td>";
  echo "<td>" . $row['hitpoints'] . "</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td>Ranged</td>";
  echo "<td>" . $row['range'] . "</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td>Prayer</td>";
  echo "<td>" . $row['prayer'] . "</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td>Magic</td>";
  echo "<td>" . $row['magic'] . "</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td>Cooking</td>";
  echo "<td>" . $row['cooking'] . "</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td>Woodcutting</td>";
  echo "<td>" . $row['woodcutting'] . "</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td>Fletching</td>";
  echo "<td>" . $row['fletching'] . "</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td>Fishing</td>";
  echo "<td>" . $row['fishing'] . "</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td>Firemaking</td>";
  echo "<td>" . $row['firemaking'] . "</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td>Crafting</td>";
  echo "<td>" . $row['crafting'] . "</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td>Smithing</td>";
  echo "<td>" . $row['smithing'] . "</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td>Mining</td>";
  echo "<td>" . $row['mining'] . "</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td>Herblore</td>";
  echo "<td>" . $row['herblore'] . "</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td>Agility</td>";
  echo "<td>" . $row['agility'] . "</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td>Thieving</td>";
  echo "<td>" . $row['thieving'] . "</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td>Slayer</td>";
  echo "<td>" . $row['slayer'] . "</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td>Farming</td>";
  echo "<td>" . $row['farming'] . "</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td>RuneCrafting</td>";
  echo "<td>" . $row['runecrafting'] . "</td>";
  echo "</tr>";
  }
echo "</table></center>";
mysql_close($c1);
}
else if (!isset ($_GET['name'])) {
echo "Welcome to the CaliScape Sig Shop!";
echo "<br />";
echo "Here you can make a custom signiture showing your CaliScape Stats!";
echo "<br />Enjoy!<br />";
}
?>
<center><a name="search"></a>
<form name="psearch" type="get" action="index.php">
<input type="hidden" name="code" value="Sigs" />
<input type="text" name="name" /><br />
<input type="submit" value="Search" />
</form></center>

Thanks in advance,

--Neil
 
Last edited:

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
try this:
PHP:
<?php 
$c1 = mysql_connect("localhost", "*****", "*******"); 
mysql_select_db("cali_3", $c1); 

function experience($L) { 
  $a=0
  for($x=1; $x<$L; $x++)
    $a += floor($x+300*pow(2, ($x/7))); 

  return floor($a/4); 
} 

if (!empty($_GET['name'])) // !empty is like isset, but also checks to see if there's a value w/ it ;)
{ 
  $name = $_GET['name']; 
  $result = mysql_query("SELECT * FROM ibf_members WHERE name='$name'") or die(mysql_error()); 

  while($row = mysql_fetch_array($result)) 
  { 
    if($playerxp >= 13034430) //fix 
      $playerLevel = 99; 

    echo "<a href=#search><h3><center><u>Search again?</u></center></h3></a><br /><br />"; 
    echo "<center><table border=1>"; 
    $stats = array("attack", "defence", "strength", "hitpoints", "range", "prayer", "magic", "cooking","woodcutting","fletching","fishing","firemaking","crafting","smithing","mining","herblore","agility","thieving","slayer","farming","runecrafting"); 

    echo "<tr>"; 
    echo "<td><strong>" . $row['name'] . "</strong></td>"; 
    echo "<td>Level</td>"; 
    echo "</tr>"; 
    echo "<tr>"; 
    echo "<td>Attack</td>"; 
    echo "<td>" . $row['attack'] . "</td>"; 
    echo "<td>" . experience($row['$stats']) . "</td>"; 
    echo "</tr>"; 
    echo "<tr>"; 
    echo "<td>Defence</td>"; 
    echo "<td>" . $row['defence'] . "</td>"; 
    echo "</tr>"; 
    echo "<tr>"; 
    echo "<td>Strength</td>"; 
    echo "<td>" . $row['strength'] . "</td>"; 
    echo "</tr>"; 
    echo "<tr>"; 
    echo "<td>Hitpoints</td>"; 
    echo "<td>" . $row['hitpoints'] . "</td>"; 
    echo "</tr>"; 
    echo "<tr>"; 
    echo "<td>Ranged</td>"; 
    echo "<td>" . $row['range'] . "</td>"; 
    echo "</tr>"; 
    echo "<tr>"; 
    echo "<td>Prayer</td>"; 
    echo "<td>" . $row['prayer'] . "</td>"; 
    echo "</tr>"; 
    echo "<tr>"; 
    echo "<td>Magic</td>"; 
    echo "<td>" . $row['magic'] . "</td>"; 
    echo "</tr>"; 
    echo "<tr>"; 
    echo "<td>Cooking</td>"; 
    echo "<td>" . $row['cooking'] . "</td>"; 
    echo "</tr>"; 
    echo "<tr>"; 
    echo "<td>Woodcutting</td>"; 
    echo "<td>" . $row['woodcutting'] . "</td>"; 
    echo "</tr>"; 
    echo "<tr>"; 
    echo "<td>Fletching</td>"; 
    echo "<td>" . $row['fletching'] . "</td>"; 
    echo "</tr>"; 
    echo "<tr>"; 
    echo "<td>Fishing</td>"; 
    echo "<td>" . $row['fishing'] . "</td>"; 
    echo "</tr>"; 
    echo "<tr>"; 
    echo "<td>Firemaking</td>"; 
    echo "<td>" . $row['firemaking'] . "</td>"; 
    echo "</tr>"; 
    echo "<tr>"; 
    echo "<td>Crafting</td>"; 
    echo "<td>" . $row['crafting'] . "</td>"; 
    echo "</tr>"; 
    echo "<tr>"; 
    echo "<td>Smithing</td>"; 
    echo "<td>" . $row['smithing'] . "</td>"; 
    echo "</tr>"; 
    echo "<tr>"; 
    echo "<td>Mining</td>"; 
    echo "<td>" . $row['mining'] . "</td>"; 
    echo "</tr>"; 
    echo "<tr>"; 
    echo "<td>Herblore</td>"; 
    echo "<td>" . $row['herblore'] . "</td>"; 
    echo "</tr>"; 
    echo "<tr>"; 
    echo "<td>Agility</td>"; 
    echo "<td>" . $row['agility'] . "</td>"; 
    echo "</tr>"; 
    echo "<tr>"; 
    echo "<td>Thieving</td>"; 
    echo "<td>" . $row['thieving'] . "</td>"; 
    echo "</tr>"; 
    echo "<tr>"; 
    echo "<td>Slayer</td>"; 
    echo "<td>" . $row['slayer'] . "</td>"; 
    echo "</tr>"; 
    echo "<tr>"; 
    echo "<td>Farming</td>"; 
    echo "<td>" . $row['farming'] . "</td>"; 
    echo "</tr>"; 
    echo "<tr>"; 
    echo "<td>RuneCrafting</td>"; 
    echo "<td>" . $row['runecrafting'] . "</td>"; 
    echo "</tr>"; 
  } 
  echo "</table></center>"; 
  mysql_close($c1); 
} 
else
{
  echo "Welcome to the CaliScape Sig Shop!"; 
  echo "<br />"; 
  echo "Here you can make a custom signiture showing your CaliScape Stats!"; 
  echo "<br />Enjoy!<br />"; 
} 
?>
you also don't need all those echo commands. It just creates useless overhead. an echo command can span over several lines, and a new line in the command will result in a new line in the source code.
 
Last edited:

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
i know. i'm just doin a rough copy.

but.

yeah. it doesnt work. takes too long to process. IPB gives me a timeout error
Edit:
must be the function.
 
Last edited:

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
try debugging the function. for instance, do <?php echo experience(5) ?> or whatever you would use for $L. If it's the loop or w/e, you'll find out there ;)

other than that, as far as digging deep, as a few other members will say, it's kinda hard for me to get involved without having code to work with and test live, as I do numerous debugging options, which really only come to me when I'm testing it :-/
 
Last edited:

xmakina

New Member
Messages
264
Reaction score
0
Points
0
If you're getting a "takes too long" error, try putting LIMIT 0, 10 at the end of your SQL. That will only pull the first 10 entries. I don't know how big your userbase is so this may not be the problem but it's a thought.
 

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
I got it.

cept i gotta fix how it works. :p

i made it loop using foreach()

thanks. might post back
Edit:
kk..

i've got it working with the loop. however the function is outputting 0 as the result. any help?

PHP:
function experience($L) { 
  $a=0
  for($x=1; $x<$L; $x++)
    $a += floor($x+300*pow(2, ($x/7))); 

  return floor($a/4); 
}
Edit:
bump
 
Last edited:

mattura

Member
Messages
570
Reaction score
2
Points
18
That function seems to work... are you sure of your value for $L? is it definitely getting to the function?
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
I got it.

cept i gotta fix how it works. :p

i made it loop using foreach()

thanks. might post back
Edit:
kk..

i've got it working with the loop. however the function is outputting 0 as the result. any help?

PHP:
function experience($L) { 
  $a=0
  for($x=1; $x<$L; $x++)
    $a += floor($x+300*pow(2, ($x/7))); 

  return floor($a/4); 
}
Edit:
bump

lol @ u, j/k. u forgot the semicolon at the end of $a=0. it now works on my end and returns 388 for $L=5
 

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
its giving me 0

http://www.caliscape.com/forum/?code=Sigs&name=Mod+Neil

and yeah, i know. I have the semicolon.

Ok, here is my script at current.

PHP:
<?php 
$c1 = mysql_connect("localhost", "****", "*******"); 
mysql_select_db("cali_3", $c1); 


function onoes($L) { 
  $a=0;
  for($x=1; $x<$L; $x++) 
    $a += floor($x+300*pow(2, ($x/7))); 
  return floor($a/4); 
} 


if (!empty($_GET['name'])) // !empty is like isset, but also checks to see if there's a value w/ it ;)
{ 
  $name = $_GET['name']; 

  $result = mysql_query("SELECT * FROM ibf_members WHERE name='$name'") or die(mysql_error()); 

  while($row = mysql_fetch_array($result)) 
  { 
    if($playerxp >= 13034430) //fix 
      $playerLevel = 99; 

    echo "<a href=#search><h3><center><u>Search again?</u></center></h3></a><br /><br />"; 
    echo "<center><table border=1>"; 
    $stats = array("attack", "defence", "strength", "hitpoints", "range", "prayer", "magic", "cooking","woodcutting","fletching","fishing","firemaking","crafting","smithing","mining","herblore","agility","thieving","slayer","farming","runecrafting"); 
    
        echo "<tr>"; 
    echo "<td><strong>" . $row['name'] . "</strong></td>"; 
    echo "<td>Level</td>"; 
    echo "<td>Experience</td>";
    echo "</tr>"; 

 function experience($skill) { 
  $a=0;
  for($x=1; $x<$skill; $x++) 
    $a += floor($x+300*pow(2, ($x/7))); 
  return floor($a/4); 
}
    
    foreach ($stats as $skill) {




  echo        "<tr>
      <td>" . $skill . "</td>
 <td>" . experience($row['$skill']) . "</td>
<td>" . $row['$skill'] . "</td>
</tr>";

    }
          
    
  echo "</table></center>"; 
  
  mysql_close($c1); 

    }

  
  } 

 
else
{
  echo "Welcome to the CaliScape Sig Shop!"; 
  echo "<br />"; 
  echo "Here you can make a custom signiture showing your CaliScape Stats!"; 
  echo "<br />Enjoy!<br />"; 
} 
?>

<center><a name="search"></a> 
<form name="psearch" type="get" action="index.php"> 
<input type="hidden" name="code" value="Sigs" /> 
<input type="text" name="name" /><br /> 
<input type="submit" value="Search" /> 
</form></center>

(Ignore the function at the top)

I get no errors, however:

1. The experience function outputs 0.
2. It will not let me use the variable $skill again in that statement. it just gives me an empty column.

Any ideas?
 
Last edited:

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
wow, looking at this, as i was about to post a big block of code, it dawned on me... $row['$skill'] has to be $row[$skill]. the apostrophe renders the variable's row useless.

so instead of the row virtually becomming $row['attack'], it stays as $row['$skill'].

http://php.net/echo
http://php.net/language.variables

try it out:
PHP:
<?php
$a = 'Hello World';
echo $a; // Should return Hello World
echo '$a'; // Should return $a
echo "$a"; // Should return Hello World
?>
 
Last edited:

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
i changed it. and now i'm getting an execution timeout from IPB.

I think its the function. can someone debug it for me
?
 

mephis

New Member
Messages
39
Reaction score
0
Points
0
how about adding some echo's at certain points in the code and see where it stops echoing? then you know where the error is located
 

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
i know where its located.

its at where it trys to grab the data from SQL and do the experience function on it
 

mephis

New Member
Messages
39
Reaction score
0
Points
0
ok... when you define the array $stats it's creating an array like this:

[0]=>"attack",
[1]=>"defense",
(...)

so instead of:
PHP:
foreach ($stats as $skill) {
try:
PHP:
foreach ($stats as $i=>$skill) {
 

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
...

you can't. you cant do
PHP:
foreach ($stats as $i>=$skill) {

it parses to the second variable. not the third.

and anyways. its this line that takes too long to proccess

PHP:
    $a += floor($x+300*pow(2, ($x/7)));
 
Last edited:

mephis

New Member
Messages
39
Reaction score
0
Points
0
you can't. you cant do

I beg to differ...
PHP:
$stats = array("attack", "defence", "strength", "hitpoints", "range");
foreach ($stats as $index=>$value) {
	echo " index ($index) = value ($value)<br>";
}
outputs:
Code:
index (0) = value (attack)
index (1) = value (defence)
index (2) = value (strength)
index (3) = value (hitpoints)
index (4) = value (range)

but that is going a bit off-topic, because that part of your code also works... (my bad)
anyway...


I tested the function like this:
PHP:
function experience($L) {
  $a=0;
  for($x=1; $x<$L; $x++)
    $a += floor($x+300*pow(2, ($x/7)));

  return floor($a/4);
}
for ($i=1;$i<1000;$i++) {
	echo "level ($i) = experience(".experience($i).")<br>";
}
without problems, and it only took only ~0.8s to execute
I don't think your problem lies in that function, must be elsewhere...
why don't you try outputting the result you get from the database query instead of processing it?
print_r() usually works for me
 

xmakina

New Member
Messages
264
Reaction score
0
Points
0
It strikes me as odd that *nowhere* does the word INF appear in your scripts, yet that is what is shown on Aarons attack level. Your problem probably exists around there.

Here's a thought: at the line
PHP:
$a += floor($x+300*pow(2, ($x/7)));
add a check to ensure $x > 0 as this is the only place an infinity could occur (divide by 0 error)
 

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
inf is correct. inf is his stats. he has infinates. xD

does mine work? lol
 
Top