Jump to content


Photo

What do I wrong?


  • Please log in to reply
7 replies to this topic

#1 bonzie

bonzie
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 15 June 2006 - 02:28 PM

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!

$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];

Anyone who knows what I do wrong?


#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 15 June 2006 - 02:32 PM

Sorry, but that code makes little sense. Where do you define the constants NUMBERI and NUMBERJ ?

#3 bonzie

bonzie
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 15 June 2006 - 02:35 PM

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) View Post[/div][div class=\'quotemain\'][!--quotec--]
Sorry, but that code makes little sense. Where do you define the constants NUMBERI and NUMBERJ ?
[/quote]


#4 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 15 June 2006 - 02:38 PM

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?

#5 bonzie

bonzie
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 15 June 2006 - 03:07 PM

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

$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);

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:

$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;
   }
}

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:
// 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;
      }
   }
}
[!--quoteo(post=384194:date=Jun 15 2006, 03:38 PM:name=thorpe)--][div class=\'quotetop\']QUOTE(thorpe @ Jun 15 2006, 03:38 PM) View Post[/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]


#6 bonzie

bonzie
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 15 June 2006 - 03:56 PM

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) View Post[/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

$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);

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:

$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;
   }
}

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:
// 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;
      }
   }
}
[/quote]


#7 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 15 June 2006 - 04:26 PM

Im sorry, but I still just can't understand what your doing. Anyway, try adding....
print_r($a);
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.

#8 bonzie

bonzie
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 15 June 2006 - 05:11 PM

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(item_id, (item_id), 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:
array([user1]=>array([item1]=>1
                                 [item2]=>0
                                 [item3]=>0)
array([user1]=>array([item1]=>0
                                 [item2]=>0
                                 [item3]=>0)
...


Therefore I use
$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
   }
}

Until now everything works. When I for example do
echo $arr_user_item[1][1];
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.
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;
      }
   }
}

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) View Post[/div][div class=\'quotemain\'][!--quotec--]
Im sorry, but I still just can't understand what your doing. Anyway, try adding....
print_r($a);
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]





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users