scottrohe Posted August 3, 2006 Share Posted August 3, 2006 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.. Quote Link to comment https://forums.phpfreaks.com/topic/16419-need-help-with-arrays-please/ Share on other sites More sharing options...
extrovertive Posted August 3, 2006 Share Posted August 3, 2006 $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] Quote Link to comment https://forums.phpfreaks.com/topic/16419-need-help-with-arrays-please/#findComment-68387 Share on other sites More sharing options...
scottrohe Posted August 3, 2006 Author Share Posted August 3, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/16419-need-help-with-arrays-please/#findComment-68596 Share on other sites More sharing options...
kenrbnsn Posted August 3, 2006 Share Posted August 3, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/16419-need-help-with-arrays-please/#findComment-68601 Share on other sites More sharing options...
scottrohe Posted August 3, 2006 Author Share Posted August 3, 2006 Ken, ty for the fast reply. Sadly though, same result. Quote Link to comment https://forums.phpfreaks.com/topic/16419-need-help-with-arrays-please/#findComment-68603 Share on other sites More sharing options...
scottrohe Posted August 3, 2006 Author Share Posted August 3, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/16419-need-help-with-arrays-please/#findComment-68627 Share on other sites More sharing options...
bltesar Posted August 3, 2006 Share Posted August 3, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/16419-need-help-with-arrays-please/#findComment-68645 Share on other sites More sharing options...
scottrohe Posted August 3, 2006 Author Share Posted August 3, 2006 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 | Authorif 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 | Authorwhith the current code i posted abovei am seeing this:Layout ID: 1 _________|thumb | X Layout Name|of | * Layout Catagory|hotlayout | AuthorLayout ID:2 _________|thumb | * Layout Name|of | X Layout Catagory|hotlayout | Authorhope this is better, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/16419-need-help-with-arrays-please/#findComment-68656 Share on other sites More sharing options...
bltesar Posted August 3, 2006 Share Posted August 3, 2006 please show some of the code in if($exists) {//show delete favorite image } else {//show add favorite image } Quote Link to comment https://forums.phpfreaks.com/topic/16419-need-help-with-arrays-please/#findComment-68700 Share on other sites More sharing options...
scottrohe Posted August 3, 2006 Author Share Posted August 3, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/16419-need-help-with-arrays-please/#findComment-68721 Share on other sites More sharing options...
bltesar Posted August 3, 2006 Share Posted August 3, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/16419-need-help-with-arrays-please/#findComment-68730 Share on other sites More sharing options...
scottrohe Posted August 3, 2006 Author Share Posted August 3, 2006 everything else appears to be right and fine.. everything else displays fine and works fine but when i add the favorites part in, the favs part just.. doesnt work.. lol Quote Link to comment https://forums.phpfreaks.com/topic/16419-need-help-with-arrays-please/#findComment-68738 Share on other sites More sharing options...
bltesar Posted August 3, 2006 Share Posted August 3, 2006 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); Quote Link to comment https://forums.phpfreaks.com/topic/16419-need-help-with-arrays-please/#findComment-68748 Share on other sites More sharing options...
scottrohe Posted August 3, 2006 Author Share Posted August 3, 2006 [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 Link to comment https://forums.phpfreaks.com/topic/16419-need-help-with-arrays-please/#findComment-68760 Share on other sites More sharing options...
HeyRay2 Posted August 3, 2006 Share Posted August 3, 2006 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 favoriteswhile ($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] Quote Link to comment https://forums.phpfreaks.com/topic/16419-need-help-with-arrays-please/#findComment-68775 Share on other sites More sharing options...
bltesar Posted August 3, 2006 Share Posted August 3, 2006 [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 Link to comment https://forums.phpfreaks.com/topic/16419-need-help-with-arrays-please/#findComment-68791 Share on other sites More sharing options...
scottrohe Posted August 4, 2006 Author Share Posted August 4, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/16419-need-help-with-arrays-please/#findComment-69047 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.