Item mall V2

This is guide mostly for website tools and code, for example CMS or registration scripts.
There can also be tools.

Moderators: osRose dev team, ospRose dev team, osiRose dev team, Moderators

Rate this script

0-2
8
28%
3-4
0
No votes
5-6
2
7%
7-8
3
10%
9-10
16
55%
 
Total votes : 29

Re: Item mall V2

Postby lmame on Thu Apr 15, 2010 11:07 am

There are weird things in your code...

Why is the owner hardcoded?
What is $boolres? It's not defined anywhere in the code you posted and with in $sql?

Now the main problem:
The mysql_fetch_array only returns record by record (so one at a time), not the whole array so you won't have all the slotnum the way you do your thing...
The world is full of love and peace ^_^
Image
User avatar
lmame
Admin
Admin
 
Posts: 8997
Joined: Mon Aug 06, 2007 4:42 pm
Location: July City

Re: Item mall V2

Postby Rifke on Thu Apr 15, 2010 11:09 am

If you take it over you have to do it right.


First of all, you execute the command. Than you set variables to your SQL command (sprintf) which doesn't make any sence.

Just change your return value from return $slotnum; to return $x; because the $x gets decreased, not $slotnum according to the code.
Rifke
Pero pero
Pero pero
 
Posts: 719
Joined: Thu Aug 09, 2007 3:01 pm
Location: Belgium

Re: Item mall V2

Postby lmame on Thu Apr 15, 2010 11:11 am

Rifke wrote:If you take it over you have to do it right.


First of all, you execute the command. Than you set variables to your SQL command (sprintf) which doesn't make any sence.

Just change your return value from return $slotnum; to return $x; because the $x gets decreased, not $slotnum according to the code.


Yep, that and the wrong use of mysql_fetch_array.
The world is full of love and peace ^_^
Image
User avatar
lmame
Admin
Admin
 
Posts: 8997
Joined: Mon Aug 06, 2007 4:42 pm
Location: July City

Re: Item mall V2

Postby Choseal on Thu Apr 15, 2010 12:39 pm

Yea, it's just the function, don't worry, I got all the variables covered and tested, they work as they should.

  1. Now the main problem:
  2. The mysql_fetch_array only returns record by record (so one at a time), not the whole array so you won't have all the slotnum the way you do your thing...


Not sure if I know what you mean, so, I should just change $res to $res['0']? -Sorry, still learning. =P

@Rifke Ooh yea, I see, stupid me. >.<
And I'm trying not too take everything from your code, since I didn't get the "this->" part, and I figured it should work this way too.

OK, the code I have now is:

  1.         function getFreeSlot()
  2.         {
  3.             $sql = mysql_query("SELECT slotnum FROM storage WHERE owner = 80 ORDER BY slotnum DESC");
  4.             $res = mysql_fetch_array($sql); //Uitkomst van sql
  5.             $sql = sprintf($sql,$boolres);
  6.             $x = 159;
  7.  
  8.             while ($x >= 1)
  9.             {
  10.                 if (in_array($x,$res))
  11.                 {
  12.                     $x--;
  13.                 }
  14.                 else
  15.                 {
  16.                     break;
  17.                 }
  18.             }
  19.             return $x;
  20.         }


Could you show me an example of how the fix the error I made, of which Lmame was talking?

