homepage Welcome to WebmasterWorld Guest from 54.198.148.191
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Accredited PayPal World Seller

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Why is my array only holding 1 row of mysql data?
tec4




msg:4386274
 7:15 pm on Nov 12, 2011 (gmt 0)

Hi Everyone,

Pretty much, I am trying to store an array in my Session class to hold a users saved items, that are stored in their MySQL Database, but it seems to only be storing the first item...

Basic Code (very slimmed down):

Session.php

class Session
{
var $userItems = array(); //The array holding all users saved items

function checkLogin() {

//code that checks if user is logged in goes here...
//confirms and set username SESSION variable

$this->userItems = $database->getUserSavedItems($_SESSION['username']);

}

Database.php

function getUserSavedItems($username){

$q = "SELECT ItemID FROM ".TBL_SAVED_ITEMS." WHERE username = '$username'";
$result = mysql_query($q, $this->connection);

/* Error occurred, return given name by default */
if(!$result || (mysql_numrows($result) < 1)){
return NULL;
}

/* Return result array */
$dbarray = mysql_fetch_array($result);
return $dbarray;

}



When I try to print out the contents of the Array, I get something like the line below (which spits out two of the same ItemID's...aka 1 DB row):

Array ( [0] => 11566001 [ItemID] => 11566001 )


How would I be able to store every ItemID that the user has under his/her username?

I am trying to store it like this because I would like to use this information to make the user know which items are already saved. For example, when searching the site, they would see something like "Remove this Item", if it's in their DB and "Save this Item", if it is not.

Thank you for your time,

Tec

 

tw56




msg:4386287
 8:00 pm on Nov 12, 2011 (gmt 0)

How are you printing out the contents of the array?
if($result) {
while($row = mysql_fetch_array($result)){
$f1 = $row[$gw];
$f2 = $row[$ew];
echo '<p class="one">' . $f1 . ' ' . $f2 . '</p>';
}

tec4




msg:4386308
 8:53 pm on Nov 12, 2011 (gmt 0)

Was just using: "print_r($session->userItems);" but did not do the loop after the query.

Ya, that makes a lot of sense....not sure why that did not click...

Added this to the database.php file and seems to store correctly now:

function getUserSavedItems($username){
$q = "SELECT ItemID FROM ".TBL_SAVED_ITEMS." WHERE username = '$username'";

$result = mysql_query($q, $this->connection);

/* Error occurred, return given name by default */
if(!$result || (mysql_numrows($result) < 1)){
return NULL;
}

$ret = Array();
while($row = mysql_fetch_assoc($result)) {

$x->name = $row['ItemID'];
$ret[] = $x;
}

return($ret);

}


How can i compare the values in the array to a item that is being looped through to show the user if they have the item in their DB already?

I am trying to use:

foreach (item as items) {

if (in_array($itmes->ItemID, $session->userItems->name)) {

echo "This Item Is in Your DB";

}

else {

echo "Not in your DB";

}

}


But I'm getting this error message:

Warning: in_array() expects parameter 2 to be array, null given in file bla bla.

When I take off the "->name" portion of the this line, like so:

if (in_array($itmes->ItemID, $session->userItems)) {

I do not get the error but it is not properly identifying whether or not the item is in the DB or not, any thoughts?

Thanks tw56 for helping with that :)

tec4




msg:4386311
 9:02 pm on Nov 12, 2011 (gmt 0)

Looks like I lied....Thought the looping that I was doing was working but seems to be looping each and storing the same ItemID for each in the array.

tw56




msg:4386318
 9:17 pm on Nov 12, 2011 (gmt 0)

First test your query and make sure it gets what you want.

tec4




msg:4386319
 9:19 pm on Nov 12, 2011 (gmt 0)

Modified, ahem..again and seems to be working...changed it to the following:

$ret = Array();
while($row = mysql_fetch_assoc($result)) {
if(!strlen(trim($row['ItemID']))) {
continue;
}
if (isset($ret[$row['ItemID']])) { continue; }
//$x->name = $row['ItemID'];
$ret[$row['ItemID']] = true;
}


return($ret);



When trying to compare all the items in my DB with just the ones in the user's DB with:

if (in_array($Item->ItemID, $session->userItems))
{
echo "In User DB";
} else { echo "NOT in user DB"; }

all of them say "In User DB", when they are not...seems odd but can put any value in the "$Item->ItemID" section of the in_array function and is always coming out true - aka "In User DB"

tec4




msg:4386320
 9:22 pm on Nov 12, 2011 (gmt 0)

ya, just tested. Each item in the array is an indivual value.

Example of the print_r now:

Array ( [F111467788] => 1 [F11141117] => 1 [11566331] => 1 [I147946] => 1 [S67539] => 1 [S23599] => 1 [CH123133763] => 1 [S678709] => 1 [A11144189] => 1 [F11144520] => 1 [12158412] => 1 [I1B44661] => 1 [Q678829] => 1 )

tec4




msg:4386342
 10:43 pm on Nov 12, 2011 (gmt 0)

Looks like I have to use array_key_exists instead of in_array.

Thanks tw56!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved