Jump to content

Archived

This topic is now archived and is closed to further replies.

bonzie

What do I wrong?

Recommended Posts

hello

I am stuck with the following script. I have spent hours to make the whole script and now I tried it out and apparently the values I assign to the array based on the sql-query appear to be lost. When I try the 'echo' statement (last line of code) I dont get any output!

[code]
$a=array();
for ($i=1; $i<=NUMBERI; $i++)
{
   for ($j=1; $j<=NUMBERJ; $j++)
   {
   $a[$i][$j]=0;
   }
}

echo $a[1][1];

for ($i=1; $i<=NUMBERI; $i++)
{
   for ($j=1; $j<=NUMBERJ; $j++)
   {
   .....
// here a sql-query
   .....

   if ($num_results>0)
      {
      $a[$i][$j]=1;
      }
   else
      {
      $a[$i][$j]=0;
      }
   }
}

echo $a[1][1];
[/code]

Anyone who knows what I do wrong?

Share this post


Link to post
Share on other sites
Sorry, but that code makes little sense. Where do you define the constants NUMBERI and NUMBERJ ?

Share this post


Link to post
Share on other sites
these are defined earlier in the script so they have a value.

[!--quoteo(post=384184:date=Jun 15 2006, 03:32 PM:name=thorpe)--][div class=\'quotetop\']QUOTE(thorpe @ Jun 15 2006, 03:32 PM) [snapback]384184[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Sorry, but that code makes little sense. Where do you define the constants NUMBERI and NUMBERJ ?
[/quote]

Share this post


Link to post
Share on other sites
And what are there values then? Help us help you. Put this code in context.... show us your query code... something?

I dont understand why you even have this code.... what is it meant to do?

Share this post


Link to post
Share on other sites
I need to construct a kind of recommendation algorithm.

I have a table in mysql named 'has_bought' with columns 'user_id' and 'item_id'. So an entry means that 'user' has bought 'item'.


I need to construct a vector V for each user in the 'itemspace': You can think of this as the rows of a binary matrix A where the columns are the items:

eg:
user 1 |0 1 1|
user 2 |1 0 1|
user 3 |0 1 1|
This means that user 1 has bought item 2 and 3.

The constant NUMBERI is the number of users and the constant NUMBERJ the number of items in the database. They are defined as

[code]
$query_num_users="select * from people"; // table people contains data about users, key is user_id
    $result_num_users=mysql_query($query_num_users, $connect);
    $num_users=mysql_num_rows($result_num_users);
define (NUMBERI, $num_users);

$query2="select item_id from item"; // table item contains data about all the items of the shop
    $result=mysql_query($query2,$connect);
    $num_results=mysql_num_rows($result);
//define constant NUMBERJ
define (NUMBERJ, $num_results);
[/code]

I have constructed the array $a ['user']['item'] because I need to make compare the vectors of the different users to determine the most similar users to a certain user (the one logged in).
I create the array with the following code:

[code]
$a();
for ($user_id=1; $user_id<=NUMBERI; $user_id++)
{
   for ($item_id=1; $item_id<=NUMBERJ; $item_id++)
   {
   $a[$user_id][$item_id]=0;
   }
}
[/code]

Now i would like toassign a 1 to a combination of user-item if the user has bought the item:

To find this in the database I constructed the following query:
[code]
// to find combinations user-item
for ($user_id=1; $user_id<=NUMBERI; $user_id++)
{
   for ($item_id=1; $item_id<=NUMBERJ; $item_id++)
   {
query3="select * from has_bought
                      where user_id=".$user_id."
         and item_id=".$item_id."";
   $result3=mysql_query($query3, $connect);
   $num_results_query3=mysql_num_rows($result3);

// to assign the values to the array $a to create the binary matrix A (rows are the users and columns the items)
   if ($num_results_query3>0)//if an entry found, this means that user has bought the item
      {
      $a[$user_id][$item_id]=1;
      }
   else
      {
      $a[$user_id][$item_id]=0;
      }
   }
}
[/code]
[!--quoteo(post=384194:date=Jun 15 2006, 03:38 PM:name=thorpe)--][div class=\'quotetop\']QUOTE(thorpe @ Jun 15 2006, 03:38 PM) [snapback]384194[/snapback][/div][div class=\'quotemain\'][!--quotec--]
And what are there values then? Help us help you. Put this code in context.... show us your query code... something?

I dont understand why you even have this code.... what is it meant to do?
[/quote]

Share this post


Link to post
Share on other sites
Ok, I have been trying again and again. But I am still stuck with this... [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /]

Is it maybe because I assign values to the array in a for-loop that I can't get these values outside the loop?
[img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /]

[!--quoteo(post=384209:date=Jun 15 2006, 04:07 PM:name=bonzie)--][div class=\'quotetop\']QUOTE(bonzie @ Jun 15 2006, 04:07 PM) [snapback]384209[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I need to construct a kind of recommendation algorithm.

I have a table in mysql named 'has_bought' with columns 'user_id' and 'item_id'. So an entry means that 'user' has bought 'item'.
I need to construct a vector V for each user in the 'itemspace': You can think of this as the rows of a binary matrix A where the columns are the items:

eg:
user 1 |0 1 1|
user 2 |1 0 1|
user 3 |0 1 1|
This means that user 1 has bought item 2 and 3.

The constant NUMBERI is the number of users and the constant NUMBERJ the number of items in the database. They are defined as

[code]
$query_num_users="select * from people"; // table people contains data about users, key is user_id
    $result_num_users=mysql_query($query_num_users, $connect);
    $num_users=mysql_num_rows($result_num_users);
define (NUMBERI, $num_users);

$query2="select item_id from item"; // table item contains data about all the items of the shop
    $result=mysql_query($query2,$connect);
    $num_results=mysql_num_rows($result);
//define constant NUMBERJ
define (NUMBERJ, $num_results);
[/code]

I have constructed the array $a ['user']['item'] because I need to make compare the vectors of the different users to determine the most similar users to a certain user (the one logged in).
I create the array with the following code:

[code]
$a();
for ($user_id=1; $user_id<=NUMBERI; $user_id++)
{
   for ($item_id=1; $item_id<=NUMBERJ; $item_id++)
   {
   $a[$user_id][$item_id]=0;
   }
}
[/code]

Now i would like toassign a 1 to a combination of user-item if the user has bought the item:

To find this in the database I constructed the following query:
[code]
// to find combinations user-item
for ($user_id=1; $user_id<=NUMBERI; $user_id++)
{
   for ($item_id=1; $item_id<=NUMBERJ; $item_id++)
   {
query3="select * from has_bought
                      where user_id=".$user_id."
         and item_id=".$item_id."";
   $result3=mysql_query($query3, $connect);
   $num_results_query3=mysql_num_rows($result3);

// to assign the values to the array $a to create the binary matrix A (rows are the users and columns the items)
   if ($num_results_query3>0)//if an entry found, this means that user has bought the item
      {
      $a[$user_id][$item_id]=1;
      }
   else
      {
      $a[$user_id][$item_id]=0;
      }
   }
}
[/code]
[/quote]

Share this post


Link to post
Share on other sites
Im sorry, but I still just can't understand what your doing. Anyway, try adding....
[code]
print_r($a);
[/code]
to the end of the script, this will give you an idea of how what your final array contains.... maybe that will lead you to a solution.

Share this post


Link to post
Share on other sites
that is the problem, then the array doesn't exist anymore:

print_r($a); doesn't give anything

euh maybe I put the problem differently

you have for example a matrix A,
the rows are the users and the columns the items:

|100|
|011|
|001|

A 1 in the matrix indicates that the user has bought the item

I have a SQL table has_bought([u]item_id[/u], ([u]item_id[/u]), so with mysql I can determine if a user has bought a particular item.

I thought of contructing an array to represent the matrix A:
[code]
array([user1]=>array([item1]=>1
                                 [item2]=>0
                                 [item3]=>0)
array([user1]=>array([item1]=>0
                                 [item2]=>0
                                 [item3]=>0)
...
[/code]

Therefore I use
[code]
$arr_user_item=array();
for ($user_id=1; $user_id<=NUMBERUSERS; $user_id++)
{
   for ($item_id=1; $item_id<=NUMBERITEMS; $item_id++)
   {
   $arr_user_item[$user_id][$item_id]=0; //this value of row '$user_id' and column 'item_id' in matrix A
   }
}
[/code]

Until now everything works. When I for example do
[code]
echo $arr_user_item[1][1];
[/code]
I get 0 on my screen.

However I have to assign the right values to the array using the mysql-query on table has_bought.

When I do this like I have showed earlier and I do again echo $arr_user_item[1][1]; or even print_r[$arr_user_item] nothing seems to happen.
[code]
for ($item_id=1; $item_id<=NUMBERUSERS; $item_id++) //constant NUMBERUSERS is the total number of users in our database
{
   for ($item_id=1; $item_id<=NUMBERITEMS; $item_id++) //constant NUMBERITEMS is the total number of items in our database

   {
// to see if user has bought the item
   $query_user_item="select * from has_bought
                      where user_id=".$user_id."
         and item_id=".$item_id."";
   $result_user_item=mysql_query($query_user_item);
   $num_results_user_item=mysql_num_rows($result_user_item);
   // if an entry found is found then user has bought the item:
   if ($num_results_user_item>0)
      {
      $arr_user_item[$user_id][$item_id]=1;
      }
   else
      {
      $arr_user_item[$user_id][$item_id]=0;
      }
   }
}
[/code]

When i do now print_r ($arr_user_item); I don't get anything.

I checked if my query worked and yes it did. So why can't I work with my array after I assigned new values?

[!--quoteo(post=384259:date=Jun 15 2006, 06:26 PM:name=thorpe)--][div class=\'quotetop\']QUOTE(thorpe @ Jun 15 2006, 06:26 PM) [snapback]384259[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Im sorry, but I still just can't understand what your doing. Anyway, try adding....
[code]
print_r($a);
[/code]
to the end of the script, this will give you an idea of how what your final array contains.... maybe that will lead you to a solution.
[/quote]

Share this post


Link to post
Share on other sites

×

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.