Jump to content

Help please simplifying code


apw

Recommended Posts

Hello

 

  Hello, I was wondering if you could give me your suggestions on simplifying some code I had written.  What I'm attempting

to do is the following:

 

  I created a database table called barn, I added a few rows in the barn (as tests) called ..

slot1_name to slot5_name

 

in each of these i filled as different foods such as potatoes, carrots, tomatoes, pumpkins and grapes (as examples)

 

now my question is, after i query my DB as follows:

  

$barnstats="SELECT * from fw_barn where playername='$player'";

    $barnstats2=mysql_query($barnstats) or die("Could not get user stats");

    $barnstats3=mysql_fetch_array($barnstats2);

 

I now have all my slots loaded into my $barnstats3 (i think) but my question is ...

 

Say player A wants to buy another food from the vendor, the script has all the current foods that player A has in their barn ..

but needs to check to see if any available slot1 to slot5 are empty .. what I did was I wrote 5 different if statements checking

to see if those slots are already filled .. is there just one if statement I can write instead to check all slot1 to slot5 to see if

they are either empty OR have something already inside them?

 

Thanks in advance for your help

Link to comment
Share on other sites

  I created a database table called barn, I added a few rows in the barn (as tests) called ..

slot1_name to slot5_name

 

in each of these i filled as different foods such as potatoes, carrots, tomatoes, pumpkins and grapes (as examples)

Huh? Did you meant to say you create r columns (or fields) for slot1_name - slot5_name? Otherwise it doesn't make sense. Based upon your query I am guessing you have one record in the table per player and each record has 5 static fields for those slots.

 

Also, not sure what you meant about 5 if() statements since you should be doing the check in your query. Your current structure (If I understand it correctly) cannot be easily modified. So, only use it if you plan on never changing the number of slots. Otherwise I have a different solution below. but, with your current structure you can see if a play has any empty slots using a single query:

 

SELECT * FROM fw_barn
WHERE slot1_name = ""
   OR slot2_name = ""
   OR slot3_name = ""
   OR slot4_name = ""
   OR slot5_name = ""

But, even with that you then have to figure out which field is empty when you update the records.

 

A better way to handle this - especially if you ever want to change the number of slots, is to create the table so each record represents ONE slot associated with a player. So, each player can have up to five records in the table. You can then query the table to see how many records (slots) a user has. If less than 5 you know there is a slot available and you can just add the record - no need to see where the record will be added. Also, when a player removes an item you just delete that record to make the 'slot' available.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.