kyleabaker Posted October 30, 2007 Share Posted October 30, 2007 First of all, I just want to point out that I'm very new to php and my code may not be the most ideal. I'm trying to make a better search page for my site that I've been slowly improving. I made a custom blog type of site just for fun I guess and I have a search page, but it's not working exactly how I want it to so I've taken up the task of rewriting it. So here is what I'm doing.. I'm splitting the search terms used into an array (works fine) and looping through the blog posts on my site searching for matches. If it finds a match then I add the number of matches for that word to a $hits variable and add the number for the next term and so on until all the words have been searched for that blog post. Then (the part that is giving me trouble) I am trying to loop through an array that I'm using to sort the results as I go according to the number of hits. I've tried several different things, but setting a specific array item doesn't seem to save as I have in my head. I guess I don't understand it well enough to think through it enough and catch the problem in my head, but I do understand how arrays work. Here is what I'm trying to do with the results that I collect.. (my reasoning and explanation is in the code, hopefully it helps get my goal across well enough) //$searchhits -> an array holding # of hits for each result so I can use it to figure out where to place the next results //$searchresults -> of course holds the results in the format that I want to echo out in the end //$hits -> integer number, # of hits for the specific blog post to be added to the arrays //$searchresultdata -> basic content from the blog post that I want to echo out for that result later if (strlen($searchresults[0]) == 0) { //**if the array is blank just add to start and be done with this result $searchhits[0]=$hits; $searchresults[0]=$searchresultdata; } else { //**else loop through the hits array and find the spot that this result should be inserted and shift everything else down for ($j = count($searchresults)-1; $j > -1; $j--) { //starting at bottom so I can shift items down if ($searchhits[$j]<$hits) { //shift item down and clear item's previous spot (just trying NULL to see if it made a different, it didn't) $searchhits[$j+1]=$searchhits[$j]; $searchresults[$j+1]=$searchresults[$j]; $searchhits[$j]=NULL; $searchresults[$j]=NULL; } else { //from bottom up, I'm assuming j+1 is clear since I NULL it if anything was once there and if it was the last spot that was compared then that spot+1 should be empty to add to $searchhits[$j+1]=$hits; $searchresults[$j+1]=$searchresultdata; } } } I'm fairly confident that the problem is hidden (to me) in the "else" inside the for loop, but I can't figure it out in my head why it won't work. I know you guys are gurus here, so this should be a piece of cake, haha. I'm looking for some simple code that I can understand by the way. Thanks in advanced..and sorry if it's an easy thing to fix. I've looked over the php manuals online for a while trying to figure this out. ??? Feel free to simplify my code! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/75330-trouble-with-arrays/ Share on other sites More sharing options...
kyleabaker Posted October 30, 2007 Author Share Posted October 30, 2007 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/75330-trouble-with-arrays/#findComment-381362 Share on other sites More sharing options...
sasa Posted October 30, 2007 Share Posted October 30, 2007 in the end of else part add line break; } else { //from bottom up, I'm assuming j+1 is clear since I NULL it if anything was once there and if it was the last spot that was compared then that spot+1 should be empty to add to $searchhits[$j+1]=$hits; $searchresults[$j+1]=$searchresultdata; break; } Quote Link to comment https://forums.phpfreaks.com/topic/75330-trouble-with-arrays/#findComment-381388 Share on other sites More sharing options...
kyleabaker Posted October 30, 2007 Author Share Posted October 30, 2007 haha, lmao. I figured it would be something so simple. So the rest of the logic makes sense? I will try this when I get out of class. Thanks sasa! Quote Link to comment https://forums.phpfreaks.com/topic/75330-trouble-with-arrays/#findComment-381395 Share on other sites More sharing options...
kyleabaker Posted October 30, 2007 Author Share Posted October 30, 2007 Still having a bit of trouble, but I have this one pin-pointed, just don't know how to handle it. Say I have the following number of hits in order that I find them: 1 1 1 1 3 My for loop is correcting shifting the entries for the first 4 down when it gets to a result with more hits, but I can't figure our how to insert it because my conditions aren't 100% correct. So when I run the search and it processes it shifts 4 down, but leaves the first result in position 1 followed by 1st,2nd,3rd,4th.. 1 1 1 1 1 Here is what I've got now.. if (strlen($searchresults[0]) == 0) { $searchhits[0]=$hits; $searchresults[0]=$searchresultdata; echo "(1) Post ID: ".$post_id." - Post Hits: ".$hits." - Array Count: ".count($searchresults)."<br>"; } else { for ($j = count($searchresults)-1; $j >= 0; $j--) { if ($searchhits[$j]<$hits) { $searchhits[$j+1]=$searchhits[$j]; $searchresults[$j+1]=$searchresults[$j]; echo "(2) Post ID: ".$post_id." - Post Hits: ".$hits." - Array Count: ".count($searchresults)."<br>"; } else { $searchhits[$j+1]=$hits; $searchresults[$j+1]=$searchresultdata; echo "(3) Post ID: ".$post_id." - Post Hits: ".$hits." - Array Count: ".count($searchresults)."<br>"; break; } } } and here is what it echoes.. (1) Post ID: 29 - Post Hits: 1 - Array Count: 1 (3) Post ID: 25 - Post Hits: 1 - Array Count: 2 (3) Post ID: 19 - Post Hits: 1 - Array Count: 3 (3) Post ID: 17 - Post Hits: 1 - Array Count: 4 (3) Post ID: 12 - Post Hits: 1 - Array Count: 5 (3) Post ID: 8 - Post Hits: 1 - Array Count: 6 (2) Post ID: 7 - Post Hits: 3 - Array Count: 7 (2) Post ID: 7 - Post Hits: 3 - Array Count: 7 (2) Post ID: 7 - Post Hits: 3 - Array Count: 7 (2) Post ID: 7 - Post Hits: 3 - Array Count: 7 (2) Post ID: 7 - Post Hits: 3 - Array Count: 7 (2) Post ID: 7 - Post Hits: 3 - Array Count: 7 Post ID is shifting everything down, but never caught to insert at position 0 in the array and I can't figure out how to catch it without catching all the others where $j is 0. Got any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/75330-trouble-with-arrays/#findComment-381479 Share on other sites More sharing options...
kyleabaker Posted October 30, 2007 Author Share Posted October 30, 2007 I threw in a boolean to see if be true if the item was added, if not then it adds to the beginning of the list since it was all shifted down, but I'm still looking for a better way if anyone can help. Quote Link to comment https://forums.phpfreaks.com/topic/75330-trouble-with-arrays/#findComment-381544 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.