P: Now it did use another slot, but it only goes down by one from 159, after that it wont go down. (Guess you already knew that would happen but thought I'd share it with you. =P)
Choseal
Electric Ghost
Electric Ghost
 
Posts: 837
Joined: Fri Jan 09, 2009 6:40 pm

Re: Item mall V2

Postby lmame on Thu Apr 15, 2010 12:46 pm

Let's say you got those slots for user 80...
1
2
3
4
5
6
8


When you do the query, you'll have 7 records, right?
When you do the mysql_fetch_array, it'll return the records one by one. So your first mysql_fetch_array will just return the slotnum "1".
That's why usually there is a while loop to gather all the records, like this:
  1. while ($row = mysql_fetch_array($result))
  2.  {
  3.     echo("Slotnum ".$row["slotnum"]."<br/>");  
  4. }


So in my example you'll have 7 loops, one for each record.

Check the php help on the php website.
The world is full of love and peace ^_^
Image
User avatar
lmame
Admin
Admin
 
Posts: 8997
Joined: Mon Aug 06, 2007 4:42 pm
Location: July City

Re: Item mall V2

Postby Choseal on Thu Apr 15, 2010 1:22 pm

Thank alot, It's almost done!

It worked, but then I emptied the storage table and tried again, now it's giving the duplicate entry error again:

  1.         function getFreeSlot()
  2.         {
  3.             $sql = mysql_query("SELECT * FROM storage WHERE owner = 80 ORDER BY slotnum DESC");
  4.             $res = mysql_fetch_array($sql); //Uitkomst van sql
  5.             //$sql = sprintf($sql,$boolres);
  6.             $x = 159;
  7.             while ($row = mysql_fetch_array($sql) & $x >= 1)
  8.             {
  9.                     if (!in_array($x,$row))
  10.                     {
  11.                         $x--;
  12.                     }
  13.                     else
  14.                     {
  15.                         break;
  16.                     }
  17.  
  18.             }
  19.             return $x;
  20.         }
Choseal
Electric Ghost
Electric Ghost
 
Posts: 837
Joined: Fri Jan 09, 2009 6:40 pm

Re: Item mall V2

Postby lmame on Thu Apr 15, 2010 1:41 pm

Well... First of all when you do a logical "AND" it's "&&" and NOT "&".
Second thing, as there is only one data returned, don't use in_array it's useless. Just test the value, like:

And why were you doing the "!in_array"? Your logic was wrong...

Third thing, why are you keeping the:

You're reading the first record doing that...
The world is full of love and peace ^_^
Image
User avatar
lmame
Admin
Admin
 
Posts: 8997
Joined: Mon Aug 06, 2007 4:42 pm
Location: July City

Re: Item mall V2

Postby Choseal on Thu Apr 15, 2010 2:09 pm

After changing everything you just said the error did not change:

  1.         function getFreeSlot()
  2.         {
  3.             $sql = mysql_query("SELECT * FROM storage WHERE owner = 80 ORDER BY slotnum DESC");
  4.             //$res = mysql_fetch_array($sql); //Uitkomst van sql
  5.             //$sql = sprintf($sql,$boolres);
  6.             $x = 159;
  7.             while ($row = mysql_fetch_array($sql) && $x >= 1)
  8.             {
  9.                     if ($row["slotnum"] == $x)
  10.                     {
  11.                         $x--;
  12.                     }
  13.                     else
  14.                     {
  15.                         break;
  16.                     }
  17.             }
  18.             return $x;
  19.         }


Thanks btw for helping me, I really couldn't/can't find the error. :D
Choseal
Electric Ghost
Electric Ghost
 
Posts: 837
Joined: Fri Jan 09, 2009 6:40 pm

Re: Item mall V2

Postby lmame on Thu Apr 15, 2010 2:58 pm

Honnestly I don't know if your code would work one day...
Here is something simple, try to understand it.

  1.  
  2. function getFreeSlot($user)
  3. {
  4.     $slot=0;
  5.     $slot_list=Array();
  6.    
  7.     $sql = mysql_query("SELECT * FROM storage WHERE owner = ".(int)$user." ORDER BY slotnum ASC");
  8.     while ($row = mysql_fetch_array($sql))
  9.     {
  10.         $slot_list[$row["slotnum"]]="ok";
  11.     }
  12.    
  13.     for($k=1;$k<160;$k++)
  14.     {
  15.         if(!isset($slot_list[$k]))
  16.         {
  17.             $slot=$k;
  18.             break;
  19.         }
  20.                
  21.     }
  22.    
  23.     return $slot;
  24. }
  25.  
  26. //getting free slot for user 11, 0 means error.
  27. getFreeSlot(11);
The world is full of love and peace ^_^
Image
User avatar
lmame
Admin
Admin
 
Posts: 8997
Joined: Mon Aug 06, 2007 4:42 pm
Location: July City

Re: Item mall V2

Postby Choseal on Thu Apr 15, 2010 4:40 pm

Awesome, it works great!

I didn't simply add it, I learned from it too, don't worry. :lol:

Thanks for helping me, appreciate it. =3
Choseal
Electric Ghost
Electric Ghost
 
Posts: 837
Joined: Fri Jan 09, 2009 6:40 pm

PreviousNext

Return to PHP / Web Guides, Scripts and tools.

Who is online

Users browsing this forum: No registered users and 2 guests