once a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
cheatboy00
Forum Contributor
Posts: 151
Joined: Sat Jun 29, 2002 10:36 am
Location: canada
Contact:

once a function

Post by cheatboy00 »

I'm having a problem with a function I created

i'm outputting all the items a character has... and i use a while loop to do it... wel.. heres what it looks like

Code: Select all

echo "<table>";
   echo "<tr><td colspan="2">";
      echo "TITLE";
   echo "</td></tr>";
   if ($this != 'that')&#123;
      $qry = mysql_query("SELECT * FROM characters WHERE pname != '$pname'"&#125;;
      while ($sen = mysql_fetch_assoc($qry))&#123;
         echo "<tr><td>";
            $name = $sen&#1111;'pname'];
            echo "$name";
         echo "</td>";
         echo "<td>";
               $e = $sen&#1111;'equip'];
               $ep = explode(",", $e);
               $it = $sen&#1111;'items'];
               $itm = explode(",", $it);
               items($ep,$itm);
         echo "</td></tr>";
      &#125;
   &#125; else &#123;
      echo "<tr><td colspan="2">";
         echo "sorry not allowed to view";
      echo "</td></tr>";
   &#125;
echo "</table>";
thats what it basicly looks like... the problem is is that it'll print out all the names. but it will only print out the items for the first character... and not the rest... if you need to see the fucntion just ask k.
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Few changes

Post by Takuma »

What does function "items()" do as it does not exist in PHP. And also $this variable is reserved so you cannot use that variable (It's like $_SESSION etc).
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Re: Few changes

Post by hob_goblin »

Takuma wrote:And also $this variable is reserved so you cannot use that variable (It's like $_SESSION etc).

Code: Select all

<?

$this = "foo";
echo $this;

?>
echoed foo... so don't give me 'you cannot use that'

also, cheatboy - post the items() function you showed me
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

ok, you can use them but you shouldn't use them.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

I agree with you Takuma. You would be inviting confusion and slop.

Now check out the below code.

Code: Select all

<?php

class tester
  &#123; var $this; &#125;
  
$tester = new tester;
$tester->this = foo;
echo $tester->this; echo "\n";
?>
Now the ridiculous thing about this is that it echoes "foo". Now I figured it would be reserved within the context of the object, but obviously it's not. That's crazy! I feel the language should enforce certain practices and this is one of them.

Stupid!

Later on,
BDKR
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

I think PHP should not allow to use variable "this". It can be confusing.
User avatar
cheatboy00
Forum Contributor
Posts: 151
Joined: Sat Jun 29, 2002 10:36 am
Location: canada
Contact:

Post by cheatboy00 »

k.. heres the function items()

Code: Select all

function items($equip, $items) &#123;
      $x = 0;
      while($equip&#1111;$x] != "")&#123;
         $ei = $equip&#1111;$x];
         echo "$ei(e), ";
         $x++;
      &#125;

      $i = 0;
      while($items&#1111;$i] != "")&#123;
         if ($items&#1111;$i] == 'Capsule Case') &#123;
            echo "Capsule Case ("";
            $i++;
            $m = $i;
            while ($items&#1111;$i] <= ($m+5))&#123;
               $cap = $items&#1111;$i];
               if ($items&#1111;$i] == ($m+5)) &#123;
                  echo "$cap"), ";
               &#125; else &#123;
                  echo "$cap, ";
                  $i++;
               &#125;
            &#125;
         &#125; else &#123;
            $it = $items&#1111;$i];
            echo "$it, ";
         &#125;
      $i++;
      &#125;
   &#125;
please note that this function is in a totally other file... that gets included before loading the output...

also that first thing was an example and there really isnt a variable called $this in my code. see teh actual code looks like that its just had more tr's and td's, and all that is needed for you helping is those 2 columns
User avatar
cheatboy00
Forum Contributor
Posts: 151
Joined: Sat Jun 29, 2002 10:36 am
Location: canada
Contact:

Post by cheatboy00 »

I'm bumping thios up cause i really need help on this... and i cant finish my site without it...
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Code: Select all

function items($equip, $items) &#123; 

      foreach($items as $ei);
         echo $ei."(e), ";
      &#125; 

      
      for($i = 0; $i < count($items); $i++)&#123; 
         if ($items&#1111;$i] == 'Capsule Case') &#123; 
            echo "Capsule Case ("";
            $m = $i; 
            $n = 0;
            while($n <= ($m+5))&#123; 
               $cap = $items&#1111;$n]; 
               if ($n == ($m+5)) &#123; 
                  echo $cap.""), "; 
               $n++;
               &#125; else &#123; 
                  echo $cap.", ";
               $n++;
               &#125; 
            &#125; 
         &#125; else &#123; 
            $it = $items&#1111;$i]; 
            echo $it.", "; 
         &#125; 
      &#125; 
   &#125;
User avatar
cheatboy00
Forum Contributor
Posts: 151
Joined: Sat Jun 29, 2002 10:36 am
Location: canada
Contact:

Post by cheatboy00 »

nope... that didnt work......

but i did find out what was wrong....

see in this table their are certian stats that your not allowed to view unless you have something ... well what i did is i put

Code: Select all

if ($itm = 'y')&#123;
   echo "<td>";
   $e = $sen&#1111;'equiped'];
   $ep = explode(", ", $e);
   $it = $sen&#1111;'items'];
   $itm = explode(", ", $it);
   items($ep,$itm);
   echo "</td>";
&#125;
see $itm was set above it. item would be set to 'y' if the character met its conditions... well do you see the problem... since i reset $itm to the value of whatevers in the db... it doest equal 'y' anymore so no more out putting items after the first go around...

well teh answer is obivous change the var.. i did to $it2
Post Reply