Jump to content

Need help with arrays please!


scottrohe

Recommended Posts

ok so heres what im at. it works EXCEPT for the fact that it only grabs 1 column from the database.. so instead of the array coming out like [ 0 ]=1 [1]=2 it comes out [ 0 ]=1  ( ( (spaces added to [ 0 ]'s on here so it didnt display weird symbol) ) ) and thats it. what am i doing wrong? this code is also in a while loop. my objective; i have the while while loop displaying the top '3' layouts by views.. then in that loop i wanted to add the option to 'add this layout to favorites'.. but if the item is already in the users favorites, instead of displaying 'add to favorites' it displays 'delete from favorites'.. thats what this code below is for.. just cant seem to get it to work after hours of trying. any help & time is much appreciated.

note: $hl_id = the "hot layout" id. this code is in the while loop..

[code=php:0]
$sqlfavc = mysql_query("SELECT favid FROM favorites WHERE username='$session->username'");
    $ss=mysql_fetch_row($sqlfavc);
    foreach ($ss as $key){
    if($key == $hl_id){
$exists='false';
}
else {
$exists='true';
} [/code]


i then have another if statement saying if exists = true, echo delete from favorites.. vice versa. did it that way since its looping..
Link to comment
Share on other sites

$ss=mysql_fetch_row($sqlfavc);

For that part, all you doing is fetching one table row. Thus, $ss will only contain the single value of 'favid' .
To get more than 1 row (assuming your query yields more than 1 row results),  use a while loop to go through your table rows.

[code=php:0]
while(list($favid) = mysql_fetch_row($sqlfavc))
{
   $exists = ($favid == $hl_di) ? false : true;
   if($exists)
   {
   //delete from favorites code
   }
}
[/code]
Link to comment
Share on other sites

still doesn't seem to work, i've messed with it for a good 30 mins too. came out with pretty much the same thing just altered a few things to check what was happening. when i do a print_r for $favid, i get no result.. $hl_id gives me the correct result still though. on the page its showing 2 things.. which are how many rows i have in my sql table.. id: 1, and 2. instead of the if statement going through each result to find one correct one, it goes through every result and echos a image for each result. so example; witch each id being in favs; id 1.. in favs? yes-no; id 2.. in favs? no-yes.. id 3 in favs? no-no-yes.. ( id 4: being, not in favs.. ) no-no-no-no.
where as it should be id1.. in favs? yes; id2.. in favs? yes; id3.. in favs? yes; id 4.. in favs? no. hopefuly made myself clear on that.. thanks for the help so far, so close!

[code=php:0]
    <?php
    $sqlfavc = mysql_query("SELECT favid FROM favorites WHERE username='$session->username'");
    while(list($favid) = mysql_fetch_row($sqlfavc))
{
  $exists = ($favid == $hl_id) ? true : false;
  if($exists) {
//delete fav image
} else {
//add fav image
}
}
?>
[/code]
Link to comment
Share on other sites

Try this:
[code]<?php
<?php
    $q = "SELECT favid FROM favorites WHERE username='$session->username'";
    $sqlfavc = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());
    while($row = mysql_fetch_assoc($sqlfavc)) {
          $exists = ($row['favid'] == $hl_id) ? true : false;
          if($exists) {
              //delete fav image
          } else {
            //add fav image
          }
    }
?>[/code]

Ken
Link to comment
Share on other sites

do you think it could be doing this because its inside another loop? i dont think it would effect it but...

[code=php:0]
<?php
$sqltwo1 = mysql_query("SELECT * FROM ".TBL_LAYOUTS." ORDER BY views DESC LIMIT 3");
while ($r=mysql_fetch_array ($sqltwo1))
{   
    $hl_id=$r["id"];
    $hl_subcat=$r["subcat"];
    $hl_username=$r["username"];
e    $hl_lname=$r["lname"];
    $hl_thumb=$r["thumb"];
    $hl_views=$r["views"];
   
    $hl_views++;
    $sqltwo2 = "UPDATE ".TBL_LAYOUTS." SET views='$hl_views' WHERE id='$hl_id'";
    $result = mysql_query($sqltwo2);

//top 3 layouts table is right here.. add/delete to favorites images are within the table.

    $q = "SELECT favid FROM favorites WHERE username='$session->username'";
    $sqlfavc = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());
    while($row = mysql_fetch_assoc($sqlfavc)) {
          $exists = ($row['favid'] == $hl_id) ? true : false;
          if($exists) {
//show delete favorite image
          } else {
//show add favorite image
          }
    }
?>
[/code]
Link to comment
Share on other sites

This really looks OK.

Can you be more specific about the output of the page?  I didn't understand the

id 1.. in favs? yes-no; id 2.. in favs? no-yes.. id 3 in favs? no-no-yes.. ( id 4: being, not in favs.. ) no-no-no-no.
where as it should be id1.. in favs? yes; id2.. in favs? yes; id3.. in favs? yes; id 4.. in favs? no.
Link to comment
Share on other sites

ill try to explain how it looks better.

"What's Hot?"

if the user [b]doesnt[/b] have it in their personal favs it should appear as ( * being a 'star' signifying 'add to favorites' ):
Layout ID:1
_________
|thumb    |  *  Layout Name
|of          |      Layout Catagory
|hotlayout |      Author

if the user [b]does[/b] have it in their personal favs it should appear as ( x being a 'X image' signifying 'delete from favorites' ):
Layout ID:2
_________
|thumb    |  X  Layout Name
|of          |      Layout Catagory
|hotlayout |      Author

whith the current code i posted above
i am seeing this:

Layout ID: 1
_________
|thumb    |  X  Layout Name
|of          | *  Layout Catagory
|hotlayout |      Author

Layout ID:2
_________
|thumb    |  *  Layout Name
|of          |  X    Layout Catagory
|hotlayout |      Author

hope this is better, thanks.
Link to comment
Share on other sites

my If statement is;
[code=php:0]
<?php
          $exists = ($row['favid'] == $hl_id) ? true : false;
          if($exists) {
?>
<a href="?x=delf&f=<?php echo $hl_id;?>&c=layouts&s=<?php echo $hl_subcat;?>"><img src="i/alreadyfav.gif" hsrc="i/alreadyfav2.gif" align="left" border="0"></a>
<?php
          } else {
?>
<a href="?x=addf&fav=<?php echo $hl_id;?>&cat=layouts&sub=<?php echo $hl_subcat;?>"><img src="i/favicon2.gif" hsrc="i/favicon2_2.gif" align="left" border="0"></a>
<?php
          }
?>
[/code]
Link to comment
Share on other sites

So it seems the impossible is happening:  you have both conditionals for your if($exists){}else{} being executed.

I know it seems unlikely, but could it be there's something wrong with the placement of your code within the HTML?  Relative to the code you've shown, where is the thumbnail image and the Layout Name, etc. data?
Link to comment
Share on other sites

OK, I think I might have it now.

It seems to me that for each thumbnail image obtained from your

$sqltwo1 = mysql_query("SELECT * FROM ".TBL_LAYOUTS." ORDER BY views DESC LIMIT 3");


that you are searching through the favids of the user and creating a 'delete from favs' or an 'add to favs' icon for each favid in the table for you user

instead, maybe you should use this query to get your $exists:

SELECT * FROM favorites WHERE username='$session->username' AND favid='$hl_id'

then $exists=mysql_num_rows($result of above query);
Link to comment
Share on other sites

[code=php:0]
<?php
  $q = "SELECT * FROM favorites WHERE username='$session->username' AND favid='$hl_id'";
    $sqlfavc = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());
          $exists=mysql_num_rows($sqlfavc);
          if($exists == $hl_id) {
?>
<a href="?x=delf&f=<?php echo $hl_id;?>&c=layouts&s=<?php echo $hl_subcat;?>"><img src="i/alreadyfav.gif" hsrc="i/alreadyfav2.gif" align="left" border="0"></a>
<?php
          } else {?>
<a href="?x=addf&fav=<?php echo $hl_id;?>&cat=layouts&sub=<?php echo $hl_subcat;?>"><img src="i/favicon2.gif" hsrc="i/favicon2_2.gif" align="left" border="0"></a>
<?php
          }
?>
[/code]

when i print_r($exists);  it shows the id "1" for both, layout 1 and 2.. like it picks out the 1st id and saves it to the variable then stops... thats why the 2nd one is getting that same output, which confuses the hell out of me since its already in a loop! thought it was right then tested and got that :( closeeeee....
Link to comment
Share on other sites

Let's save you some database calls and move things around a bit:

[code=php:0]
<?php
// Fetch favorites for the current user, place them in an enumerated array
$sqlfavc = mysql_query("SELECT favid FROM favorites WHERE username='$session->username'") or die("Error Retrieving User Favorites: ".mysql_error());
$user_favs = array_values(mysql_fetch_array($sqlfavc));

// Fetch all favorites
$sqltwo1 = mysql_query("SELECT * FROM ".TBL_LAYOUTS." ORDER BY views DESC LIMIT 3") or die("Error Retrieving Favorites: ".mysql_error());

// Loop through all favorites
while ($r=mysql_fetch_array($sqltwo1))
{   
    $hl_id=$r["id"];
    $hl_subcat=$r["subcat"];
    $hl_username=$r["username"];
    $hl_lname=$r["lname"];
    $hl_thumb=$r["thumb"];
    $hl_views=$r["views"];
   
    $hl_views++;
    $sqltwo2 = "UPDATE ".TBL_LAYOUTS." SET views='$hl_views' WHERE id='$hl_id'";
    $result = mysql_query($sqltwo2);

//top 3 layouts table is right here.. add/delete to favorites images are within the table.

    // Check if the user already has this favorite
    if(in_array($hl_id, $user_favs)){
          //show delete favorite image
    } else {
          //show add favorite image
    }
}
?>
[/code]
Link to comment
Share on other sites

[quote author=scottrohe link=topic=102829.msg409097#msg409097 date=1154630273]
[code=php:0]
<?php
   $q = "SELECT * FROM favorites WHERE username='$session->username' AND favid='$hl_id'";
    $sqlfavc = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());
           $exists=mysql_num_rows($sqlfavc);
           if($exists == $hl_id) {
?>
<a href="?x=delf&f=<?php echo $hl_id;?>&c=layouts&s=<?php echo $hl_subcat;?>"><img src="i/alreadyfav.gif" hsrc="i/alreadyfav2.gif" align="left" border="0"></a>
<?php
           } else {?>
<a href="?x=addf&fav=<?php echo $hl_id;?>&cat=layouts&sub=<?php echo $hl_subcat;?>"><img src="i/favicon2.gif" hsrc="i/favicon2_2.gif" align="left" border="0"></a>
<?php
           }
?>
[/code]

when i print_r($exists);  it shows the id "1" for both, layout 1 and 2.. like it picks out the 1st id and saves it to the variable then stops... thats why the 2nd one is getting that same output, which confuses the hell out of me since its already in a loop! thought it was right then tested and got that :( closeeeee....
[/quote]

you're almost there. 

instead of  if($exists == $hl_id), you should have if($exists).  Remember, $exists is the number of rows, which should be either 1 for a macth or 0 for no match.
Link to comment
Share on other sites

[quote author=bltesar link=topic=102829.msg409131#msg409131 date=1154633838]
[quote author=scottrohe link=topic=102829.msg409097#msg409097 date=1154630273]
[code=php:0]
<?php
  $q = "SELECT * FROM favorites WHERE username='$session->username' AND favid='$hl_id'";
    $sqlfavc = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());
          $exists=mysql_num_rows($sqlfavc);
          if($exists == $hl_id) {
?>
<a href="?x=delf&f=<?php echo $hl_id;?>&c=layouts&s=<?php echo $hl_subcat;?>"><img src="i/alreadyfav.gif" hsrc="i/alreadyfav2.gif" align="left" border="0"></a>
<?php
          } else {?>
<a href="?x=addf&fav=<?php echo $hl_id;?>&cat=layouts&sub=<?php echo $hl_subcat;?>"><img src="i/favicon2.gif" hsrc="i/favicon2_2.gif" align="left" border="0"></a>
<?php
          }
?>
[/code]

when i print_r($exists);  it shows the id "1" for both, layout 1 and 2.. like it picks out the 1st id and saves it to the variable then stops... thats why the 2nd one is getting that same output, which confuses the hell out of me since its already in a loop! thought it was right then tested and got that :( closeeeee....
[/quote]

you're almost there. 

instead of  if($exists == $hl_id), you should have if($exists).  Remember, $exists is the number of rows, which should be either 1 for a macth or 0 for no match.
[/quote]

THANKYOU. That did it :) RELIEF!!!!!!!!!!!!!!! heh. Thanks a lot for all the help you guys have gave, it's greatly appreciated. I'm sure i'll be back again sometime. Once again thanks.. Better go before I piss myself outa excitement.
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.