Jump to content

Help with Arrays + MYSQL


Infect

Recommended Posts

OK i'm stumped here. I don't know much PHP so i need some help from all you smart coders out there

 

1. What i want to do, is send a customer to a URL, and on that URL, i just want it to print a single item from an array. Once that item has been printed, i want to move it to a different array (ie: from $itemsToPrint to $itemsNotToPrint). I have the array configured fine, the problem is i don't know how to figure out which items have been selected and i dont know how to move the selected items to another array. Apparently i also need to set up a MYSQL Database to keep track of what has and has not been selected and used. Unfortunately i'm not very familiar with MYSQL either, i pretty much just understand a bit of PHP MyAdmin...

 

2. Also, i want to make sure that each ip address can only do this once by giving them a cookie or something (so that they can't repeatedly steal more and more item codes by simply refreshing the page or something), or even better if i could do something with the url to make each item in the array have it's own url or be able to mask the real url.

 

Any help would be greatly appreciated! Thanks,

 

Infect

Link to comment
Share on other sites

MMkay well the starting array contains lots of codes (ie: 1M0zzl2apr4 or something seemingly random like that) and i want the user to access the page, have it print just ONE of the codes in the array, and once that code has been printed, i want to move it to a different array (ie: From $itemsToPrint TO $itemsNotToPrint)

 

Does that clarify a bit more?

Link to comment
Share on other sites

I think you could implement it something like this:

 

CREATE TABLE item_codes (

  id serial,

  code text

);

 

CREATE TABLE ip_item_codes (

  item_id bigint,

  ip text,

  date_fetched datetime

);

 

"id" is an integer uniquely identifying each item.

 

ip_item_codes records which items have been given to which ip address, and at what time.  This is assuming you use ip address to uniquely identify your users (which is fraught with problems).

 

Each time you give out an item you can store a row in ip_item_codes.  Then you can refuse to give more items if that ip has a row in that table with date_fetched less than 24 hours ago.

 

This is not an ideal solution but it's a start.

 

This also allows you to check that someone has been given an item code when they try to use the item, as you can check in the ip_item_codes table.

Link to comment
Share on other sites

ok, well i created the table fine, now i've used this code:

mysql_query("CREATE TABLE item_codes (
  id serial,
  code text
);");

while($row = $result)
{
    echo "ID :{$row['id']} <br>" .
         "CODE : {$row['code']} <br>";
}

 

and it won't echo what i want it do...obviously something wrong with my command but idk what it is? What i want to do now is just echo every single item in my table.

Link to comment
Share on other sites

This command is done once only, to create a place to store data (called a "table"):

 

$sql = "CREATE TABLE item_codes (
  id serial,
  code text
)";
$result = mysql_query($sql);
if (!$result) die("Query $sql failed: " . mysql_error());

 

Then this query is done each time you want to fetch data from the table

 

$sql = "SELECT * FROM item_code";
$result = mysql_query($result);
f (!$result) die("Query $sql failed: " . mysql_error());
while($row = mysql_fetch_array($result))
{
    echo "ID :{$row['id']} <br>" .
         "CODE : {$row['code']} <br>";
}

 

But first you will need to add some data, otherwise you'll get nothing from that SELECT query:

 

$sql = "INSERT INTO item_code (code) values ('foo')";
$result = mysql_query($result);
if (!$result) die("Query $sql failed: " . mysql_error());

 

Once those ones are working you can add some more data and start looking at the "WHERE" clause from SELECT.

Link to comment
Share on other sites

ah ok great thx. So now i have the table built and everything is entered correctly. The problem now is, i want to only display one randomly from the list, and then once it's displayed, move it to a new table (table already created).

 

How would i go about doing this?

Link to comment
Share on other sites

The easiest way to pick a random one is to generate a random number in php.  You can find the highest code with "SELECT max(code) FROM item_codes", with the result being named "max".

 

There's also a thread in the mysql subforum here about choosing random rows from mysql, but it's a little complex.

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.